I have a viewmodel with an observable array of child viewmodels.
These should be user-sortable, so I'm using the sort()
method of the observableArray
:
function ViewModel() {
this.items = ko.observableArray([/* ... some items ... */]);
this.sort = function () {
this.items.sort(function (a, b) {
// comparison
}
}
}
Each item has a type
observable, bound to a list of radio boxes via the checked
binding.
<li>
<input type="radio" value="A" data-bind="checked: type" /> A
<input type="radio" value="B" data-bind="checked: type" /> B
<input type="radio" value="C" data-bind="checked: type" /> C
<input type="radio" value="D" data-bind="checked: type" /> D
</li>
The phenomenon is that when the items
array is sorted, the radio boxes randomly lose their state, i.e. no dot is visible on the screen for a seemingly random sub-set of them - even though their value still is maintained in the viewmodel.
Try it yourself: http://jsfiddle.net/yxg53bph/ - click "Sort" a few times and observe the effect.
Simple question: What's wrong here and how do I fix it?
Calling notifySubscribers()
on the individual items after the sort fixes the display problem. However that's a hack, not a solution.