knockout.js observableArray计算值

时间:2013-11-29 01:04:14

标签: javascript knockout.js ko.observablearray

情况:用户必须输入地址,该条目查询数据库并提供建议。选择现有值 - 设置现有ID。新值 - ID = 0.如果是新条目,则会显示其他选择框。

通过select2设置id,值正确更改。计算值不会(总是错误)。

<div data-bind="foreach: deliveryBuildings">
    <div>
        <input type="hidden" data-bind="value: buildingId, text: 'buildingName', select2: { minimumInputLength: 0, ajax: buildingAjax }" style="width: 200px; padding-top: 5px" />
        <div data-bind="visible: newBuilding">
             <input type="hidden" data-bind="value: regionId, select2: { minimumInputLength: 0, ajax: regionAjax }" style="width: 200px; padding-top: 5px" />
        </div>
    </div>
</div>


var DeliveryBuildingItem = function () {
    var self = this;
    self.buildingId = ko.observable();
    self.newBuilding = ko.computed(function () {
        return self.buildingId == '0' ? true : false;
    }, self);
}

var ViewModel = function () {
    var self = this;
    this.deliveryBuildings = ko.observableArray([new DeliveryBuildingItem()]);
}

1 个答案:

答案 0 :(得分:1)

在计算

中创建self.buildingid()而不是self.buildingid