使用 $.widget 工厂覆盖 jQuery UI 自动完成选项
我决定编写一个自定义小部件来扩展和覆盖标准自动完成小部件。
$.widget工厂的官方文档提到了继承,但它没有看起来不可能覆盖原始的小部件选项(即不仅仅是添加到它们)。
例如,我无法使用以下代码重新定义 source 选项(默认为 null):
$.widget("ui.productSearch", $.ui.autocomplete, {
bla: 15,
source: function( request, response ) {
response($.ui.autocomplete
.filter(["a", "b", "c"],request.term));
},
minLength: 2
});
我尝试了上面的代码 在 JS Bin 上。如果您检查生成的小部件,您可以看到 bla 已添加到选项中,但 source 和 minLength 仍然反映在$.ui.自动完成。 source 仍然为 null,因此会引发错误:this.source 不是函数。
我做错了什么?如果这是 $.widget 的预期行为,那么覆盖选项的最佳方法是什么?
I decided to write a custom widget that extends and overrides the standard Autocomplete widget.
The official documentation for the $.widget factory mentions inheritance, but it doesn't look like it's possibile to override the original widget options (i.e. not just add to them).
For example, I wasn't able to redefine the source option (that defaults to null) with this code:
$.widget("ui.productSearch", $.ui.autocomplete, {
bla: 15,
source: function( request, response ) {
response($.ui.autocomplete
.filter(["a", "b", "c"],request.term));
},
minLength: 2
});
I tried out the above code on JS Bin. If you inspect the resulting widget you can see that bla is added to the options, but both source and minLength still reflect the default values defined in $.ui.autocomplete. source is still null, and thus an error is raised: this.source is not a function.
What am I doing wrong? And if this is the expected behavior of $.widget, what is the best way to override an option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,在我看来,您只是在新小部件中定义了一个函数“源”;没关系,但您还没有覆盖该选项。如果你尝试过怎么办
?
Well, looks to me like you're just defining a function 'source' in the new widget; that's fine, but you haven't overridden the option. What if you tried
?