淘汰赛手风琴绑定突破 [英] Knockout accordion bindings break
问题描述
下面结合工作之前1.9:
ko.bindingHandlers.accordion = {
初始化:功能(元素,valueAccessor){
VAR的选择= valueAccessor()|| {};
的setTimeout(函数(){
$(元素).accordion(选件);
},0);
ko.utils.domNodeDisposal.addDisposeCallback(元素,函数(){
$(元素).accordion(消灭);
});
},
更新:功能(元素,valueAccessor){
VAR的选择= valueAccessor()|| {};
$(元素).accordion(消灭)手风琴(期权)。
}
}
但自1.9,它不再起作用,并且给出了以下错误:
未捕获的错误:不能调用初始化之前手风琴方法;试图调用方法'破坏'
我无法弄清楚为什么。我查了一下jQuery UI的升级说明,但似乎没有什么意义。
是什么造成这一点,那我绑定需要改变?
未捕获的错误:无法之前的初始化呼吁手风琴的方法;试图调用方法'破坏'
块引用>此错误说,你是叫
初始化之前小部件销毁折叠菜单的
方法。问题是,你使用
的setTimeout
自定义绑定code。里面的setTimeout的code你的更新功能后运行。因此,手风琴插件是不是在你的元素,并在您致电您的更新功能初始化摧毁
手风琴的方法。有一个简单的方法是,你应该检查手风琴插件是否被初始化过的元素或不调用任何方法之前,如:
如果(typeof运算$(元素)。数据(UI-手风琴)!=未定义){
$(元素).accordion(消灭)手风琴(期权)。
}在这里,您可以检查 工作拨弄 。
The following binding worked prior to 1.9:
ko.bindingHandlers.accordion = { init: function(element, valueAccessor) { var options = valueAccessor() || {}; setTimeout(function() { $(element).accordion(options); }, 0); ko.utils.domNodeDisposal.addDisposeCallback(element, function(){ $(element).accordion("destroy"); }); }, update: function(element, valueAccessor) { var options = valueAccessor() || {}; $(element).accordion("destroy").accordion(options); } }
But since 1.9, it no longer works, and the following error is given:
Uncaught Error: cannot call methods on accordion prior to initialization; attempted to call method 'destroy'
I'm having trouble figuring out why. I looked over the jQuery UI upgrade notes, but nothing seemed relevant.
What's causing this, and what about my binding needs to change?
解决方案Uncaught Error: cannot call methods on accordion prior to initialization; attempted to call method 'destroy'
This error says that you are calling
destroy
method of accordion widget before initializing the widget.The issue is with your custom binding code where you use
setTimeOut
. The code inside setTimeOut runs after your update function. So the accordion plugin is not initialized over your element and in your update function you are callingdestroy
method of the accordion.A simple alternative is you should check whether accordion plugin is initialized over the element or not before calling any method, like :
if(typeof $(element).data("ui-accordion") != "undefined"){ $(element).accordion("destroy").accordion(options); }
Here you can check working fiddle.
这篇关于淘汰赛手风琴绑定突破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!