Ensure to push items into the observableArray
instead of replacing them
Data Model:
function ViewModel() {
var self = this;
self.sampleArray = ko.observableArray([{
'Hours': 0.5
}])
setTimeout(function () {
alert('Simulated ajax call')
var newData = ko.mapping.fromJS(data1)();
self.sampleArray.push.apply(self.sampleArray,newData)
}, 2000);
}
ko.applyBindings(new ViewModel())
Benefits of using push.apply compared to conventional loops:
If you add items one by one to an array / collection (using array.push(item)), subscribers will be notified for each addition causing multiple UI refreshes and impacting performance.
However, by employing array.push.appy, you can add multiple items while notifying subscribers only once.
This is the advantage and utility of the array.push.apply function.
View a working example on Fiddle: here
Another Fiddle showcasing usage with utils.forEach: here