I have a simple jsfiddle where i am trying to change value of each foreach binding. If I try to change value of a row then binding updates all other rows which I don’t want. what wrong with this binding?
<div data-bind="foreach:lines">
<div>
<input data-bind="value: qty, valueUpdate: 'keyup'" />
<label data-bind="text: qty"></label>
</div>
var Product = function (qty) {
self = this;
self.qty = ko.observable(qty);
};
var Cart = function () {
self = this;
self.lines = ko.observableArray([]);
self.lines.push(new Product(1));
self.lines.push(new Product(2));
};
ko.applyBindings(new Cart());
UPDATE: I moved self.lines.push into cart model
The problem is that you’re missing
varfromself = this. It needs to bevar self = this. In your example,selfis a global variable and each object is sharing the sameselfvalue.