Skip to content

Commit 9a01531

Browse files
committed
feat(computed): Adds mask to computed properties.
Also adds some minor code improvements.
1 parent d585e76 commit 9a01531

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

src/module/extended/builder-computed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ RMModule.factory('RMBuilderComputed', ['restmod',
2525
* @description Registers a model computed property
2626
*
2727
* @param {string} _attr Attribute name
28-
* @param {function} _fn Function that returns the desired attribute value when run.
28+
* @param {function} _fn Function that returns the desired attribute value when run.
2929
* @return {BuilderApi} self
3030
*/
3131
attrAsComputed: function(_attr, _fn) {
3232
this.attrComputed(_attr, _fn);
3333
return this;
3434
}
35-
}
35+
};
3636

3737
return restmod.mixin(function() {
3838
this.extend('attrAsComputed', EXT.attrAsComputed, ['computed']);

src/module/factory.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
2424
urlPrefix: null
2525
},
2626
serializer = new Serializer(Model),
27-
computes = {},
2827
defaults = [], // attribute defaults as an array of [key, value]
28+
computes = [], // computed attributes
2929
meta = {}, // atribute metadata
3030
hooks = {},
3131
builder; // the model builder
@@ -332,19 +332,17 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
332332

333333
// default initializer: loads the default parameter values
334334
$initialize: function() {
335-
var tmp, self = this;
336-
for(var i = 0; (tmp = defaults[i]); i++) {
335+
var tmp, i, self = this;
336+
for(i = 0; (tmp = defaults[i]); i++) {
337337
this[tmp[0]] = (typeof tmp[1] === 'function') ? tmp[1].apply(this) : tmp[1];
338338
}
339-
Object.keys(computes).forEach(function(key) {
340-
//console.log(self);
341-
Object.defineProperty(self, key, {
339+
340+
for(i = 0; (tmp = computes[i]); i++) {
341+
Object.defineProperty(self, tmp[0], {
342342
enumerable: true,
343-
get: function() {
344-
return computes[key].apply(self);
345-
}
343+
get: tmp[1]
346344
});
347-
});
345+
}
348346
}
349347

350348
}, CommonApi, RecordApi, ExtendedApi);
@@ -449,13 +447,15 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
449447
* @description Sets a computed value for an attribute.
450448
*
451449
* Computed values are set only on object construction phase.
450+
* Computed values are always masked
452451
*
453452
* @param {string} _attr Attribute name
454453
* @param {function} _fn Function that returns value
455454
* @return {BuilderApi} self
456455
*/
457456
attrComputed: function(_attr, _fn) {
458-
computes[_attr] = _fn;
457+
computes.push([_attr, _fn]);
458+
this.attrMask(_attr, true);
459459
return this;
460460
},
461461

test/computed-spec.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ describe('RMBuilderComputed', function() {
66

77
beforeEach(module('restmod'));
88

9-
beforeEach(module(function($provide, restmodProvider) {
9+
beforeEach(module(function($provide) {
1010
$provide.factory('UserModel', function(restmod) {
1111
return restmod.model('/api/users', {
1212
firstName: ''
1313
});
1414
});
1515
}));
1616

17-
1817
// cache entities to be used in tests
1918
beforeEach(inject(['$injector',
2019
function(_$injector) {
@@ -32,6 +31,7 @@ describe('RMBuilderComputed', function() {
3231
describe('basics', function() {
3332

3433
var DeviceModel, device;
34+
3535
beforeEach(function() {
3636
DeviceModel = restmod.model('/api/devices', {
3737
vendor: 'default vendor',
@@ -50,16 +50,28 @@ describe('RMBuilderComputed', function() {
5050
});
5151

5252
it('changes when model properties update', function() {
53-
device.vendor = "Apple";
54-
device.model = "iPhone";
53+
device.vendor = 'Apple';
54+
device.model = 'iPhone';
5555
expect(device.fancyName).toEqual('Apple: iPhone');
5656
});
5757

58+
it('should be masked by default',function() {
59+
var encoded = device.$encode();
60+
expect(encoded.fancyName).toBeUndefined();
61+
});
62+
63+
it('should be listed in $each',function() {
64+
var test = {};
65+
device.$each(function(v, k) { test[k] = v; });
66+
expect(test.fancyName).toBeDefined();
67+
});
68+
5869
});
5970

6071
describe('with relations', function() {
6172

62-
var DeviceModel, UserModel, device;
73+
var DeviceModel, device;
74+
6375
beforeEach(function() {
6476

6577
DeviceModel = restmod.model('/api/devices', {
@@ -70,7 +82,7 @@ describe('RMBuilderComputed', function() {
7082
},
7183
ownedBy: {
7284
computed: function() {
73-
return this.user.firstName + "'s " + this.model;
85+
return this.user.firstName + '\'s ' + this.model;
7486
}
7587
}
7688
});
@@ -84,7 +96,7 @@ describe('RMBuilderComputed', function() {
8496
});
8597

8698
it('can access related models', function() {
87-
expect(device.ownedBy).toEqual("Johnny's Watch");
99+
expect(device.ownedBy).toEqual('Johnny\'s Watch');
88100
});
89101

90102
});

0 commit comments

Comments
 (0)