Skip to content

Instantly share code, notes, and snippets.

@autioch
Created September 27, 2015 13:17
Show Gist options
  • Save autioch/8e661307479d5524de48 to your computer and use it in GitHub Desktop.
Save autioch/8e661307479d5524de48 to your computer and use it in GitHub Desktop.
Template for jQuery Template
/* jQuery plugin template
* Replace Plugin__Constructor with name of constructor function.
* Replace plugin__name with string name, used for namespacing events, data binding.
**/
(function(Window, Document, $, factory) {
'use strict';
var theModule = factory($, Document);
if (typeof define === 'function' && define.amd) {
define(theModule);
} else if ('undefined' !== typeof module && module.exports) {
module.exports = theModule;
} else {
$.fn.Plugin__Constructor = theModule;
}
}(window, document, jQuery, function($, document) { /* jshint ignore:line */
'use strict';
var defaultSettings = {
};
/* Instance counter for binding global events for specific instance */
var cid = 0;
/* PLugin name variable for using name as string */
var pluginName = 'plugin__name';
/* Returns eventname specific to plugin instance */
function getEventName(instance, eventName) {
return eventName + '.' + pluginName + instance.__cid;
}
/* Plugin constructor */
function Plugin__Constructor(element, options) {
var that = this;
that.el = element;
that.$el = $(element).data(pluginName, that);
that.__cid = cid++;
that.__settings = $.extend({}, defaultSettings, options);
that.init(that.__settings);
}
$.extend(Plugin__Constructor.prototype, {
$: function(query) {
return this.$el.find(query);
},
/* Binding to events specific to current instance */
on: function($el, eventName, delegate, eventHandler) {
if ($.isFunction(delegate) && !eventHandler) {
$el.on(getEventName(this, eventName), delegate.bind(this));
} else {
$el.on(getEventName(this, eventName), delegate, eventHandler.bind(this));
}
return this;
},
/* Unbinding from events specific to current instance */
off: function($el, eventName) {
if ('undefined' !== typeof eventName) {
$el.off(getEventName(this, eventName));
} else {
$el.off();
}
return this;
},
/* Initialize plugin */
init: function(options) {
this.on(this.$el, 'click', this.onClick);
},
onClick: function() {
alert('test');
},
/* Always give option to destroy instance of plugin */
destroy: function() {
/* Unbind all events of plugin */
this.off(this.$el, '');
this.$el.removeData(pluginName);
/* Remove all properties of an instance to prevent memory leaks */
for (var prop in this) {
if (this.hasOwnProperty(prop)) {
this[prop] = null;
}
}
}
});
return function(options) {
var data;
return this.each(function() {
data = $(this).data(pluginName);
if (!(data instanceof Plugin__Constructor)) {
return new Plugin__Constructor(this, options);
}
/* if it is already initialized and has requested method */
if (data[options] && (typeof data[options] === 'function')) {
/* call the method with arguments except the first one (method name) */
data[options].call(data, Array.prototype.slice.call(arguments, 1));
}
});
};
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment