The knockout website has a great example of adding and removing list items using a foreach binding, but how would you update these values? I've tried doing so in the following fiddle, but the DOM elements do not respond to my update: http://jsfiddle.net/SC5Lw/1/
<ul data-bind="foreach: people">
<li>
Name at position <span data-bind="text: $index"> </span>:
<span data-bind="text: name"> </span>
<a href="#" data-bind="click: $parent.removePerson">Remove</a>
<a href="#" data-bind="click: $parent.updatePerson">Update</a>
</li>
</ul>
var person1 = ko.observable({ name: 'Bert' });
var person2 = ko.observable({ name: 'Charles' });
var person3 = ko.observable({ name: 'Denise' });
self.people = ko.observableArray([person1, person2, person3]);
...
self.updatePerson = function(person) {
for(var i = 0 ; i < self.people.length ; i++)
{
if(self.people[i].name === person.name)
{
self.people[i].name = 'Bob';
}
}
};
I've made the elements of the observableArray into observables themselves, so I'm not sure why the elements they are bound to don't react to changing the property in the view model.