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

[WIP] add DidChangeAttrs mixin #5

Closed
Closed
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
54 changes: 53 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import WeakMap from 'ember-weakmap';

import Ember from 'ember';

function isEqual(key, a, b) {
return a === b;
Expand Down Expand Up @@ -49,3 +49,55 @@ export default function(keys, hook) {
hook.apply(this, [(isFirstCall ? null : changedAttrs), ...arguments]);
};
}

const DidChangeAttrs = Ember.Mixin.create({
_didChangeAttrsWeakMap: null, //this tracks previous state of any `trackAttrChanges`
didChangeAttrsConfig: [], //attributes to track

didReceiveAttrs() {
this._super(...arguments);

let weakMap = this.get('_didChangeAttrsWeakMap');

if (weakMap === null) { //first run
let config = this.get('didChangeAttrsConfig');
let trackedAttrs = config.attrs;
let initialValues = {};

for (let i=0; i<trackedAttrs.length; i++) {
let key = trackedAttrs[i];
initialValues[key] = this.get(key);
}

weakMap = new WeakMap();
weakMap.set(this, initialValues);
this.set('_didChangeAttrsWeakMap', weakMap);
}
},

didUpdateAttrs() {
this._super(...arguments);

let config = this.get('didChangeAttrsConfig');
let trackedAttrs = config.attrs;
let oldValues = this.get('_didChangeAttrsWeakMap').get(this);
let changes = {};

for (let i=0; i<trackedAttrs.length; i++) {
let key = trackedAttrs[i];
let current = this.get(key);
let previous = oldValues[key];

if (!isEqual(key, previous, current)) { //TODO: configurable equality fn
changes[key] = { previous, current };
oldValues[key] = current;
}
}

if(Object.keys(changes).length > 0) {
this.didChangeAttrs(changes);
}
},
});

export { DidChangeAttrs };
49 changes: 49 additions & 0 deletions tests/integration/did-change-attrs-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Ember from 'ember';
import { test, moduleForComponent } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { DidChangeAttrs } from 'ember-diff-attrs';

function registerComponent(testSuite, hash, klass = Ember.Component) {
testSuite.register('component:x-changer', klass.extend(DidChangeAttrs, hash));
}

moduleForComponent('x-changer', 'Integration | DidChangeAttrs', {
integration: true
});

test('Basic usage', function(assert) {
let changedAttrs, didChangeAttrsCallCount = 0;

registerComponent(this, {
didChangeAttrsConfig: {
attrs: ['email', 'isAdmin']
},

didChangeAttrs(changes) {
this._super(...arguments);

didChangeAttrsCallCount++;
changedAttrs = changes;
}
});

this.set('name', 'Tomster');
this.set('email', '[email protected]');
this.set('isAdmin', false);

this.render(hbs`{{x-changer email=email isAdmin=isAdmin name=name}}`);
assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called on initial render');

this.set('email', '[email protected]');
assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is called when an attribute changed');

assert.deepEqual(changedAttrs, {
email: {
previous: '[email protected]',
current: '[email protected]'
}
}, '`changedAttrs` contains the attribute changes');

this.set('name', 'TheTomster');
assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is not called when an untracked attribute is changed');
});
Loading