Created
April 29, 2019 12:56
-
-
Save man-oi/7fb3b9137e63b7fe1ec7b73f3c8bddbd to your computer and use it in GitHub Desktop.
jquery plugin template
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 () { | |
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