我正在研究一种用于jQuery插件的全功能基础框架。我基于jQuery插件/创作示例发现here的结构。带有方法和回调的jQuery插件
这里的结构的进一步简化版本,我的建筑:
(function($){
var methods = {
init : function(options) {
var defaults = {
// place default settings for plugin here
}
var options = $.extend(defaults, options);
return this.each(function(){
var $this = $(this),
data = $this.data('PLUGINNAME');
if (! data) {
// do more setup stuff here
}
});
},
destroy : function() {
return this.each(function(){
// do stuff to destroy any function binding
})
},
update : function(content) {
return this.each(function() {
//do your update stuff here
})
}
};
$.fn.PLUGINNAME = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.PLUGINNAME');
}
};
})(jQuery);
,我试图现在要弄清楚的是如何把一个回调函数添加到该插件调用。我知道,我需要另一个参数是这样的:
$.fn.PLUGINNAME = function(method, callback) {
,但我不知道如何去实现一个基于什么我现在有。
这个回调应该做什么,它的签名是什么? – 2012-01-07 17:57:44
回调应该做什么并不重要。也许它只是一个函数,需要等到插件运行某个脚本(如getJSON调用)才能处理页面上的某个元素。 – 2012-01-07 18:28:37