Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Created July 25, 2011 03:53
Show Gist options
  • Save al-the-x/1103536 to your computer and use it in GitHub Desktop.
Save al-the-x/1103536 to your computer and use it in GitHub Desktop.
jQuery Plugin Template
// 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