HTML
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr><th width="483"><span>Product Name</span></th></tr>
</thead>
<tbody data-bind='foreach: basket.model.products()'>
<tr><td><div class="ml10 productDetails" data-bind="text: name"></div></td></tr>
</tbody>
</table>
JAVASCRIPT CODE
var ProductLine = function(data) {
var self = this;
self.name = ko.observable(data.name);
};
function BasketModel(data) {
var self = this;
self.initialData = ko.observable(data);
self.products = ko.observableArray();
$.each(self.initialData().products,function(i,val){
self.products.push(new ProductLine(this));
});
}
function renderBasket(data){
basket = { model: new BasketModel(data)};
ko.applyBindings(basket.model);
}
$(document).ready(function(){
var sampleData = [{"name":"product 1"},{"name":"product 2"}];
renderBasket(sampleData );
});
When i add a new product to basket within ajax post , i call the function below with response data
renderBasket(response.data);
Example response.data like [{"name":"product 1"},{"name":"product 2"}, {"name":"product 3"}];
At first: table rows are like :
product 1
product 2
product 3
AFTER i add a new product result is :
product 1
product 1
product 1
product 2
product 2
product 2
product 3
product 3
product 3
I've tried ko.cleanNode and basket.model.products.removeAll() before ko.applyBindings(basket.model); But i couldn't solve it.
Thank you.