Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery 插件模板
/**
* @author zhixin wen <wenzhixin2010@gmail.com>
* @version 0.0.1
* @github https://github.com/wenzhixin/
* @blog http://wenzhixin.net.cn
*/
(function($) {
'use strict';
function Plugin($el) {
this.$el = $el;
}
Plugin.prototype = {
constructor: Plugin,
events: function() {
},
init: function(options) {
// init views
// init datas
// init events
this.events();
},
/** public function **/
func: function() {
}
};
$.fn.plugin = function() {
var option = arguments[0],
args = arguments,
value,
allowedMethods = ['func']; // public function
this.each(function() {
var $this = $(this),
data = $this.data('plugin'),
options = $.extend({}, $.fn.plugin.defaults, typeof option === 'object' && option);
if (typeof option === 'string') {
if ($.inArray(option, allowedMethods) < 0) {
throw 'Unknown method: ' + option;
}
value = data[option](args[1]);
} else {
if (!data) {
data = new Plugin($this);
data.init(options, true);
$this.data('plugin', data);
} else {
data.init(options);
}
}
});
return typeof value !== 'undefined' ? value : this;
};
$.fn.plugin.defaults = {
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.