如何使用knockout JS创建手风琴?

时间:2013-06-10 14:30:54

标签: knockout.js jquery-ui-accordion

添加以下自定义绑定处理程序似乎打破了手风琴,这是由于淘汰赛的版本?

ko.bindingHandlers.accordion = {
init: function(element, valueAccessor) {
    var options = valueAccessor() || {};
    setTimeout(function() {
        $(element).accordion(options);
    }, 0);

    //handle disposal (if KO removes by the template binding)
      ko.utils.domNodeDisposal.addDisposeCallback(element, function(){
          $(element).accordion("destroy");
      });
},
update: function(element, valueAccessor) {
    var options = valueAccessor() || {};
    $(element).accordion("destroy").accordion(options);
}

}

1 个答案:

答案 0 :(得分:1)

更新到knockout 2.2后,手风琴自定义绑定处理程序需要引用updateOn,因此不会为手风琴上的每个事件调用过多的绑定。

   <ul data-bind="foreach: items, accordion:{updateOn: items}"></ul>

   ko.bindingHandlers.accordion = {
    init: function(element, valueAccessor) {
        var options = valueAccessor() || {};
        setTimeout(function() {
            $(element).accordion(options);
        }, 0);

        //handle disposal (if KO removes by the template binding)
          ko.utils.domNodeDisposal.addDisposeCallback(element, function(){
              $(element).accordion("destroy");
          });
    },
    update: function(element, valueAccessor) {
        var options = valueAccessor() || {};
        if (options.updateOn && ko.isObservable(options.updateOn)) {
            options.updateOn();
        }
        $(element).accordion("destroy").accordion(options);
    }
}