Created
October 18, 2014 13:14
-
-
Save enijar/d054153a80d603411507 to your computer and use it in GitHub Desktop.
jQuery Plugin Boilerplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* 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