Skip to content

Instantly share code, notes, and snippets.

@kevinblake
Last active December 28, 2015 21:19
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 kevinblake/7564149 to your computer and use it in GitHub Desktop.
Save kevinblake/7564149 to your computer and use it in GitHub Desktop.
jquery plugin boilerplate
// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
; (function ($, window, document, undefined) {
// undefined is used here as the undefined global variable in ECMAScript 3 is
// mutable (ie. it can be changed by someone else). undefined isn't really being
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
// can no longer be modified.
// window and document are passed through as local variable rather than global
// as this (slightly) quickens the resolution process and can be more efficiently
// minified (especially when both are regularly referenced in your plugin).
// Create the defaults once
var pluginName = "defaultPluginName",
dataPlugin = "plugin_" + pluginName,
defaults = {
propertyName: "value"
};
// The actual plugin constructor
function Plugin(element, options) {
this.element = element;
// jQuery has an extend method which merges the contents of two or
// more objects, storing the result in the first object. The first object
// is generally empty as we don't want to alter the default options for
// future instances of the plugin
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
}
Plugin.prototype = {
init: function () {
// Place initialization logic here
// You already have access to the DOM element and
// the options via the instance, e.g. this.element
// and this.options
// you can add more functions like the one below and
// call them like so: this.yourOtherFunction(this.element, this.options).
console.log("xD");
},
yourOtherFunction: function () {
// some logic
}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function (arg) {
var args, instance;
// only allow the plugin to be instantiated once
if (!(this.data(dataPlugin) instanceof Plugin)) {
// if no instance, create one
this.data(dataPlugin, new Plugin(this));
}
instance = this.data(dataPlugin);
instance.element = this;
// Is the first parameter an object (arg), or was omitted,
// call Plugin.init( arg )
if (typeof arg === 'undefined' || typeof arg === 'object') {
if (typeof instance['init'] === 'function') {
instance.init(arg);
}
return instance;
// checks that the requested public method exists
} else if (typeof arg === 'string' && typeof instance[arg] === 'function') {
// copy arguments & remove function name
args = Array.prototype.slice.call(arguments, 1);
// call the method
return instance[arg].apply(instance, args);
} else {
$.error('Method ' + arg + ' does not exist on jQuery.' + pluginName);
}
};
})(jQuery, window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment