Skip to content

Commit fbcbd02

Browse files
committed
Merge pull request #2 from thoov/master
[Enhancement] Adding the ability to apply or discard a subset of keys
2 parents 4272572 + bc6adfc commit fbcbd02

File tree

3 files changed

+164
-17
lines changed

3 files changed

+164
-17
lines changed

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,50 @@ buffer.get('firstName'); // => 'stefan'
2323
buffer.set('firstName', 'Kris');
2424

2525
buffer.get('firstName'); // => 'Kris'
26-
buffer.get('firstName.content'); // => 'stefan'
26+
buffer.get('content.firstName'); // => 'stefan'
2727

2828
buffer.applyBufferedChanges();
2929

3030
buffer.get('firstName'); // => 'Kris'
31-
buffer.get('firstName.content'); // => 'Kris'
31+
buffer.get('content.firstName'); // => 'Kris'
3232

3333
buffer.set('firstName', 'Luke');
3434
buffer.get('firstName'); // => 'Luke'
35-
buffer.get('firstName.content'); // => 'Kris'
35+
buffer.get('content.firstName'); // => 'Kris'
3636

3737
buffer.discardBufferedChanges();
3838

3939
buffer.get('firstName'); // => 'Kris'
40-
buffer.get('firstName.content'); // => 'Kris'
40+
buffer.get('content.firstName'); // => 'Kris'
41+
42+
// Below demonstrates that applyBufferedChanges and discardBufferedChanges
43+
// can take an optional array of keys.
44+
45+
buffer.set('email', 'example@example.com');
46+
buffer.get('email'); // => 'example@example.com'
47+
buffer.get('content.email'); // => undefined
48+
49+
buffer.set('address', '123 paradise road');
50+
buffer.get('address'); // => '123 paradise road'
51+
buffer.get('content.address'); // => undefined
52+
53+
buffer.applyBufferedChanges(['email']); // Only apply the email from the buffer
54+
55+
buffer.get('email'); // => 'example@example.com'
56+
buffer.get('address'); // => '123 paradise road'
57+
buffer.get('content.email'); // => 'example@example.com'
58+
buffer.get('content.address'); // => undefined
59+
60+
buffer.setProperties({
61+
'email', 'sample@sample.com',
62+
'address', '1717 rose street'
63+
});
64+
65+
buffer.discardBufferedChanges(['address']); // Discard only the address property from the buffer
66+
67+
buffer.get('email'); // => example@example.com
68+
buffer.get('address'); // => 1717 rose street
69+
4170
```
4271

4372
Or you can grab the mixin directly

addon/mixin.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Ember from 'ember';
33
var hasOwnProp = Object.prototype.hasOwnProperty;
44
var get = Ember.get;
55
var set = Ember.set;
6+
var keys = Ember.keys;
7+
var isArray = Ember.isArray;
68

79
function empty(obj) {
810
var key;
@@ -19,8 +21,15 @@ export default Ember.Mixin.create({
1921
this.hasBufferedChanges = false;
2022
},
2123

22-
initializeBuffer: function() {
23-
this.buffer = {};
24+
initializeBuffer: function(onlyTheseKeys) {
25+
if(isArray(onlyTheseKeys) && !empty(onlyTheseKeys)) {
26+
onlyTheseKeys.forEach(function(key) {
27+
delete this.buffer[key];
28+
}, this);
29+
}
30+
else {
31+
this.buffer = {};
32+
}
2433
},
2534

2635
unknownProperty: function(key) {
@@ -65,28 +74,41 @@ export default Ember.Mixin.create({
6574
return value;
6675
},
6776

68-
applyBufferedChanges: function() {
77+
applyBufferedChanges: function(onlyTheseKeys) {
6978
var buffer = this.buffer;
7079
var content = this.get('content');
7180

72-
Ember.keys(buffer).forEach(function(key) {
81+
keys(buffer).forEach(function(key) {
82+
if (isArray(onlyTheseKeys) && !onlyTheseKeys.contains(key)) {
83+
return;
84+
}
85+
7386
set(content, key, buffer[key]);
7487
});
7588

76-
this.initializeBuffer();
77-
this.set('hasBufferedChanges', false);
89+
this.initializeBuffer(onlyTheseKeys);
90+
91+
if (empty(this.buffer)) {
92+
this.set('hasBufferedChanges', false);
93+
}
7894
},
7995

80-
discardBufferedChanges: function() {
96+
discardBufferedChanges: function(onlyTheseKeys) {
8197
var buffer = this.buffer;
8298

83-
this.initializeBuffer();
99+
this.initializeBuffer(onlyTheseKeys);
100+
101+
keys(buffer).forEach(function(key) {
102+
if (isArray(onlyTheseKeys) && !onlyTheseKeys.contains(key)) {
103+
return;
104+
}
84105

85-
Ember.keys(buffer).forEach(function(key) {
86106
this.propertyWillChange(key);
87107
this.propertyDidChange(key);
88108
}, this);
89109

90-
this.set('hasBufferedChanges', false);
110+
if (empty(this.buffer)) {
111+
this.set('hasBufferedChanges', false);
112+
}
91113
}
92114
});

tests/unit/mixin-test.js

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import Ember from 'ember';
33

44
module("ember-buffered-proxy/mixin");
55

6-
test("exists", function() {
6+
test("exists", function() {
77
ok(Mixin);
88
});
99

1010
test("that it works", function() {
1111
var BufferedPorxy = Ember.ObjectProxy.extend(Mixin);
1212

1313
var content = {
14-
baz: 1
14+
baz: 1
1515
};
1616

1717
var proxy = BufferedPorxy.create({
18-
content: content
18+
content: content
1919
});
2020

2121
equal(proxy.get('baz'), 1);
@@ -60,3 +60,99 @@ test("that it works", function() {
6060
equal(proxy.get('baz'), 1);
6161
equal(content.baz, 1);
6262
});
63+
64+
test("that apply/discard only these keys works", function() {
65+
var BufferedPorxy = Ember.ObjectProxy.extend(Mixin);
66+
67+
var content = {
68+
baz: 1,
69+
world: 'hello'
70+
};
71+
72+
var proxy = BufferedPorxy.create({
73+
content: content
74+
});
75+
76+
equal(proxy.get('baz'), 1);
77+
equal(content.baz, 1);
78+
equal(proxy.get('world'), 'hello');
79+
equal(content.world, 'hello');
80+
81+
ok(!('foo' in content));
82+
equal(proxy.get('hasBufferedChanges'), false);
83+
84+
proxy.set('foo', 1);
85+
86+
equal(proxy.get('foo'), 1);
87+
ok(!('foo' in content));
88+
equal(proxy.get('hasBufferedChanges'), true);
89+
90+
proxy.set('testing', '1234');
91+
92+
equal(proxy.get('testing'), '1234');
93+
ok(!('testing' in content));
94+
equal(proxy.get('hasBufferedChanges'), true);
95+
96+
proxy.applyBufferedChanges(['foo']);
97+
98+
equal(proxy.get('foo'), 1);
99+
ok('foo' in content);
100+
ok(!('testing' in content));
101+
equal(proxy.get('hasBufferedChanges'), true);
102+
equal(content.foo, 1);
103+
equal(proxy.get('testing'), '1234');
104+
105+
proxy.applyBufferedChanges(['testing']);
106+
107+
equal(proxy.get('testing'), '1234');
108+
ok('testing' in content);
109+
equal(proxy.get('hasBufferedChanges'), false);
110+
equal(content.testing, '1234');
111+
112+
// Testing discardBufferdChanges with onlyTheseKeys
113+
114+
proxy.setProperties({
115+
bar: 2,
116+
example: 123
117+
});
118+
119+
equal(proxy.get('foo'), 1);
120+
equal(proxy.get('bar'), 2);
121+
equal(proxy.get('example'), 123);
122+
123+
ok('foo' in content);
124+
ok('testing' in content);
125+
ok(!('bar' in content));
126+
ok(!('example' in content));
127+
128+
equal(proxy.get('hasBufferedChanges'), true);
129+
130+
proxy.discardBufferedChanges(['bar']);
131+
132+
equal(proxy.get('foo'), 1);
133+
equal(proxy.get('testing'), '1234');
134+
equal(proxy.get('bar'), undefined);
135+
equal(proxy.get('example'), 123);
136+
137+
ok('foo' in content);
138+
ok('testing' in content);
139+
ok(!('bar' in content));
140+
ok(!('example' in content));
141+
equal(proxy.get('hasBufferedChanges'), true);
142+
143+
proxy.discardBufferedChanges(['example']);
144+
145+
equal(proxy.get('foo'), 1);
146+
equal(proxy.get('testing'), '1234');
147+
equal(proxy.get('bar'), undefined);
148+
equal(proxy.get('example'), undefined);
149+
150+
ok('foo' in content);
151+
ok('testing' in content);
152+
ok(!('bar' in content));
153+
ok(!('example' in content));
154+
equal(proxy.get('hasBufferedChanges'), false);
155+
156+
equal(proxy.get('baz'), 1);
157+
equal(content.baz, 1);
158+
});

0 commit comments

Comments
 (0)