Skip to content

Instantly share code, notes, and snippets.

@morningtoast
Last active October 13, 2015 18:08
Show Gist options
  • Select an option

  • Save morningtoast/4235585 to your computer and use it in GitHub Desktop.

Select an option

Save morningtoast/4235585 to your computer and use it in GitHub Desktop.
jQuery plugin template
jQuery(function($){
function debug(str) {
if (settings.debug) { console.log(str); }
}
// Helper methods
var helper = {
};
// Main methods
var methods = {
// Initial plugin, runs if parameter to plugin does not match another method
"init" : function(options) {
settings = {
}
// Override/combine variables with any that have been passed
if (options) { $.extend(settings, options); }
// Handling of targeted element; loop through each match
return this.each(function() {
// Do stuff here on the matched elements
}); // End each()
return(true);
}
}
// Plugin namespace and attachment - Syntax: $(element).kickappsVideo({options});
$.fn.MYPLUGINNAME = function(methodOrOptions) {
if ( methods[methodOrOptions] ) {
return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
// Default to "init"
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist' );
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment