Created
July 25, 2011 03:53
-
-
Save al-the-x/1103536 to your computer and use it in GitHub Desktop.
jQuery Plugin Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See the last line for why "$" is important here... | |
(function($){ // closure | |
/** | |
* jsDocs go here... | |
*/ | |
$.fn.plugin = function( options ){ | |
/** | |
* @var jQuery collection passed to the plugin | |
*/ | |
var collection = this; | |
/** | |
* @var object of plugin options | |
*/ | |
options = $.extend({}, $.fn.plugin.defaults, options); | |
/** | |
* Always return the jQuery object for method chaining, and use each() | |
* to apply the desired behaviors to the Elements individually. | |
*/ | |
return collection.each(function(){ | |
/** | |
* @var Element from the collection, stripped of jQuery | |
*/ | |
var element = this; | |
/** | |
* @var jQuery collection with just the current Element | |
*/ | |
var $element = $(element); | |
/** | |
* @var object containing options for the current Element, taken from metadata, if available | |
*/ | |
var localOpts = ( $.meta ? | |
$.extend({}, options, $element.data()) : options | |
); | |
// DO SOMETHING USEFUL... | |
}); // END collection.each() | |
}; // END $.fn.plugin | |
// Private Functions are protected by the closure and can't be altered. | |
// function example ( param1, param2 ) | |
// { | |
// // Do something small... | |
// } // END example | |
// Public Functions are exposed and can be overridden or altered. | |
// $.fn.plugin.example = function( param1, param2 ){ | |
// // Do something small... | |
// }; // END site.example() | |
})(jQuery.noConflict()); // END closure | |
// jQuery is only registered as "$" within the closure... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment