In my viewmodel, I have a knockoutjs ObserableArray. Just after I initialized the ViewModel, it binds the data successfully. Then, what I need to do is to sort the collection.
$.each(vm.searchResults(), function (i, property) {
console.log(property.Name());
});
vm.searchResults().sort(function (a, b) {
return a.Name().toLowerCase() > b.Name().toLowerCase() ? 1 : -1;
});
$.each(vm.searchResults(), function (i, property) {
console.log(property.Name());
});
As you can see, I output the Name of the element to the console to see the order before and after the sorting. The sorting works just fine. The problem is with the UI update. Somehow, the UI is not updated.
Then, try to remove a record from the array with the following code to see if the UI will respond to that or not:
vm.searchResults().shift();
The UI stays the the same and didn't update again. What would be the problem here?
Edit:
Here is a sample case as well: http://jsfiddle.net/tugberk/KLpwP/
The same problem here as well.
Edit:
I solved the problem as shown in this sample: http://jsfiddle.net/tugberk/KLpwP/16/ But I am still not sure why it worked as I tried at first.