Skip to content

Commit c1e71b8

Browse files
committed
Allow value equality checker to be overridden
1 parent 3fcaa05 commit c1e71b8

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ var buffer = ObjectProxy.extend(BufferedMixin).create({
9797
// same as above
9898
```
9999

100+
You can also customize the function that is used to determine equality between
101+
values:
102+
103+
```js
104+
const CustomBufferedProxy = BufferedProxy.extend({
105+
isEqual(a, b, key) {
106+
if (key === 'foo') {
107+
return customComparisonFn(a, b);
108+
} else {
109+
return this._super(...arguments);
110+
}
111+
}
112+
};
113+
```
100114
101115
## development
102116

addon/mixin.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ export default Ember.Mixin.create({
6767

6868
previous = hasOwnProp.call(buffer, key) ? buffer[key] : current;
6969

70-
if (previous === value) {
70+
if (this.isEqual(previous, value, key)) {
7171
return;
7272
}
7373

7474
this.propertyWillChange(key);
7575

76-
if (current === value) {
76+
if (this.isEqual(current, value, key)) {
7777
delete buffer[key];
7878
if (empty(buffer)) {
7979
set(this, 'hasBufferedChanges', false);
@@ -141,5 +141,31 @@ export default Ember.Mixin.create({
141141
}
142142

143143
return false;
144+
},
145+
146+
/**
147+
Determines if two values are equal.
148+
This is used to determine if a value being set is equal to the buffered
149+
value. This is also used to determine if the value being set is equal to the value
150+
in the underlying content.
151+
152+
This can be overriden if you wish to do a custom value comparison:
153+
154+
```js
155+
const CustomBufferedProxy = BufferedProxy.extend({
156+
isEqual(a, b, key) {
157+
if (key === 'foo') {
158+
return customComparisonFn(a, b);
159+
} else {
160+
return this._super(...arguments);
161+
}
162+
}
163+
};
164+
```
165+
166+
@return {Boolean}
167+
*/
168+
isEqual(a, b) {
169+
return a === b;
144170
}
145171
});

tests/unit/mixin-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,24 @@ test('that .hasChanged() works', (assert) => {
211211
assert.equal(proxy.hasChanged(), false, 'Not passing a key returns false');
212212
assert.equal(proxy.hasChanged('baz'), false, 'If the key does not exist on the proxy then return false');
213213
});
214+
215+
test('that a custom isEqual works', (assert) => {
216+
const BufferedProxy = Ember.ObjectProxy.extend(Mixin, {
217+
isEqual(a, b, key) {
218+
if (key === 'fooArray') {
219+
return a.every(item => b.includes(item));
220+
} else {
221+
return this._super(...arguments);
222+
}
223+
}
224+
});
225+
226+
const content = { fooArray: [1, 2, 3], barArray: [1, 2, 3] };
227+
const proxy = BufferedProxy.create({ content });
228+
229+
set(proxy, 'fooArray', [1, 2, 3]);
230+
set(proxy, 'barArray', [1, 2, 3]);
231+
232+
assert.equal(proxy.hasChanged('fooArray'), false, 'custom equality checker works with arrays');
233+
assert.equal(proxy.hasChanged('barArray'), true, 'default equality check does not work with arrays');
234+
});

0 commit comments

Comments
 (0)