Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow value equality checker to be overridden #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 28 additions & 2 deletions addon/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
});
21 changes: 21 additions & 0 deletions tests/unit/mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});