diff --git a/page/plugins/advanced_plugin_concepts.md b/page/plugins/advanced_plugin_concepts.md
index 1939e511..5e0b9f56 100644
--- a/page/plugins/advanced_plugin_concepts.md
+++ b/page/plugins/advanced_plugin_concepts.md
@@ -9,7 +9,7 @@ default plugin settings. This is important because it makes it very easy for
plugin users to override/customize the plugin with minimal code. And this is
where we begin to take advantage of the function object.
-
+```
// plugin definition
$.fn.hilight = function(options) {
// Extend our default options with those provided.
@@ -23,25 +23,25 @@ $.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
-
+```
Now users can include a line like this in their scripts:
-
+```
// this need only be called once and does not
// have to be called from within a 'ready' block
$.fn.hilight.defaults.foreground = 'blue';
-
+```
And now we can call the plugin method like this and it will use a blue foreground color:
-
+```
$('#myDiv').hilight();
-
+```
As you can see, we've allowed the user to write a single line of code to alter the default foreground color of the plugin. And users can still selectively override this new default value when they want:
-
+```
// override plugin default foreground color
$.fn.hilight.defaults.foreground = 'blue';
// ...
@@ -52,7 +52,7 @@ $('.hilightDiv').hilight();
$('#green').hilight({
foreground: 'green'
});
-
+```
### Provide public access to secondary functions as applicable
@@ -62,7 +62,7 @@ implementation of our plugin may define a function called "format" which
formats the hilight text. Our plugin may now look like this, with the default
implementation of the format method defined below the hilight function.
-
+```
// plugin definition
$.fn.hilight = function(options) {
@@ -80,7 +80,7 @@ $.fn.hilight = function(options) {
$.fn.hilight.format = function(txt) {'
return '' + txt + '';
};
-
+```
We could have just as easily supported another property on the options object
that allowed a callback function to be provided to override the default
@@ -100,11 +100,11 @@ that's where this type of extensibility is useful. The Cycle Plugin exposes a
"transitions" object to which users can add their own custom transition
definitions. It's defined in the plugin like this:
-
+```
$.fn.cycle.transitions = {
// ...
};
-
+```
This technique makes it possible for others to define and ship transition definitions that plug-in to the Cycle Plugin.
@@ -124,7 +124,7 @@ function will log the number of selected elements to the Firebug console. To
create a closure, we wrap the entire plugin definition in a function (as
detailed in the jQuery Authoring Guidelines).
-
+```
// create closure
(function($) {
// plugin definition
@@ -140,7 +140,7 @@ detailed in the jQuery Authoring Guidelines).
// ...
// end of closure
})(jQuery);
-
+```
Our "debug" method cannot be accessed from outside of the closure and thus is private to our implementation.
@@ -152,7 +152,7 @@ even more powerful. Personally, I love the Metadata Plugin because it lets you
use unobtrusive markup to override plugin options (which is particularly useful
when creating demos and examples). And supporting it is very simple!
-
+```
// plugin definition
$.fn.hilight = function(options) {
// ... build main options before element iteration
@@ -164,7 +164,7 @@ $.fn.hilight = function(options) {
var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
//...
-
+```
This line is added as the last argument to *jQuery.extend* so it will override
@@ -172,7 +172,7 @@ any other option settings. Now we can drive behavior from the markup if we
choose:
-
+```
Have a nice day!
@@ -183,19 +183,19 @@ choose:
Have a nice day!
-
+```
And now we can hilight each of these divs uniquely using a single line of script:
-
+```
$('.hilight').hilight();
-
+```
###Bob and Sue
Let's say Bob has created a wicked new gallery plugin (called "superGallery") which takes a list of images and makes them navigable. Bob's thrown in some animation to make it more interesting. He's tried to make the plugin as customizable as possible, and has ended up with something like this:
-
+```
jQuery.fn.superGallery = function(options) {
// Bob's default settings:
var defaults = {
@@ -226,7 +226,7 @@ jQuery.fn.superGallery = function(options) {
// ----------------------------
});
};
-
+```
The first thing that probably comes to your mind (ok, maybe not the first) is the prospect of how huge this plugin must be to accommodate such a level of customization. The plugin, if it weren't fictional, would probably be a lot larger than necessary. There are only so many kilobytes people will be willing to spend!
@@ -254,7 +254,7 @@ Developers who use your plugin shouldn't have to learn a new language or termino
Bob thought he was offering maximum customization with his *delay* option (look above). He made it so that with his plugin you can specify four different delays, "quite short," "very short," "quite long," or "very long":
-
+```
var delayDuration = 0;
switch (settings.delay) {
case 'very short' : delayDuration = 100;
@@ -267,7 +267,7 @@ switch (settings.delay) {
break;
default : delayDuration = 200
}
-
+```
Not only does this limit the level of control people have, but it takes up quite a bit of space. Twelve lines of code just to define the delaying time is a bit much, don't you think? A better way to construct this option would be to let plugin users specify the amount of time (in milliseconds) as a number, so that no processing of the option needs to take place.
@@ -279,7 +279,7 @@ If your plugin creates elements to be used within the DOM, then it's a good idea
A bad implementation:
-
+```
// Plugin code
$('<div id="the_gallery_Wrapper" />').appendTo('body');
$('#the_gallery_wrapper').append('...');
@@ -291,7 +291,7 @@ var $wrapper = $('<div />')
.attr(settings.wrapperAttrs)
.appendTo(settings.container);
$wrapper.append('...'); // Easy to reference later...
-
+```
Notice that we've created a reference to the injected wrapper and we're also calling the 'attr' method to add any specified attributes to the element. So, in our settings it might be handled like this:
@@ -309,7 +309,7 @@ Notice that we've created a reference to the injected wrapper and we're also cal
// We can use the extend method to merge options/settings as usual:
// But with the added first parameter of TRUE to signify a DEEP COPY:
var settings = $.extend(true, {}, defaults, options);
-
+```
The *$.extend()* method will now recurse through all nested objects to give us a merged version of both the defaults and the passed options, giving the passed options precedence.
@@ -317,7 +317,7 @@ The plugin user now has the power to specify any attribute of that wrapper eleme
The same model can be used to let the user define CSS styles:
-
+```
var defaults = {
wrapperCSS : {},
@@ -331,7 +331,7 @@ The same model can be used to let the user define CSS styles:
.attr(settings.wrapperAttrs)
.css(settings.wrapperCSS) // ** Set CSS!
.appendTo(settings.container);
-
+```
Your plugin may have an associated StyleSheet where developers can add CSS styles. Even in this situation it's a good idea to offer some convenient way of setting styles in JavaScript, without having to use a selector to get at the elements.
@@ -341,7 +341,7 @@ Your plugin may have an associated StyleSheet where developers can add CSS style
If your plugin is driven by events then it might be a good idea to provide a callback capability for each event. Plus, you can create your own custom events and then provide callbacks for those. In this gallery plugin it might make sense to add an 'onImageShow' callback.
-
+```
var defaults = {
onImageShow : function(){}, // we define an empty anonymous function
@@ -366,7 +366,7 @@ If your plugin is driven by events then it might be a good idea to provide a cal
Instead of initiating the callback via traditional means (adding parenthesis) we're calling it in the context of 'this' which will be a reference to the image node. This means that you have access to the actual image node through the 'this' keyword within the callback:
-
+```
$('ul.imgs li').superGallery({
onImageShow : function() {
@@ -378,7 +378,7 @@ Instead of initiating the callback via traditional means (adding parenthesis) we
// ...
});
-
+```
Similarly you could add an "onImageHide" callback and numerous other ones...
diff --git a/page/plugins/basic-plugin-creation.md b/page/plugins/basic-plugin-creation.md
index 6cfd907e..70807dbb 100644
--- a/page/plugins/basic-plugin-creation.md
+++ b/page/plugins/basic-plugin-creation.md
@@ -10,9 +10,9 @@ In this case, you may want to write a plugin.
Before we write our own plugins, we must first understand a little about how jQuery works. Take a look at this code:
-
+```
$('a').css('color','red');
-
+```
This is some pretty basic jQuery code, but do you know what's happening behind the scenes? Whenever you use the `$` function to select elements, it returns an object. This object contains all of the methods you've been using (`css()`, `click()`, etc.), and all of the elements that fit your selector. The `$` function gets the methods from the `$.fn` object. This object contains all of the jQuery methods, and If we want to write our own methods, it will need to contain those as well.
@@ -20,13 +20,13 @@ This is some pretty basic jQuery code, but do you know what's happening behind t
Let's say we want to create a plugin that makes text green. All we have to do is add a function called `greenify` to `$.fn` and it will available just like any other method.
-
+```
$.fn.greenify = function () {
this.css('color','green');
}
$('a').greenify(); // makes all the links green
-
+```
Notice that to use `css()`, another method, we use `this`, not `$(this)`. This is because our `greenify` function is a part of the same object as `css()`.
@@ -34,31 +34,31 @@ Notice that to use `css()`, another method, we use `this`, not `$(this)`. This i
This works, but there's a couple of things we need to do for our plugin to survive in the real world. One of jQuery's features is chaining, when you link five or six actions onto one selector. This is accomplished by having all jQuery methods return the original jQuery object again (there are a few exeptions: `width()` called without parameters returns the width of the selected element, and is not chainable). Making our plugin chainable takes one line of code:
-
+```
$.fn.greenify = function () {
this.css('color','green');
return this;
}
$('a').greenify().addClass('greenified');
-
+```
##Adding scope
The `$` variable is very popular among javascript libraries, and if you're using one with jQuery, you will have to make jQuery not use the `$` with `jQuery.noConflict()`. However, this will break our plugin. To work well with other plugins, _and_ still use the jQuery `$` variable, we need to put all of our code inside of an [Immediately Invoked Function Expression](http://stage.learn.jquery.com/javascript-101/functions/#immediately-invoked-function-expression), and then pass the function `jQuery`, and name the parameter `$`:
-
+```
(function ($) {
$.fn.greenify = function () {
this.css('color','green');
return this;
}
}(jQuery));
-
+```
In addition, the primary purpose of an Immediately Invoked Function is to allow us to have our own private variables. Pretend we want a different color green, and we want to store it in a variable.
-
+```
(function ($) {
var shade = '#556B2F';
@@ -67,13 +67,13 @@ In addition, the primary purpose of an Immediately Invoked Function is to allow
return this;
}
}(jQuery));
-
+```
##Minimizing Plugin Footprint
It's good practice when writing plugins to only take up one slot within `$.fn`. This reduces both the chance that your plugin will be overriden, and the chance that your plugin will override other plugins. In other words, this is bad:
-
+```
(function ($) {
$.fn.openPopup = function () {
// Open popup code
@@ -84,11 +84,11 @@ It's good practice when writing plugins to only take up one slot within `$.fn`.
};
}(jQuery));
-
+```
It would be much better to have one slot, and use parameters to control what action that one slot performs.
-
+```
(function ($) {
$.fn.popup = function (action) {
if( action === 'open') {
@@ -99,7 +99,7 @@ It would be much better to have one slot, and use parameters to control what act
};
}(jQuery));
-
+```
##Using the each() method
@@ -109,13 +109,13 @@ If you want to do any manipulating with specific elements (eg: getting data an
attribute, calculating specific positions) then you need to use `each()` to
loop through the elements.
-
+```
$.fn.myNewPlugin = function() {
return this.each(function(){
// do something to each element here
});
};
-
+```
Notice that we return the results of `each()` instead of returning `this`.
Since `each()` is already chainable, it returns `this`, which we then return.
@@ -128,7 +128,7 @@ customizable by accepting options. The easiest way do this, especially if there
are lots of options, is with an object literal. Let's change our greenify plugin to
accept some options.
-
+```
(function ($) {
$.greenify = function (options) {
// This is the easiest way to have default options.
@@ -144,15 +144,15 @@ accept some options.
});
};
}(jQuery));
-
+```
Example usage:
-
+```
$('div').greenify({
'color': 'orange'
});
-
+```
The default value for `color` of `#556B2F` gets overriden by `$.extend` to be orange.
@@ -161,7 +161,7 @@ The default value for `color` of `#556B2F` gets overriden by `$.extend` to be or
Here's an example of a small plugin using some of the techniques
we've discussed:
-
+```
(function($){
$.fn.showLinkLocation = function() {
return this.filter('a').each(function(){
@@ -172,22 +172,22 @@ we've discussed:
// Usage example:
$('a').showLinkLocation();
-
+```
This handy plugin goes through all anchors in the collection and appends the
href attribute in brackets.
-
+```
Foo
Foo (page.html)
-
+```
Our plugin can be optimized though:
-
+```
(function($){
$.fn.showLinkLocation = function() {
return this.filter('a').append(function(){
@@ -195,7 +195,7 @@ Our plugin can be optimized though:
});
};
}(jQuery));
-
+```
We're using the `append` method's capability to accept a callback, and the
return value of that callback will determine what is appended to each element
diff --git a/page/plugins/stateful-plugins-with-widget-factory.md b/page/plugins/stateful-plugins-with-widget-factory.md
index da61acec..f800c186 100644
--- a/page/plugins/stateful-plugins-with-widget-factory.md
+++ b/page/plugins/stateful-plugins-with-widget-factory.md
@@ -25,7 +25,7 @@ This is different from a standard jQuery plugin in two important ways.
First, the context is an object, not a DOM element.
Second, the context is always a single object, never a collection.
-
+```
A simple, stateful plugin using the jQuery UI widget factory
$.widget("nmk.progressbar", {
@@ -36,7 +36,7 @@ A simple, stateful plugin using the jQuery UI widget factory
.text(progress);
}
});
-
+```
The name of the plugin must contain a namespace; in this case we’ve used the `nmk` namespace.
There is a limitation that namespaces be exactly one level deep — that is, we can't use a namespace like `nmk.foo`.
@@ -52,13 +52,13 @@ In our example we use the `nmk` namespace. The `ui` namespace is reserved for of
This makes it clear where the plugin came from and whether it is part of a larger collection.
-
+```
Passing options to a widget
$("<div></div>")
.appendTo( "body" )
.progressbar({ value: 20 });
-
+```
When we call `jQuery.widget `it extends jQuery by adding a method to `jQuery.fn` (the same way we'd create a standard plugin).
The name of the function it adds is based on the name you pass to `jQuery.widget`, without the namespace; in our case it will create `jQuery.fn.progressbar`.
@@ -66,7 +66,7 @@ The options passed to our plugin get set in `this.options` inside of our plugin
As shown below, we can specify default values for any of our options.
When designing your API, you should figure out the most common use case for your plugin so that you can set appropriate default values and make all options truly optional.
-
+```
Setting default options for a widget
$.widget("nmk.progressbar", {
@@ -82,7 +82,7 @@ Setting default options for a widget
.text( progress );
}
});
-
+```
### Adding Methods to a Widget
@@ -90,7 +90,7 @@ Now that we can initialize our progress bar, we’ll add the ability to perform
To define a plugin method, we just include the function in the object literal that we pass to `jQuery.widget`.
We can also define “private” methods by prepending an underscore to the function name.
-
+```
Creating widget methods
$.widget("nmk.progressbar", {
@@ -129,11 +129,11 @@ Creating widget methods
return value;
}
});
-
+```
To call a method on a plugin instance, you pass the name of the method to the jQuery plugin. If you are calling a method that accepts parameters, you simply pass those parameters after the method name.
-
+```
Calling methods on a plugin instance
var bar = $("<div></div>")
@@ -148,7 +148,7 @@ Calling methods on a plugin instance
// get the current value again
alert(bar.progressbar("value"));
-
+```
### Note
@@ -165,7 +165,7 @@ When used as a getter, the plugin will return the current value of the option th
When used as a setter, the plugin’s `_setOption` method will be called for each option that is being set.
We can specify a `_setOption` method in our plugin to react to option changes.
-
+```
Responding when an option is set
$.widget("nmk.progressbar", {
@@ -188,7 +188,7 @@ Responding when an option is set
this.element.text(progress);
}
});
-
+```
### Adding Callbacks
@@ -204,7 +204,7 @@ pass the native mousemove event when triggering a drag callback; this would
allow users to react to the drag based on the x/y coordinates provided by the
event object.
-
+```
Providing callbacks for user extension
$.widget("nmk.progressbar", {
@@ -230,7 +230,7 @@ Providing callbacks for user extension
}
}
});
-
+```
Callback functions are essentially just additional options, so you can get and
set them just like any other option. Whenever a callback is executed, a
@@ -246,7 +246,7 @@ event: by calling `event.preventDefault()` or using `return false`. If the user
cancels the callback, the `_trigger` method will return false so you can
implement the appropriate functionality within your plugin.
-
+```
Binding to widget events
var bar = $("
")
@@ -262,7 +262,7 @@ Binding to widget events
});
bar.progressbar("option", "value", 100);
-
+```
### The Widget Factory: Under the Hood
@@ -270,7 +270,7 @@ When you call jQuery.widget, it creates a constructor function for your plugin a
Because the plugin instance is directly linked to the DOM element, you can access the plugin instance directly instead of going through the exposed plugin method if you want. This will allow you to call methods directly on the plugin instance instead of passing method names as strings and will also give you direct access to the plugin’s properties.
-
+```
var bar = $("
")
.appendTo("body")
.progressbar()
@@ -281,7 +281,7 @@ Because the plugin instance is directly linked to the DOM element, you can acces
// access properties on the plugin instance
alert(bar.options.value);
-
+```
One of the biggest benefits of having a constructor and prototype for a plugin
is the ease of extending the plugin. By adding or modifying methods on the
@@ -290,11 +290,11 @@ For example, if we wanted to add a method to our progress bar to reset the
progress to 0% we could add this method to the prototype and it would instantly
be available to be called on any plugin instance.
-
+```
$.nmk.progressbar.prototype.reset = function() {
this._setOption("value", 0);
};
-
+```
### Cleaning Up
@@ -307,7 +307,7 @@ can be used for garbage collection as well. The default `destroy` method removes
the link between the DOM element and the plugin instance, so it’s important to
call the base function from your plugin’s `destroy` method.
-
+```
Adding a destroy method to a widget
$.widget( "nmk.progressbar", {
@@ -342,7 +342,7 @@ Adding a destroy method to a widget
$.Widget.prototype.destroy.call(this);
}
});
-
+```
### Conclusion