Last active
December 20, 2015 16:49
-
-
Save oscarotero/6163869 to your computer and use it in GitHub Desktop.
jQuery plugin boilerplate
This file contains 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
;(function ($, window, document, undefined) { | |
var pluginName = "name", defaults = {}; | |
function Plugin (element, options) { | |
this.element = element; | |
this.settings = $.extend({}, defaults, options); | |
this.init(); | |
} | |
Plugin.prototype = { | |
init: function () { | |
} | |
}; | |
$.fn[pluginName] = function (options) { | |
if ((options === undefined) || (typeof options === 'object')) { | |
return this.each(function () { | |
if (!$.data(this, "plugin_" + pluginName)) { | |
$.data(this, "plugin_" + pluginName, new Plugin(this, options)); | |
} | |
}); | |
} | |
if ((typeof options === 'string') && (options[0] !== '_') && (options !== 'init')) { | |
var returns, args = arguments; | |
this.each(function () { | |
var instance = $.data(this, 'plugin_' + pluginName); | |
if ((instance instanceof Plugin) && (typeof instance[options] === 'function')) { | |
returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1)); | |
} | |
if (options === 'destroy') { | |
$.data(this, 'plugin_' + pluginName, null); | |
} | |
}); | |
return returns !== undefined ? returns : this; | |
} | |
}; | |
})(jQuery, window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment