Skip to content

Commit e5ff14a

Browse files
committed
Merge pull request #5 from mitchlloyd/add-shorter-method-aliases
Add shorter method aliases
2 parents fbcbd02 + 840df7a commit e5ff14a

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ buffer.discardBufferedChanges(['address']); // Discard only the address property
6666

6767
buffer.get('email'); // => example@example.com
6868
buffer.get('address'); // => 1717 rose street
69+
```
70+
71+
You can also use these shorter method names
6972

73+
```js
74+
buffer.discardChanges(); // equivalent to buffer.discardBufferedChanges()
75+
buffer.applyChanges(); // equivalent to buffer.applyBufferedChanges()
7076
```
7177

7278
Or you can grab the mixin directly

addon/mixin.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ var get = Ember.get;
55
var set = Ember.set;
66
var keys = Ember.keys;
77
var isArray = Ember.isArray;
8+
var computed = Ember.computed;
9+
10+
function aliasMethod(methodName) {
11+
return function() {
12+
return this[methodName].apply(this, arguments);
13+
};
14+
}
815

916
function empty(obj) {
1017
var key;
@@ -21,6 +28,8 @@ export default Ember.Mixin.create({
2128
this.hasBufferedChanges = false;
2229
},
2330

31+
hasChanges: computed.readOnly('hasBufferedChanges'),
32+
2433
initializeBuffer: function(onlyTheseKeys) {
2534
if(isArray(onlyTheseKeys) && !empty(onlyTheseKeys)) {
2635
onlyTheseKeys.forEach(function(key) {
@@ -93,6 +102,8 @@ export default Ember.Mixin.create({
93102
}
94103
},
95104

105+
applyChanges: aliasMethod('applyBufferedChanges'),
106+
96107
discardBufferedChanges: function(onlyTheseKeys) {
97108
var buffer = this.buffer;
98109

@@ -110,5 +121,7 @@ export default Ember.Mixin.create({
110121
if (empty(this.buffer)) {
111122
this.set('hasBufferedChanges', false);
112123
}
113-
}
124+
},
125+
126+
discardChanges: aliasMethod('discardBufferedChanges'),
114127
});

tests/unit/mixin-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,23 @@ test("that apply/discard only these keys works", function() {
156156
equal(proxy.get('baz'), 1);
157157
equal(content.baz, 1);
158158
});
159+
160+
test("aliased methods work", function() {
161+
var BufferedProxy = Ember.ObjectProxy.extend(Mixin);
162+
163+
var proxy = BufferedProxy.create({
164+
content: { property: 1 }
165+
});
166+
167+
proxy.set('property', 2);
168+
ok(proxy.get('hasChanges'), "Modified proxy has changes");
169+
170+
proxy.applyChanges();
171+
equal(proxy.get('content.property'), 2, "Applying changes sets the content's property");
172+
ok(!(proxy.get('hasChanges')), "Proxy has no changes after changes are applied");
173+
174+
proxy.set('baz', 3);
175+
proxy.discardChanges();
176+
equal(proxy.get('property'), 2, "Discarding changes resets the proxy's property");
177+
ok(!(proxy.get('hasChanges')), "Proxy has no changes after changes are discarded");
178+
});

0 commit comments

Comments
 (0)