Skip to content

Instantly share code, notes, and snippets.

@lukesutton
Created October 29, 2013 23:26
Show Gist options
  • Save lukesutton/7224573 to your computer and use it in GitHub Desktop.
Save lukesutton/7224573 to your computer and use it in GitHub Desktop.
Scaffold for a jQuery plugin.
(function($) {
function Plugin($el, opts) {
this.$el = $el;
this.settings = $.extend({
// Add default settings here.
}, opts);
}
Plugin.prototype = {
_run: function(name, args) {
if (typeof this[name] === "function") {
return this[name].apply(this, args);
}
else {
throw "$.fn.plugin does not have a function called " + name;
}
}
};
$.fn.plugin = function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < this.length; i++) {
var $this = $(this[i]);
if (!$this.is('element')) {
throw "$.fn.plugin can only be called on element elements";
}
if ($this.data('plugin')) {
return $this.data('plugin')._run(args.shift(), args);
}
else {
$this.data('plugin', new Plugin($this, args[0]));
}
});
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment