Last active
September 18, 2017 07:20
-
-
Save wenzhixin/7634953 to your computer and use it in GitHub Desktop.
jQuery 插件模板
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
/** | |
* @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