Skip to content

Instantly share code, notes, and snippets.

@garystorey
Created May 7, 2013 16:40
Show Gist options
  • Select an option

  • Save garystorey/5534087 to your computer and use it in GitHub Desktop.

Select an option

Save garystorey/5534087 to your computer and use it in GitHub Desktop.
A basic setup for a jQuery plugin
//PLUGINNAME === Pluginname (UpperCase first letter)
//PLUGIN === pluginname (all lowercase)
var PLUGINNAME = function( $elm, options ) {
this.debug = false;
this.$el = $elm;
this.options = jQuery.extend( true, {}, $.fn.PLUGIN.defaults, options );
this.$el.data('PLUGINNAME', this);
};
PLUGINNAME.prototype = {
init : function () { },
destroy : function () { }
};
jQuery.fn.PLUGIN = function( options ){
if ( typeof options !== 'string' ) {
return this.each( function () {
var obj = new PLUGINNAME( jQuery( this ), options );
obj.init();
});
} else {
PLUGINNAME = $(this).data('PLUGINNAME');
if ( PLUGINNAME !== null ) {
switch( options ) {
case 'init':
PLUGINNAME.init();
break;
case 'destroy':
PLUGINNAME.destroy();
break;
}
}
}
};
jQuery.fn.PLUGIN.defaults = {
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment