当绑定中的值更改时,Knockout计算的函数不会更新

时间:2014-03-06 19:10:29

标签: javascript knockout.js

我已经使用Knockout构建了一个级联下拉列表,但是级联部分(ko.computed)没有更新。

淘汰赛版本为3.0.0

序:

我正在使用树数据结构来建模级联。

{ id: '', text: '', childItems: [] }

理念取自:

knockout.js - nested array data and cascading pre-populated dropdown lists binding

Fiddle

HTML:

<select data-bind="options: manufacturers,
                   optionsCaption:'Manufacturer',
                   optionsText: 'text',
                   optionsValue: 'id',
                   value: selectedManufacturer">
</select>
<select data-bind="options: models,
                   optionsCaption:'Model',
                   optionsText: 'text',
                   optionsValue: 'id',
                   value: selectedModel,
                   enable: enableModels">
</select>
<select data-bind="options: engines,
                   optionsCaption:'Engine',
                   optionsText: 'text',
                   optionsValue: 'id',
                   value: selectedEngine,
                   enable: enableEngines">
</select>

JS:

视图模型:

function ViewModel(items) {
    this.manufacturers = ko.observableArray(items);
    // These three observables should be numbers (e.g. 1)
    // Corresponding to the id
    this.selectedManufacturer = ko.observable();
    this.selectedModel = ko.observable();
    this.selectedEngine = ko.observable();

    function getById(items, id) {
        return ko.utils.arrayFirst(items, function(item) {
            return item.id === id;
        });
    }

    this.models = ko.computed(function(){
        var items = ko.utils.unwrapObservable(this.manufacturers);
        var id = ko.utils.unwrapObservable(this.selectedManufacturer);
        return id ? getById(items, id).childItems : [];
    }, this);

    this.enableModels = ko.computed(function(){
        var items = ko.utils.unwrapObservable(this.manufacturers);
        var id = ko.utils.unwrapObservable(this.selectedManufacturer);
        return id ? getById(items, id).value > 0 : false;
    }, this);

    // generate engines based on models
    this.engines = ko.computed(function(){
        var items = ko.utils.unwrapObservable(this.models);
        var id = ko.utils.unwrapObservable(this.selectedModel);
        return id ? getById(items, id).childItems : [];
    }, this);

    this.enableEngines = ko.computed(function(){
        var items = ko.utils.unwrapObservable(this.models);
        var id = ko.utils.unwrapObservable(this.selectedModel);
        return id ? getById(items, id).value > 0 : false;
    }, this);
}

数据:

var items = [
    { text: 'Ford', id: 1, childItems:
     [
         { text: 'F-150', id: 1, childitems:
          [
              { text: 'Gasoline', id: 1, childitems: [] },
              { text: 'Diesel', id: 2, childitems: [] }
          ]
         },
         { text: 'F-250', id: 2, childitems:
          [
              { text: 'Gasoline', id: 3, childitems: [] },
              { text: 'Diesel', id: 4, childitems: [] }
          ]
         }
     ]
    },
    { text: 'Honda', id: 2, childItems:
     [
         { text: 'Civic', id: 5, childitems:
          [
              { text: 'Gasoline', id: 5, childitems: [] },
              { text: 'Electric', id: 6, childitems: [] }
          ]
         },
         { text: 'Accord', id: 6, childitems:
          [
              { text: 'Gasoline', id: 7, childitems: [] },
              { text: 'Electric', id: 8, childitems: [] }
          ]
         }
     ]
    }
];

结合:

var module = {};

module.viewModel = new ViewModel(items);

ko.applyBindings(module.viewModel);

更新

以下是基于答案的complete working sample

1 个答案:

答案 0 :(得分:2)

您的问题是,在“启用”计算中,您将返回找到的项的value属性。您可能想要检查childItems是否存在,然后childItems的长度是否大于0。

否则,你可以删除“enable”计算器,并且只是绑定选项的长度,如enable: models().length(如果0长度则为falsey,如果大于0则为truthy)。

这是一个更新的小提琴:http://jsfiddle.net/rniemeyer/GWVW8/1/

还有一些拼写错误(childItemschilditems)。