Skip to content

Instantly share code, notes, and snippets.

@elidupuis
Created December 17, 2010 01:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elidupuis/744331 to your computer and use it in GitHub Desktop.
Save elidupuis/744331 to your computer and use it in GitHub Desktop.
A base structure to use when building new plugins!
(function($) {
var methods = {
init: function( options ) {
// iterate and reformat each matched element
return this.each(function() {
var $this = $(this),
opts = $.extend({}, $.fn.PLUGIN_NAME.defaults, options),
data = $this.data('PLUGIN_NAME');
// If the plugin hasn't been initialized yet
if ( ! data ) {
// do all your main awesomeness in here...
// attach
$this.data('PLUGIN_NAME', {
target : $this,
opts: opts
});
};
});
},
update: function() {
// each method must loop through all selected elements and return 'this'.
return this.each(function() {
if(window.console) window.console.log('update called.');
});
}
};
// main plugin declaration:
$.fn.PLUGIN_NAME = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.PLUGIN_NAME' );
};
};
// defaults
$.fn.PLUGIN_NAME.defaults = {
optionA: true
// etc...
};
$.fn.PLUGIN_NAME.publicfunc = function() { return "jquery.PLUGIN_NAME public function. "; };
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment