Skip to content

Instantly share code, notes, and snippets.

@akikoo
Created June 10, 2012 16:27
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 akikoo/2906490 to your computer and use it in GitHub Desktop.
Save akikoo/2906490 to your computer and use it in GitHub Desktop.
jQuery plugin boilerplate
/*!
* jQuery plugin boilerplate
*
* Inspiration:
* http://msdn.microsoft.com/en-us/scriptjunkie/ff608209
* http://www.learningjquery.com/2007/10/a-plugin-development-pattern
* http://shichuan.github.com/javascript-patterns/
*
* Author: Aki Karkkainen/@akikoo
* Licensed under the MIT license
*/
;(function ($, window, document, undefined) {
"use strict";
// Plugin definition
// Shortcut for $.prototype.myplugin = function(options) {}
$.fn.myplugin = function (options) {
// Extend our default options with those provided
var settings = $.extend({}, $.fn.myplugin.defaults, options);
// "this" is already a jquery object
return this.each(function () {
// Assign the current object to a variable for easier use
var $this = $(this),
// Merge in the metadata elements (Metadata plugin) for this specific node
o = $.metadata ? $.extend({}, settings, $.metadata.get(this)) : settings;
// Call private function
//debug($this,o);
// Call public function
// $.fn.plugin.funcname();
});
};
// Private function
function debug($obj, opts) {
//Options are available like so: opts.opt1
}
// Public function
$.fn.myplugin.funcname = function () {
return this;
};
// Plugin defaults
// To override one of the default values used for future instances of a plugin,
// write code like the following: $.fn.myplugin.defaults.opt1 = 'value3';
$.fn.myplugin.defaults = {
opt1 : 'value1',
opt2 : 'value2',
complete : null
};
}(jQuery, window, document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment