Skip to content

Instantly share code, notes, and snippets.

@bunnymatic
Created June 26, 2011 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunnymatic/1048060 to your computer and use it in GitHub Desktop.
Save bunnymatic/1048060 to your computer and use it in GitHub Desktop.
jquery plugin boilerplate
// jquery plugin boilerplate
// this plugin has methods and manages separate settings for each instance
$.myPluginDefaults = {
imageContainer: '.ir_img_container',
imageUrls: []
};
$.fn.myPlugin = function( method ) {
var inArgs = arguments;
var methods = {
init: function(options) {
// you may want to skip this step for performance if you don't need each element
// do have the options data readily available.
var localSettings = $.extend({},$.myPluginDefaults, options);
$(this).data(localSettings);
// if you expect $(this) to be multi select, you may need to wrap
// initialization or binding in another $.each( $(this), function() {}); type block
},
};
return this.each(function() {
// If options exist, send them to init
// and merge with default settings
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( inArgs, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, inArgs );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.myPlugin' );
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment