That happens because isSelected()
method accesses selectedId
property (which is observable
). Consider this:
HTML
<!-- adding into the foreach: block -->
<span data-bind="text: $root.someFunc()"></span>
<span data-bind="text: $root.someOtherFunc()"></span>
JS
// adding to a model definition
self.someFunc = function() {
self.selectedId();
console.log('I AM AN OBSERVER');
return 'oi: ' + Math.random();
};
self.someOtherFunc = function() {
// self.selectedId();
console.log('AND I AM NOT');
return 'no: ' + Math.random();
}
As you see, the only difference between those functions is that the first one does check the value of a model property defined as ko.observable
. So each time self.selectedId
is changed, this function is notified about it (which effectively means its rerun).
Note that if you drop the corresponding data-bind
part, this method won't be run in the view initialization phase, thus failing to register as a proper observer.
The second method, though invoked in the initialization phase too, doesn't attempt to check selectedId
value - so it's not registered as this value's observer, and it's not invoked subsequently.