This is because the isSelected() method accesses the selectedId ( observable ) property. Consider this:
HTML
<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(); }
Fiddle
As you can see, the only difference between these functions is that the first checks the value of the model property defined as ko.observable . Therefore, every time self.selectedId changes, this function is notified about this (which effectively means repeating it).
Please note that if you drop the corresponding part of the data-bind , this method will not be launched in the initialization phase of the view, so it will not be able to register as the correct observer.
The second method, although called during the initialization phase, also does not try to check the selectedId value, so it is not registered as this value observer and is not called later.
source share