Skip to content

Instantly share code, notes, and snippets.

@man-oi
Created April 29, 2019 12:56
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 man-oi/7fb3b9137e63b7fe1ec7b73f3c8bddbd to your computer and use it in GitHub Desktop.
Save man-oi/7fb3b9137e63b7fe1ec7b73f3c8bddbd to your computer and use it in GitHub Desktop.
jquery plugin template
(function () {
var pluginName = "pluginName",
defaults = {
propertyName: "value"
};
// The actual plugin constructor
function Plugin(element, options) {
this.element = element;
// jQuery has an extend method which merges the contents of two or
// more objects, storing the result in the first object. The first object
// is generally empty as we don't want to alter the default options for
// future instances of the plugin
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function () {
this.$element = $(this.element);
this.bindEvents();
},
bindEvents: function () {}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment