In my ViewModel I have:
self.collaps = ko.observableArray([0,0,0,0,0,0,0]);
self.shouldShow = function(index) { return self.collaps()[index]; };
My test div:
<div data-bind="visible: shouldShow(5)">Shown!</div>
I data-bind a button with click: show :
self.show = function() {
// 1. Index 5 gets true after 2 clicks!!? But UI never updates!
self.collaps()[5] = true;
// 2. This is push-ing in true starting at index 0
self.collaps.replace(self.collaps()[5], true);
// 3. If I combine the two, I get the behavior I want, I think :)
self.collaps()[5] = true;
self.collaps.replace(self.collaps()[5], true);
};
What is happening here? What is the right way of doing this?
—-> JSFIDDLE for your pleasure! <—-
Here is a quote from the
kodocumentation:So when you are changing array item’s value
knockoutis not notified. You can usevalueHasMutatedfunction to notify subscribers manually: