【发布时间】:2013-05-06 14:13:29
【问题描述】:
我已经阅读了几篇关于最佳 jquery 插件模式的文章,包括官方文档 -
http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
https://github.com/jquery-boilerplate/patterns/
http://docs.jquery.com/Plugins/Authoring
但仍然无法确定最适合我的需求,也许有人会建议。
我现在需要从内部函数创建一个公共方法。
目前我正在使用基本的插件结构:
(function ($) {
$.fn.plugin = function (options) {
var defaults = {
someoption: true
};
options = $.extend(defaults, options);
return this.each(function () {
// all logic goes here...
if (options.someoption) {
mainMethod();
}
function mainMethod () {
// main method bla bla..
// also calls in certain circumstances this func:
somePublicMethod();
}
function somePublicMethod () {
// AND this method should be accessible like:
// $('elem').plugin('somePublicMethod');
}
});
};
})(jQuery);
这个somePublicMethod() func 应该可以像$('elem').plugin('somePublicMethod'); 一样访问
你有什么想法吗?
【问题讨论】:
-
你需要插件的意图是你首先需要找出的。显然有很多插件可以用于各种操作
-
我说的是模式:)
-
http://jqueryboilerplate.com/这个插件对于初学者来说是最好的,其中`docs.jquery.com/Plugins/Authoring`可以参考。 -
@dreamweiver 这很好,我已经看过了,但这不是关于添加方法和选项
标签: javascript jquery design-patterns plugins