Skip to content

Instantly share code, notes, and snippets.

@wenzhixin
Last active September 18, 2017 07:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wenzhixin/7634953 to your computer and use it in GitHub Desktop.
Save wenzhixin/7634953 to your computer and use it in GitHub Desktop.
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