Last active
December 18, 2015 00:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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