最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knockout accordion bindings break - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Mar 28, 2013 at 4:19 Gaurav 8,49714 gold badges57 silver badges91 bronze badges asked Mar 28, 2013 at 2:10 Benjamin AllisonBenjamin Allison 2,1543 gold badges31 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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 calling destroy 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 jsbin.

发布评论

评论列表(0)

  1. 暂无评论