Skip to content

Commit de2a911

Browse files
committed
Merge pull request #6 from thoov/codeCleanup
[Enhancement] Minor cleanup / refactoring
2 parents b81d386 + 0d378a5 commit de2a911

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

addon/helpers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var hasOwnProp = Object.prototype.hasOwnProperty;
2+
3+
function aliasMethod(methodName) {
4+
return function() {
5+
return this[methodName].apply(this, arguments);
6+
};
7+
}
8+
9+
function empty(obj) {
10+
var key;
11+
for (key in obj) {
12+
if (!hasOwnProp.call(obj, key)) { continue; }
13+
return false;
14+
}
15+
return true;
16+
}
17+
18+
export { aliasMethod, empty };

addon/mixin.js

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
import Ember from 'ember';
2+
import {aliasMethod, empty} from './helpers';
23

3-
var hasOwnProp = Object.prototype.hasOwnProperty;
4-
var get = Ember.get;
5-
var set = Ember.set;
6-
var keys = Ember.keys;
7-
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-
}
15-
16-
function empty(obj) {
17-
var key;
18-
for (key in obj) {
19-
if (!hasOwnProp.call(obj, key)) { continue; }
20-
return false;
21-
}
22-
return true;
23-
}
4+
var get = Ember.get;
5+
var set = Ember.set;
6+
var keys = Ember.keys;
7+
var isArray = Ember.isArray;
8+
var computed = Ember.computed;
249

2510
export default Ember.Mixin.create({
11+
12+
hasChanges : computed.readOnly('hasBufferedChanges'),
13+
applyChanges : aliasMethod('applyBufferedChanges'),
14+
discardChanges : aliasMethod('discardBufferedChanges'),
15+
2616
init: function() {
2717
this.initializeBuffer();
2818
this.hasBufferedChanges = false;
2919
},
3020

31-
hasChanges: computed.readOnly('hasBufferedChanges'),
32-
3321
initializeBuffer: function(onlyTheseKeys) {
3422
if(isArray(onlyTheseKeys) && !empty(onlyTheseKeys)) {
3523
onlyTheseKeys.forEach(function(key) {
@@ -52,15 +40,16 @@ export default Ember.Mixin.create({
5240
},
5341

5442
setUnknownProperty: function(key, value) {
55-
var buffer = this.buffer;
43+
var buffer = this.buffer;
5644
var content = this.get('content');
5745
var current;
46+
var previous;
5847

5948
if (content != null) {
6049
current = get(content, key);
6150
}
6251

63-
var previous = buffer.hasOwnProperty(key) ? buffer[key] : current;
52+
previous = buffer.hasOwnProperty(key) ? buffer[key] : current;
6453

6554
if (previous === value) {
6655
return;
@@ -84,7 +73,7 @@ export default Ember.Mixin.create({
8473
},
8574

8675
applyBufferedChanges: function(onlyTheseKeys) {
87-
var buffer = this.buffer;
76+
var buffer = this.buffer;
8877
var content = this.get('content');
8978

9079
keys(buffer).forEach(function(key) {
@@ -102,8 +91,6 @@ export default Ember.Mixin.create({
10291
}
10392
},
10493

105-
applyChanges: aliasMethod('applyBufferedChanges'),
106-
10794
discardBufferedChanges: function(onlyTheseKeys) {
10895
var buffer = this.buffer;
10996

@@ -121,7 +108,5 @@ export default Ember.Mixin.create({
121108
if (empty(this.buffer)) {
122109
this.set('hasBufferedChanges', false);
123110
}
124-
},
125-
126-
discardChanges: aliasMethod('discardBufferedChanges'),
111+
}
127112
});

tests/unit/mixin-test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import Mixin from 'ember-buffered-proxy/mixin';
21
import Ember from 'ember';
2+
import Mixin from 'ember-buffered-proxy/mixin';
33

4-
module("ember-buffered-proxy/mixin");
4+
module('ember-buffered-proxy/mixin');
55

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

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

1313
var content = {
@@ -61,7 +61,7 @@ test("that it works", function() {
6161
equal(content.baz, 1);
6262
});
6363

64-
test("that apply/discard only these keys works", function() {
64+
test('that apply/discard only these keys works', function() {
6565
var BufferedPorxy = Ember.ObjectProxy.extend(Mixin);
6666

6767
var content = {
@@ -157,22 +157,22 @@ test("that apply/discard only these keys works", function() {
157157
equal(content.baz, 1);
158158
});
159159

160-
test("aliased methods work", function() {
160+
test('aliased methods work', function() {
161161
var BufferedProxy = Ember.ObjectProxy.extend(Mixin);
162162

163163
var proxy = BufferedProxy.create({
164164
content: { property: 1 }
165165
});
166166

167167
proxy.set('property', 2);
168-
ok(proxy.get('hasChanges'), "Modified proxy has changes");
168+
ok(proxy.get('hasChanges'), 'Modified proxy has changes');
169169

170170
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");
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');
173173

174174
proxy.set('baz', 3);
175175
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");
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');
178178
});

tests/unit/proxy-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import Ember from 'ember';
22
import BufferedProxy from 'ember-buffered-proxy/proxy';
33
import BufferedMixin from 'ember-buffered-proxy/mixin';
44

5-
module("ember-buffered-proxy/mixin");
5+
module('ember-buffered-proxy/mixin');
66

7-
test("exists", function() {
7+
test('exists', function() {
88
ok(BufferedProxy);
99
});
1010

11-
test("that appears correct", function() {
12-
ok(BufferedMixin.detect( BufferedProxy.create()));
11+
test('that appears correct', function() {
12+
ok(BufferedMixin.detect(BufferedProxy.create()));
1313
});

0 commit comments

Comments
 (0)