Skip to content

Instantly share code, notes, and snippets.

@pulse00
Created July 24, 2014 13:48
Show Gist options
  • Save pulse00/3a15eaea2a7b1c9b9d01 to your computer and use it in GitHub Desktop.
Save pulse00/3a15eaea2a7b1c9b9d01 to your computer and use it in GitHub Desktop.
var __slice = [].slice;
(function($, window) {
var MyPlugin;
MyPlugin = (function() {
MyPlugin.prototype.defaults = {
paramA: 'foo',
paramB: 'bar'
};
function MyPlugin(el, options) {
this.options = $.extend({}, this.defaults, options);
this.$el = $(el);
}
MyPlugin.prototype.myMethod = function(echo) {
return this.$el.html(this.options.paramA + ': ' + echo);
};
return MyPlugin;
})();
return $.fn.extend({
myPlugin: function() {
var args, option;
option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return this.each(function() {
var $this, data;
$this = $(this);
data = $this.data('myPlugin');
if (!data) {
$this.data('myPlugin', (data = new MyPlugin(this, option)));
}
if (typeof option === 'string') {
return data[option].apply(data, args);
}
});
}
});
})(window.jQuery, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment