From the Knockout v2.1.0 debug source code:
ko.observable['fn'] = {
"equalityComparer": function valuesArePrimitiveAndEqual(a, b) {
var oldValueIsPrimitive = (a === null) || (typeof(a) in primitiveTypes);
return oldValueIsPrimitive ? (a === b) : false;
}
};
This seems unintuitive to me but there must be some reason Steve Sanderson went out of his way to define this. Why would this be the case? It seems to unnecessarily trigger change notifications.
This was done because if you have an observable that holds an object, Knockout does not know if sub-properties were changed or not.
At this point, we trigger a notification just in case one of the object's properties did change.
If you have an observable, which is holding an object, you could make a custom equalityComparer, to return equality based on your needs. Just set the property on the observable instance you want to customize. The signature is:
myObservable["equalityComparer"] = function(a, b){
return a===b;// Or any arbitrary comparison
};
Now your observable is only raising change-event, when function is returning false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With