Stefan Gabos web developer extraordinaire
jQuery Plugin Boilerplate
Please refer to the new version of the jQuery Plugin Boilerplate
- 1. Overview
- 2. Requirements
- 3. Comments
A boilerplate for jump-starting jQuery plugins development.
Contains lots of comments to help you get going easily. It implements public and private methods, as well as public and private properties making it the ideal candidate for when building both simple and complex jQuery plugins.
It does not adhere to the suggestions made by the jQuery documentation regarding Plugins/Authoring.
The jQuery Plugin Boilerplate took some inspiration from Doug Neiner‘s Starter.
With comments:
// jQuery Plugin Boilerplate
// A boilerplate for jumpstarting jQuery plugins development
// version 1.1, May 14th, 2011
// by Stefan Gabos
// remember to change every instance of "pluginName" to the name of your plugin!
(function($) {
// here we go!
$.pluginName = function(element, options) {
// plugin's default options
// this is private property and is accessible only from inside the plugin
var defaults = {
foo: 'bar',
// if your plugin is event-driven, you may provide callback capabilities
// for its events. execute these functions before or after events of your
// plugin, so that users may customize those particular events without
// changing the plugin's code
onFoo: function() {}
}
// to avoid confusions, use "plugin" to reference the
// current instance of the object
var plugin = this;
// this will hold the merged default, and user-provided options
// plugin's properties will be available through this object like:
// plugin.settings.propertyName from inside the plugin or
// element.data('pluginName').settings.propertyName from outside the plugin,
// where "element" is the element the plugin is attached to;
plugin.settings = {}
var $element = $(element), // reference to the jQuery version of DOM element
element = element; // reference to the actual DOM element
// the "constructor" method that gets called when the object is created
plugin.init = function() {
// the plugin's final properties are the merged default and
// user-provided options (if any)
plugin.settings = $.extend({}, defaults, options);
// code goes here
}
// public methods
// these methods can be called like:
// plugin.methodName(arg1, arg2, ... argn) from inside the plugin or
// element.data('pluginName').publicMethod(arg1, arg2, ... argn) from outside
// the plugin, where "element" is the element the plugin is attached to;
// a public method. for demonstration purposes only - remove it!
plugin.foo_public_method = function() {
// code goes here
}
// private methods
// these methods can be called only from inside the plugin like:
// methodName(arg1, arg2, ... argn)
// a private method. for demonstration purposes only - remove it!
var foo_private_method = function() {
// code goes here
}
// fire up the plugin!
// call the "constructor" method
plugin.init();
}
// add the plugin to the jQuery.fn object
$.fn.pluginName = function(options) {
// iterate through the DOM elements we are attaching the plugin to
return this.each(function() {
// if plugin has not already been attached to the element
if (undefined == $(this).data('pluginName')) {
// create a new instance of the plugin
// pass the DOM element and the user-provided options as arguments
var plugin = new $.pluginName(this, options);
// in the jQuery version of the element
// store a reference to the plugin object
// you can later access the plugin and its methods and properties like
// element.data('pluginName').publicMethod(arg1, arg2, ... argn) or
// element.data('pluginName').settings.propertyName
$(this).data('pluginName', plugin);
}
});
}
})(jQuery);
Without comments:
// jQuery Plugin Boilerplate
// A boilerplate for jumpstarting jQuery plugins development
// version 1.1, May 14th, 2011
// by Stefan Gabos
(function($) {
$.pluginName = function(element, options) {
var defaults = {
foo: 'bar',
onFoo: function() {}
}
var plugin = this;
plugin.settings = {}
var $element = $(element),
element = element;
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
// code goes here
}
plugin.foo_public_method = function() {
// code goes here
}
var foo_private_method = function() {
// code goes here
}
plugin.init();
}
$.fn.pluginName = function(options) {
return this.each(function() {
if (undefined == $(this).data('pluginName')) {
var plugin = new $.pluginName(this, options);
$(this).data('pluginName', plugin);
}
});
}
})(jQuery);
Usage
$(document).ready(function() {
// attach the plugin to an element
$('#element').pluginName({'foo': 'bar'});
// call a public method
$('#element').data('pluginName').foo_public_method();
// get the value of a property
$('#element').data('pluginName').settings.foo;
});
Requirements
jQuery 1.5.2+
It’s the only version I’ve tested the boilerplate with; it may be applicable to earlier versions, too. If you can test, please let me know. Thanks!



