Skip to content

Commit 95189a1

Browse files
committed
Adding the ability to apply or discard a subset of keys
1 parent 04e144c commit 95189a1

File tree

2 files changed

+133
-13
lines changed

2 files changed

+133
-13
lines changed

addon/mixin.js

Lines changed: 34 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,17 @@ 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) && onlyTheseKeys.length > 0) {
26+
keys(this.buffer).forEach(function(key) {
27+
if(onlyTheseKeys.contains(key)) {
28+
delete this.buffer[key];
29+
}
30+
}, this);
31+
}
32+
else {
33+
this.buffer = {};
34+
}
2435
},
2536

2637
unknownProperty: function(key) {
@@ -65,28 +76,41 @@ export default Ember.Mixin.create({
6576
return value;
6677
},
6778

68-
applyBufferedChanges: function() {
79+
applyBufferedChanges: function(onlyTheseKeys) {
6980
var buffer = this.buffer;
7081
var content = this.get('content');
7182

72-
Ember.keys(buffer).forEach(function(key) {
83+
keys(buffer).forEach(function(key) {
84+
if (isArray(onlyTheseKeys) && !onlyTheseKeys.contains(key)) {
85+
return;
86+
}
87+
7388
set(content, key, buffer[key]);
7489
});
7590

76-
this.initializeBuffer();
77-
this.set('hasBufferedChanges', false);
91+
this.initializeBuffer(onlyTheseKeys);
92+
93+
if (keys(this.buffer).length === 0) {
94+
this.set('hasBufferedChanges', false);
95+
}
7896
},
7997

80-
discardBufferedChanges: function() {
98+
discardBufferedChanges: function(onlyTheseKeys) {
8199
var buffer = this.buffer;
82100

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

85-
Ember.keys(buffer).forEach(function(key) {
86108
this.propertyWillChange(key);
87109
this.propertyDidChange(key);
88110
}, this);
89111

90-
this.set('hasBufferedChanges', false);
112+
if (keys(this.buffer).length === 0) {
113+
this.set('hasBufferedChanges', false);
114+
}
91115
}
92116
});

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 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)