Created
October 29, 2013 23:26
-
-
Save lukesutton/7224573 to your computer and use it in GitHub Desktop.
Scaffold for a jQuery plugin.
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($) { | |
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