I am a 32 year old web developer working from Bucharest, Romania. I am coding since I was 14 and I am extremely passionate about it. For the server side of things I use PHP/MySQL while on the front-end I write valid HTML 5, nice CSS and lots of JavaScript code using jQuery.
Thanks for responding Stefan
sorry to disturb you further but i’m new at javascript and jquery and i want to be sure i develop the plugin correctly
can the plugin be called like this
?
in the boilerplate you have
is that a function or what ?
if i call the plugin like i said before is the onFoo called or i need to define the function like in your example
not sure if i got the info correctly i want to define some functions that (return some data or not) in my plugin and let the user call the functions
if i define a function can the user overwrite the functionality ?
example i bind a function to be executed onclik how can i let the user define his own code and bypass my function ?
thanks in advance for any response
Replyyou are making a confusion between public methods – methods that can be called from “outside” – and events which are fired by the plugin during different stages and are to be “hooked” from a custom function.
for the public methods read the comments and the usage examples. let’s say you have a tooltip plugin and in it you may a have a public method named “close” that hides the “tooltip”. you’d use $(‘#element’).data(‘pluginName’).close().
events are fired by the plugin during different stages and are to be “hooked” from a custom function. for our tooltip plugin we might want to have the “onClose” event. In this case, you’d replace “onFoo” with “onClose” (leave the function(){}) and in our public method called “close”, after all the code runs, we’d call “plugin.onClose()”. this way, if the user attached a custom function to the onClose() event, it will be executed when the tooltip closes.
thanks Stefan you have been a great help
Replycan you recommend some related books that will help me understand this thing better
I’m sorry, I can’t really help you with that. Best way to learn is trial and error. Or try a Google search. Or maybe this link: http://www.learningjquery.com/2010/07/great-ways-to-learn-jquery
I tested with jQuery 1.5.1 and it works fine…
Replythanks!
Hey Stefan,
really love your plugin boilerplate and have used it a few times by now.
I have one question however:
Is there a specific reason why the init function is public?
Replyno particular reason…you’re right it shouldn’t be public
I am very confused about using both $.pluginName and $.fn.pluginName. If the plugin will operate on the DOM why not just use the $.fn namespace.
Also the code inside $.fn.pluginName doesn’t allow calling public methods with strings which is the most recommended way to call plugin methods according to http://docs.jquery.com/Plugins/Authoring
ReplyThanks for this excellent boilerplate, I use it often. Just out of curiosity; the second variable declaration in line below seems redundant, is that there pure for aesthetics?
var $element = $(element), element = element;
(ps. the captcha on this page is quite unreadable)
Replyyes it is there so that both $element and element are visible side by side and users realize that there’s a difference
First of all, thanks for your work, it’s very handy
So, I made a few additions suiting my needs, but I would like your opinion on this specific change:
this way you don’t need to remember to change every instance of pluginName and it can also be referenced inside for logging and other purposes
Replythe code is perfectly ok I just don’t like how it looks
but it’s a subjective point of view
I perfectly understand your point of view. Thanks for replying.
Just came across this. Wish I had seen it sooner! Great!!
Thanks!
ReplyHow would you use this to extend an existing plugin? say there is a $.fn.baseplugin you want to extend do you then use $.fn.baseplugin.myoverrides to build the overriding one?
ReplyI made a small modification to ease the access to public methods:
$.fn.DialogPersonAuswahl = function (options) {
if (options == undefined && $(this).data(pluginName) != undefined)
return $(this).data(pluginName);
else
return this.each(function () {
if (undefined == $(this).data(‘DialogPersonAuswahl’)) {
var plugin = new $.DialogPersonAuswahl(this, options);
$(this).data(‘DialogPersonAuswahl’, plugin);
}
});
};
Access methods:
Reply// first call creates the plugin
$(‘#element’).pluginName().
// further calls access the plugins methods
$(‘#element’).pluginName().foo_public_method();
thanks for this, i read through the jquery docs for this and this is SOOO much easier to understand. thank you.
Reply