Skip to content

Instantly share code, notes, and snippets.

@drzax
Created August 8, 2012 09:56
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 drzax/3293915 to your computer and use it in GitHub Desktop.
Save drzax/3293915 to your computer and use it in GitHub Desktop.
jQuery Plugin Boilerplate
/*
* jQuery.name plugin
* https://github.com/{username}/{plugin-name}
*
* Copyright 2011, {real name}
* {website}
*
* This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
* http://creativecommons.org/licenses/by-sa/3.0/
*/
;(function(window, undefined){
// Info about this plugin
var jQueryRequiredVersion = 1.7, // The required version of jQuery
pluginName = 'pluginName'; // The name of the plugin
// Give up if jQuery doesn't exist.
if (!window.jQuery || window.jQuery.fn.jquery < jQueryRequiredVersion) {
log(pluginName + ' is a jQuery plugin. jQuery is definitely required, and it must be greater than version ' + jQueryRequiredVersion);
return;
}
var $ = window.jQuery;
/*
* Make error logging easier.
*/
var log = function(message, error){
if ( window.console && window.console.log ) {
window.console.log(message);
}
if (error) $.error(message);
}
var methods = {
init : function(_opts) {
var opts = $.extend( {}, $.fn[pluginName].defaults || {}, _opts || {} );
return this.each(function() {
});
}
}
// The plugin function
$.fn[pluginName] = function( method ) {
if ( methods[method] ) {
// Remove the first argument (method name) and send on to the requested method.
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
// Initialise
return methods.init.apply( this, arguments );
} else {
log( '[' + pluginName + '] method ' + method + ' does not exist', true );
return this;
}
}
// Default options exposed so they can be updated globally.
$.fn[pluginName].defaults = {}
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment