诀窍是,在Widget工厂完成创建Widget之后,它基本上只是一个普通的插件。这意味着你可以进一步扩展它。
想法是保存原始函数,用自定义函数替换它,并使其通过选择器。此代码可以位于Widget工厂之后,第一次使用对象之前的任何位置。请在拨打$.widget()
之后将它放入您的插件文件中。
该示例中的小部件的名称是test
。
// Save the original factory function in a variable
var testFactory = $.fn.test;
// Replace the function with our own implementation
$.fn.test = function(obj){
// Add the selector property to the options
$.extend(obj, { selector: this.selector });
// Run the original factory function with proper context and arguments
return testFactory.apply(this,arguments);
};
这确保this.selector
作为命名选项通过。现在您可以通过参考this.options.selector
来访问您工厂的任何地方。
作为补充,我们不需要破解任何jQuery UI代码。
太棒了。从来没有想过这样做。我需要玩弄我正在做的事情的论点,但看起来它会工作得很好。谢谢! – 2011-05-26 18:08:38