lag
lag

Reputation: 89

collection item is not refreshed in knockout.js

I have a problem with updating elements in collection.

When my form have a collection and same filed is bound to "input box" and "span" modifications of input box aren't propagated to span element.

My form looks like this:

<tbody data-bind='foreach: gifts'>
    <tr>
        <td>
            <input class='required' data-bind='value: name, uniqueName: true' />
            <span data-bind='text: name' >&nbsp;</span>
        </td>
    </tr>
</tbody>

and you can try it on jsFiddle

Upvotes: 0

Views: 59

Answers (1)

G. Stoynev
G. Stoynev

Reputation: 7783

Your model's name and price need to be observables.

So instead of yours:

var viewModel = new GiftModel([
    { name: "Tall Hat", price: "39.95"},
    { name: "Long Cloak", price: "120.00"}
]);

Do this:

var viewModel = new GiftModel([
    new Item("Tall Hat", "39.95"),
    new Item("Long Cloak", "120.00") ]);

where Items is as follows:

var Item = function(pName, pPrice) {
    var self = this;
    self.name = ko.observable(pName);
    self.price = ko.observable(pPrice);
};

Here's a working example: http://jsfiddle.net/D2agq/

Upvotes: 1

Related Questions