user3047656
user3047656

Reputation: 3

knockout.js observableArray computed value

Situation: Address must be entered by user, the entry queries the database and provides suggestions. Selects existing value - sets existing ID. New value - ID = 0. If it's a new entry, additional selection box is shown.

The id is set via select2, the value changes correctly. Computed value does not (always false).

<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()]);
}

Upvotes: 0

Views: 1280

Answers (1)

PW Kad
PW Kad

Reputation: 14995

Make it self.buildingid() instead of self.buildingid inside the computed

Upvotes: 1

Related Questions