Last active
December 15, 2015 03:49
-
-
Save backflip/5197613 to your computer and use it in GitHub Desktop.
jQuery Plugin Boilerplate – Based on http://markdalgleish.com/2011/05/creating-highly-configurable-jquery-plugins/
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
/** | |
* Accordion plugin | |
* | |
* @author XY (123 AG) | |
*/ | |
;(function(window, document, $) { | |
/** | |
* Plugin name | |
* | |
* Used for $.fn namespace ($.fn[PLUGIN_NAME]) and options data attribute (data-PLUGIN_NAME-options) | |
*/ | |
var PLUGIN_NAME = 'accordion'; | |
/** | |
* Plugin constructor | |
* | |
* @param {jQuery object} $element | |
* @param {Object} options | |
*/ | |
Plugin = function($element, options) { | |
this.$element = $element; | |
this.options = options; | |
this.defaults = { | |
}; | |
// … | |
}; | |
Plugin.prototype = { | |
/**-------------------------------------------------------------- | |
* Pseudo-public methods | |
* -------------------------------------------------------------- */ | |
init: function() { | |
var metadata = this.$element.data(PLUGIN_NAME +'-options'); | |
this.settings = $.extend(true, this.defaults, this.options, metadata); | |
// … | |
}, | |
enable: function() {}, | |
disable: function() {}, | |
/** | |
* Open specific item | |
* | |
* @param {Number} index Target element index | |
*/ | |
open: function(index) {}, | |
/** | |
* Close specific item | |
* | |
* @param {Number} index Target element index | |
*/ | |
close: function(index) {}, | |
/**-------------------------------------------------------------- | |
* Pseudo-private methods | |
* -------------------------------------------------------------- */ | |
/** | |
* Some helper methods | |
* | |
* @params {Number} param | |
* @returns {Number} | |
*/ | |
_someHelperMethod: function(param) { | |
return param * 100; | |
} | |
}; | |
$.fn[PLUGIN_NAME] = function(options) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
return this.each(function() { | |
var $this = $(this), | |
instance = $this.data(PLUGIN_NAME); | |
if (instance && typeof options === 'string' && instance[options] !== undefined) { | |
// Apply method | |
instance[options].apply(instance, args); | |
} else { | |
// Init new instance | |
instance = new Plugin($this, options); | |
instance.init(); | |
// Save instance | |
$this.data(PLUGIN_NAME, instance); | |
} | |
}); | |
}; | |
})(window, document, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment