Skip to content

Commit 24c9928

Browse files
thoovlukemelia
authored andcommitted
Add .hasChanged(key) method (#29)
Add .hasChanged(key) method Find if an individual key has changed or return false.
1 parent 06c5b9b commit 24c9928

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ buffer.get('hasChanges'); // => false
3737
buffer.set('firstName', 'Luke');
3838
buffer.get('firstName'); // => 'Luke'
3939
buffer.get('content.firstName'); // => 'Kris'
40+
buffer.hasChanged('firstName'); // => true
4041

4142
buffer.discardBufferedChanges();
4243

4344
buffer.get('firstName'); // => 'Kris'
4445
buffer.get('content.firstName'); // => 'Kris'
46+
buffer.hasChanged('firstName'); // => false
4547

4648
// Below demonstrates that applyBufferedChanges and discardBufferedChanges
4749
// can take an optional array of keys.

addon/mixin.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,23 @@ export default Ember.Mixin.create({
123123
if (empty(get(this, 'buffer'))) {
124124
set(this, 'hasBufferedChanges', false);
125125
}
126+
},
127+
128+
/*
129+
* Determines if a given key has changed else returns false. Allows individual key lookups where
130+
* as hasBufferedChanged only looks at the whole buffer.
131+
*/
132+
hasChanged(key) {
133+
const { buffer, content } = getProperties(this, ['buffer', 'content']);
134+
135+
if (typeof key !== 'string' || typeof get(buffer, key) === 'undefined') {
136+
return false;
137+
}
138+
139+
if (get(buffer, key) !== get(content, key)) {
140+
return true;
141+
}
142+
143+
return false;
126144
}
127145
});

tests/unit/mixin-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,37 @@ test('allows passing other variables at .create time', (assert) => {
177177
assert.equal(proxy.get('container'), fakeContainer, 'Proxy didn\'t allow defining container property at create time');
178178
assert.equal(proxy.get('foo'), 'foo', 'Proxy didn\'t allow setting an arbitrary value at create time');
179179
});
180+
181+
test('that .hasChanged() works', (assert) => {
182+
const BufferedProxy = Ember.ObjectProxy.extend(Mixin);
183+
const content = {};
184+
185+
const proxy = BufferedProxy.create({ content });
186+
187+
set(proxy, 'foo', 1);
188+
189+
assert.equal(proxy.hasChanged('foo'), true);
190+
assert.equal(proxy.hasChanged('bar'), false);
191+
192+
set(proxy, 'bar', 1);
193+
194+
assert.equal(proxy.hasChanged('foo'), true);
195+
assert.equal(proxy.hasChanged('bar'), true);
196+
197+
proxy.applyBufferedChanges(['bar']);
198+
199+
set(proxy, 'foobar', false);
200+
201+
assert.equal(proxy.hasChanged('foo'), true);
202+
assert.equal(proxy.hasChanged('bar'), false);
203+
assert.equal(proxy.hasChanged('foobar'), true);
204+
205+
proxy.applyBufferedChanges();
206+
207+
assert.equal(proxy.hasChanged('foo'), false);
208+
assert.equal(proxy.hasChanged('bar'), false);
209+
assert.equal(proxy.hasChanged('foobar'), false);
210+
211+
assert.equal(proxy.hasChanged(), false, 'Not passing a key returns false');
212+
assert.equal(proxy.hasChanged('baz'), false, 'If the key does not exist on the proxy then return false');
213+
});

0 commit comments

Comments
 (0)