Skip to content

Instantly share code, notes, and snippets.

@k-maru
Last active August 29, 2015 13:55
Show Gist options
  • Save k-maru/8690148 to your computer and use it in GitHub Desktop.
Save k-maru/8690148 to your computer and use it in GitHub Desktop.
Bootstrap Plugin Base
//change "Plugin"
function Plugin(element, options){
this._element = element;
this._options = options;
}
//change "Plugin"
Plugin.nullValue = {};
//change "pluginName"
$.fn.pluginName = function (methodOrOptions) {
var args = arguments,
firstResult,
chainResult = this.each(function () {
if (typeof firstResult !== "undefined") {
return;
}
var target = $(this),
//change "namespace.pluginName"
data = target.data("namespace.pluginName"),
methodArgs;
if (!data) {
//change "namespace.pluginName" & "Plugin"
target.data("namespace.pluginName", (data = new Plugin(target, methodOrOptions)));
}
if (typeof methodOrOptions === "string") {
methodArgs = [data];
Array.prototype.push.apply(methodArgs, Array.prototype.slice.call(args, 1));
firstResult = data[methodOrOptions].apply(target, methodArgs);
}
});
//change "Plugin"
return firstResult === Plugin.nullValue ? (void 0) : (firstResult || chainResult);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment