Skip to content

Instantly share code, notes, and snippets.

@Vintharas
Last active December 18, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vintharas/5694526 to your computer and use it in GitHub Desktop.
Save Vintharas/5694526 to your computer and use it in GitHub Desktop.
By default knockout notifies subscribers of a change when an object value is written, even if it is identical to the old one
// spec/observableBehaviors.js
it('Should notify subscribers of a change when an object value is written, even if it is identical to the old value', function() {
// Because we can't tell whether something further down the object graph has changed, we regard
// all objects as new values. To override this, set an "equalityComparer" callback
var constantObject = {};
var instance = new ko.observable(constantObject);
var notifiedValues = [];
instance.subscribe(notifiedValues.push, notifiedValues);
instance(constantObject);
expect(notifiedValues).toEqual([constantObject]);
});
// src/subscribables/observable.js
ko.observable['fn'] = {
"equalityComparer": function valuesArePrimitiveAndEqual(a, b) {
var oldValueIsPrimitive = (a === null) || (typeof(a) in primitiveTypes);
return oldValueIsPrimitive ? (a === b) : false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment