Last active
August 29, 2015 14:08
-
-
Save iambigd/8630211d2210cdecc357 to your computer and use it in GitHub Desktop.
jQuery plugin Template
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
/* | |
* Template plugin | |
* @author [email protected] | |
* @date 20141022 | |
*/ | |
;(function($) { | |
//define plugin name | |
var pluginName = 'jqueryPlugin'; | |
//create plugin class | |
function Plugin (element,options){ | |
this.el = element; | |
this.$el = $(element); | |
this.options = $.extend({}, $.fn[pluginName].defaults, options); | |
//constrctor | |
this.init(); | |
return this; | |
}; | |
Plugin.prototype.name = pluginName; | |
Plugin.prototype.version = '0.0.1'; | |
Plugin.prototype = { | |
init : function(){ | |
var plugin = this; | |
}, | |
/** | |
* The 'destroy' method is were you free the resources used by your plugin: | |
* references, unregister listeners, etc. | |
* | |
* Remember to unbind for your event: | |
* | |
* @example | |
* this.$someSubElement.off('.' + pluginName); | |
* | |
* Above example will remove any listener from your plugin for on the given | |
* element. | |
*/ | |
destroy: function() { | |
// Remove any attached data from your plugin | |
this.$el.removeData(); | |
}, | |
/** | |
* Write public methods within the plugin's prototype. They can | |
* be called with: | |
* | |
* @example | |
* $('#element').jqueryPlugin('somePublicMethod','Arguments', 'Here', 1001); | |
* | |
* @param {[type]} foo [some parameter] | |
* @param {[type]} bar [some other parameter] | |
* @return {[type]} | |
*/ | |
pubMethod : function(){ | |
} | |
} | |
/** | |
* This is a real private method. A plugin instance has access to it | |
* @return {[type]} | |
*/ | |
var privateMethod = function() { | |
console.log("privateMethod"); | |
console.log(this); | |
}; | |
// Plugin wrapper around the constructor, | |
$.fn[pluginName] = function(options) { | |
var args = arguments; | |
if (options === undefined || typeof options === 'object') { | |
// Create a plugin instance for each selected element. | |
return this.each(function() { | |
if (!$.data(this, 'plugin_' + pluginName)) { | |
$.data(this, 'plugin_' + pluginName, new Plugin(this, options)); | |
} | |
}); | |
} else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { | |
// Call a pluguin method for each selected element. | |
if (Array.prototype.slice.call(args, 1).length == 0 && $.inArray(options, $.fn[pluginName].getters) != -1) { | |
// If the user does not pass any arguments and the method allows to | |
// work as a getter then break the chainability | |
var instance = $.data(this[0], 'plugin_' + pluginName); | |
return instance[options].apply(instance, Array.prototype.slice.call(args, 1)); | |
} else { | |
// Invoke the speficied method on each selected element | |
return this.each(function() { | |
var instance = $.data(this, 'plugin_' + pluginName); | |
if (instance instanceof Plugin && typeof instance[options] === 'function') { | |
instance[options].apply(instance, Array.prototype.slice.call(args, 1)); | |
} | |
}); | |
} | |
} | |
} | |
/** | |
* Names of the pluguin methods that can act as a getter method. | |
* @type {Array} | |
*/ | |
$.fn[pluginName].getters = ['pubMethod']; | |
/** | |
* Default options | |
*/ | |
$.fn[pluginName].defaults = { | |
defaultOption: "I'm a default option" | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment