From 77ca7970e26d9974581f78c47804d382034a4f1e Mon Sep 17 00:00:00 2001 From: Eric Kelly Date: Thu, 25 Feb 2016 20:55:46 -0500 Subject: [PATCH] Allow value equality checker to be overridden --- README.md | 14 ++++++++++++++ addon/mixin.js | 30 ++++++++++++++++++++++++++++-- tests/unit/mixin-test.js | 21 +++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 01f48f5..f688b82 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,20 @@ var buffer = ObjectProxy.extend(BufferedMixin).create({ // same as above ``` +You can also customize the function that is used to determine equality between +values: + +```js +const CustomBufferedProxy = BufferedProxy.extend({ + isEqual(a, b, key) { + if (key === 'foo') { + return customComparisonFn(a, b); + } else { + return this._super(...arguments); + } + } +}; +``` ## development diff --git a/addon/mixin.js b/addon/mixin.js index 532f664..bc71837 100644 --- a/addon/mixin.js +++ b/addon/mixin.js @@ -67,13 +67,13 @@ export default Ember.Mixin.create({ previous = hasOwnProp.call(buffer, key) ? buffer[key] : current; - if (previous === value) { + if (this.isEqual(previous, value, key)) { return; } this.propertyWillChange(key); - if (current === value) { + if (this.isEqual(current, value, key)) { delete buffer[key]; if (empty(buffer)) { set(this, 'hasBufferedChanges', false); @@ -141,5 +141,31 @@ export default Ember.Mixin.create({ } return false; + }, + + /** + Determines if two values are equal. + This is used to determine if a value being set is equal to the buffered + value. This is also used to determine if the value being set is equal to the value + in the underlying content. + + This can be overridden if you wish to do a custom value comparison: + + ```js + const CustomBufferedProxy = BufferedProxy.extend({ + isEqual(a, b, key) { + if (key === 'foo') { + return customComparisonFn(a, b); + } else { + return this._super(...arguments); + } + } + }; + ``` + + @return {Boolean} + */ + isEqual(a, b) { + return a === b; } }); diff --git a/tests/unit/mixin-test.js b/tests/unit/mixin-test.js index a198e13..6c59ce5 100644 --- a/tests/unit/mixin-test.js +++ b/tests/unit/mixin-test.js @@ -211,3 +211,24 @@ test('that .hasChanged() works', (assert) => { assert.equal(proxy.hasChanged(), false, 'Not passing a key returns false'); assert.equal(proxy.hasChanged('baz'), false, 'If the key does not exist on the proxy then return false'); }); + +test('that a custom isEqual works', (assert) => { + const BufferedProxy = Ember.ObjectProxy.extend(Mixin, { + isEqual(a, b, key) { + if (key === 'fooArray') { + return a.every(item => b.indexOf(item) > -1); + } else { + return this._super(...arguments); + } + } + }); + + const content = { fooArray: [1, 2, 3], barArray: [1, 2, 3] }; + const proxy = BufferedProxy.create({ content }); + + set(proxy, 'fooArray', [1, 2, 3]); + set(proxy, 'barArray', [1, 2, 3]); + + assert.equal(proxy.hasChanged('fooArray'), false, 'custom equality checker works with arrays'); + assert.equal(proxy.hasChanged('barArray'), true, 'default equality check does not work with arrays'); +});