Skip to content

Instantly share code, notes, and snippets.

@steelywing
Last active March 11, 2020 19:57
Show Gist options
  • Save steelywing/9438061 to your computer and use it in GitHub Desktop.
Save steelywing/9438061 to your computer and use it in GitHub Desktop.
jQuery plugin boilerplate
/*
* Project:
* Description:
* Author:
* License:
*/
// 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 is 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).
var // plugin name
pluginName = "pluginName",
// key using in $.data()
dataKey = "plugin_" + pluginName;
var privateMethod = function () {
// ...
};
var Plugin = function (element, options) {
this.element = element;
this.options = {
// default options
};
/*
* Initialization
*/
this.init(options);
};
Plugin.prototype = {
// initialize options
init: function (options) {
$.extend(this.options, options);
/*
* Update plugin for options
*/
},
publicMethod: function () {
// ...
}
};
/*
* Plugin wrapper, preventing against multiple instantiations and
* return plugin instance.
*/
$.fn[pluginName] = function (options) {
var plugin = this.data(dataKey);
// has plugin instantiated ?
if (plugin instanceof Plugin) {
// if have options arguments, call plugin.init() again
if (typeof options !== 'undefined') {
plugin.init(options);
}
} else {
plugin = new Plugin(this, options);
this.data(dataKey, plugin);
}
return plugin;
};
}(jQuery, window, document));
/*
* Project:
* Description:
* Author:
* License:
*/
// 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 is 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).
var pluginName = "pluginName",
// the name of using in .data()
dataPlugin = "plugin-" + pluginName,
// default options
defaults = {};
var privateMethod = function () {};
// The actual plugin constructor
var Plugin = function ( element ) {
/*
* Plugin instantiation
*/
this.options = $.extend( {}, defaults );
};
Plugin.prototype = {
init: function ( options ) {
// extend options ( http://api.jquery.com/jQuery.extend/ )
$.extend( this.options, options );
/*
* Place initialization logic here
*/
},
destroy: function () {
// unset Plugin data instance
this.element.data( dataPlugin, null );
},
// public get method
publicMethod: function () {
return this;
},
}
/*
* Plugin wrapper, preventing against multiple instantiations and
* allowing any public function to be called via the jQuery plugin,
* e.g. $(element).pluginName('functionName', arg1, arg2, ...)
*/
$.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 );
}
// 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