Skip to content

Commit 615699e

Browse files
committedAug 26, 2014
Closes #136.
1 parent 8b2a4f3 commit 615699e

File tree

9 files changed

+275
-37
lines changed

9 files changed

+275
-37
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
###### Backwards compatible API changes
77
- #132 - Added `findHasMany` option and capability to `DS.inject`
8+
- #136 - Add a way to force computed properties to be computed. Added `DS.compute`. Added `DSCompute` to prototype of instances.
89
- #137 - Add `DS.link`, `DS.linkAll`, and `DS.linkInverse`, and added `linkInverse` option to `DS.inject`
910

1011
###### Backwards compatible bug fixes

‎dist/angular-data.js

+135-27
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,6 @@ function DSHttpAdapterProvider() {
14721472
* ```js
14731473
* angular.module('myApp').config(function (DSHttpAdapterProvider) {
14741474
* angular.extend(DSHttpAdapterProvider.defaults.$httpConfig, {
1475-
* interceptor: [...],
14761475
* headers: {
14771476
* Authorization: 'Basic YmVlcDpib29w'
14781477
* },
@@ -4217,7 +4216,7 @@ function DSProvider() {
42174216

42184217
module.exports = DSProvider;
42194218

4220-
},{"../utils":71,"./async_methods":43,"./sync_methods":61}],50:[function(require,module,exports){
4219+
},{"../utils":72,"./async_methods":43,"./sync_methods":62}],50:[function(require,module,exports){
42214220
function errorPrefix(resourceName) {
42224221
return 'DS.bindAll(scope, expr, ' + resourceName + ', params[, cb]): ';
42234222
}
@@ -4439,6 +4438,102 @@ function changes(resourceName, id) {
44394438
module.exports = changes;
44404439

44414440
},{}],53:[function(require,module,exports){
4441+
function errorPrefix(resourceName) {
4442+
return 'DS.compute(' + resourceName + ', instance): ';
4443+
}
4444+
4445+
function _compute(fn, field) {
4446+
var _this = this;
4447+
var args = [];
4448+
angular.forEach(fn.deps, function (dep) {
4449+
args.push(_this[dep]);
4450+
});
4451+
// compute property
4452+
this[field] = fn[fn.length - 1].apply(this, args);
4453+
}
4454+
4455+
/**
4456+
* @doc method
4457+
* @id DS.sync methods:compute
4458+
* @name compute
4459+
* @description
4460+
* Force the given instance or the item with the given primary key to recompute its computed properties.
4461+
*
4462+
* ## Signature:
4463+
* ```js
4464+
* DS.compute(resourceName, instance)
4465+
* ```
4466+
*
4467+
* ## Example:
4468+
*
4469+
* ```js
4470+
* var User = DS.defineResource({
4471+
* name: 'user',
4472+
* computed: {
4473+
* fullName: ['first', 'last', function (first, last) {
4474+
* return first + ' ' + last;
4475+
* }]
4476+
* }
4477+
* });
4478+
*
4479+
* var user = User.createInstance({ first: 'John', last: 'Doe' });
4480+
* user.fullName; // undefined
4481+
*
4482+
* User.compute(user);
4483+
*
4484+
* user.fullName; // "John Doe"
4485+
*
4486+
* var user2 = User.inject({ id: 2, first: 'Jane', last: 'Doe' });
4487+
* user2.fullName; // undefined
4488+
*
4489+
* User.compute(1);
4490+
*
4491+
* user2.fullName; // "Jane Doe"
4492+
*
4493+
* // if you don't pass useClass: false then you can do:
4494+
* var user3 = User.createInstance({ first: 'Sally', last: 'Doe' });
4495+
* user3.fullName; // undefined
4496+
* user3.DSCompute();
4497+
* user3.fullName; // "Sally Doe"
4498+
* ```
4499+
*
4500+
* ## Throws
4501+
*
4502+
* - `{IllegalArgumentError}`
4503+
* - `{NonexistentResourceError}`
4504+
*
4505+
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
4506+
* @param {object|string|number} instance Instance or primary key of the instance (must be in the store) for which to recompute properties.
4507+
* @returns {Object} The instance.
4508+
*/
4509+
function compute(resourceName, instance) {
4510+
var DS = this;
4511+
var IA = DS.errors.IA;
4512+
var definition = DS.definitions[resourceName];
4513+
4514+
if (!definition) {
4515+
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
4516+
} else if (!DS.utils.isObject(instance) && !DS.utils.isString(instance) && !DS.utils.isNumber(instance)) {
4517+
throw new IA(errorPrefix(resourceName) + 'instance: Must be an object, string or number!');
4518+
}
4519+
4520+
if (DS.utils.isString(instance) || DS.utils.isNumber(instance)) {
4521+
instance = DS.get(resourceName, instance);
4522+
}
4523+
4524+
DS.utils.forOwn(definition.computed, function (fn, field) {
4525+
_compute.call(instance, fn, field);
4526+
});
4527+
4528+
return instance;
4529+
}
4530+
4531+
module.exports = {
4532+
compute: compute,
4533+
_compute: _compute
4534+
};
4535+
4536+
},{}],54:[function(require,module,exports){
44424537
function errorPrefix(resourceName) {
44434538
return 'DS.createInstance(' + resourceName + '[, attrs][, options]): ';
44444539
}
@@ -4535,7 +4630,7 @@ function createInstance(resourceName, attrs, options) {
45354630

45364631
module.exports = createInstance;
45374632

4538-
},{}],54:[function(require,module,exports){
4633+
},{}],55:[function(require,module,exports){
45394634
/*jshint evil:true*/
45404635
var errorPrefix = 'DS.defineResource(definition): ';
45414636

@@ -4568,6 +4663,9 @@ var methodsToProxy = [
45684663
'inject',
45694664
'lastModified',
45704665
'lastSaved',
4666+
'link',
4667+
'linkAll',
4668+
'linkInverse',
45714669
'loadRelations',
45724670
'previous',
45734671
'refresh',
@@ -4774,6 +4872,10 @@ function defineResource(definition) {
47744872
return !!dep;
47754873
});
47764874
});
4875+
4876+
def[def.class].prototype.DSCompute = function () {
4877+
return DS.compute(def.name, this);
4878+
};
47774879
}
47784880

47794881
// Initialize store data for the new resource
@@ -4817,7 +4919,7 @@ function defineResource(definition) {
48174919

48184920
module.exports = defineResource;
48194921

4820-
},{}],55:[function(require,module,exports){
4922+
},{}],56:[function(require,module,exports){
48214923
var observe = require('../../../lib/observe-js/observe-js');
48224924

48234925
/**
@@ -4853,7 +4955,7 @@ function digest() {
48534955

48544956
module.exports = digest;
48554957

4856-
},{"../../../lib/observe-js/observe-js":1}],56:[function(require,module,exports){
4958+
},{"../../../lib/observe-js/observe-js":1}],57:[function(require,module,exports){
48574959
function errorPrefix(resourceName, id) {
48584960
return 'DS.eject(' + resourceName + ', ' + id + '): ';
48594961
}
@@ -4946,7 +5048,7 @@ function eject(resourceName, id) {
49465048

49475049
module.exports = eject;
49485050

4949-
},{}],57:[function(require,module,exports){
5051+
},{}],58:[function(require,module,exports){
49505052
function errorPrefix(resourceName) {
49515053
return 'DS.ejectAll(' + resourceName + '[, params]): ';
49525054
}
@@ -5060,7 +5162,7 @@ function ejectAll(resourceName, params) {
50605162

50615163
module.exports = ejectAll;
50625164

5063-
},{}],58:[function(require,module,exports){
5165+
},{}],59:[function(require,module,exports){
50645166
function errorPrefix(resourceName) {
50655167
return 'DS.filter(' + resourceName + '[, params][, options]): ';
50665168
}
@@ -5143,7 +5245,7 @@ function filter(resourceName, params, options) {
51435245

51445246
module.exports = filter;
51455247

5146-
},{}],59:[function(require,module,exports){
5248+
},{}],60:[function(require,module,exports){
51475249
function errorPrefix(resourceName, id) {
51485250
return 'DS.get(' + resourceName + ', ' + id + '): ';
51495251
}
@@ -5207,7 +5309,7 @@ function get(resourceName, id, options) {
52075309

52085310
module.exports = get;
52095311

5210-
},{}],60:[function(require,module,exports){
5312+
},{}],61:[function(require,module,exports){
52115313
function errorPrefix(resourceName, id) {
52125314
return 'DS.hasChanges(' + resourceName + ', ' + id + '): ';
52135315
}
@@ -5270,7 +5372,7 @@ function hasChanges(resourceName, id) {
52705372

52715373
module.exports = hasChanges;
52725374

5273-
},{}],61:[function(require,module,exports){
5375+
},{}],62:[function(require,module,exports){
52745376
module.exports = {
52755377

52765378
/**
@@ -5293,6 +5395,16 @@ module.exports = {
52935395
*/
52945396
bindAll: require('./bindAll'),
52955397

5398+
/**
5399+
* @doc method
5400+
* @id DS.sync methods:compute
5401+
* @name compute
5402+
* @methodOf DS
5403+
* @description
5404+
* See [DS.compute](/documentation/api/api/DS.sync methods:compute).
5405+
*/
5406+
compute: require('./compute').compute,
5407+
52965408
/**
52975409
* @doc method
52985410
* @id DS.sync methods:createInstance
@@ -5454,8 +5566,9 @@ module.exports = {
54545566
hasChanges: require('./hasChanges')
54555567
};
54565568

5457-
},{"./bindAll":50,"./bindOne":51,"./changes":52,"./createInstance":53,"./defineResource":54,"./digest":55,"./eject":56,"./ejectAll":57,"./filter":58,"./get":59,"./hasChanges":60,"./inject":62,"./lastModified":63,"./lastSaved":64,"./link":65,"./linkAll":66,"./linkInverse":67,"./previous":68}],62:[function(require,module,exports){
5569+
},{"./bindAll":50,"./bindOne":51,"./changes":52,"./compute":53,"./createInstance":54,"./defineResource":55,"./digest":56,"./eject":57,"./ejectAll":58,"./filter":59,"./get":60,"./hasChanges":61,"./inject":63,"./lastModified":64,"./lastSaved":65,"./link":66,"./linkAll":67,"./linkInverse":68,"./previous":69}],63:[function(require,module,exports){
54585570
var observe = require('../../../lib/observe-js/observe-js');
5571+
var _compute = require('./compute')._compute;
54595572
var stack = 0;
54605573
var data = {
54615574
injectedSoFar: {}
@@ -5489,12 +5602,7 @@ function _inject(definition, resource, attrs) {
54895602
});
54905603
compute = compute || !fn.deps.length;
54915604
if (compute) {
5492-
var args = [];
5493-
angular.forEach(fn.deps, function (dep) {
5494-
args.push(item[dep]);
5495-
});
5496-
// recompute property
5497-
item[field] = fn[fn.length - 1].apply(item, args);
5605+
_compute.call(item, fn, field);
54985606
}
54995607
});
55005608
}
@@ -5758,7 +5866,7 @@ function inject(resourceName, attrs, options) {
57585866

57595867
module.exports = inject;
57605868

5761-
},{"../../../lib/observe-js/observe-js":1}],63:[function(require,module,exports){
5869+
},{"../../../lib/observe-js/observe-js":1,"./compute":53}],64:[function(require,module,exports){
57625870
function errorPrefix(resourceName, id) {
57635871
return 'DS.lastModified(' + resourceName + '[, ' + id + ']): ';
57645872
}
@@ -5815,7 +5923,7 @@ function lastModified(resourceName, id) {
58155923

58165924
module.exports = lastModified;
58175925

5818-
},{}],64:[function(require,module,exports){
5926+
},{}],65:[function(require,module,exports){
58195927
function errorPrefix(resourceName, id) {
58205928
return 'DS.lastSaved(' + resourceName + '[, ' + id + ']): ';
58215929
}
@@ -5877,7 +5985,7 @@ function lastSaved(resourceName, id) {
58775985

58785986
module.exports = lastSaved;
58795987

5880-
},{}],65:[function(require,module,exports){
5988+
},{}],66:[function(require,module,exports){
58815989
function errorPrefix(resourceName) {
58825990
return 'DS.link(' + resourceName + ', id[, relations]): ';
58835991
}
@@ -5985,7 +6093,7 @@ function link(resourceName, id, relations) {
59856093

59866094
module.exports = link;
59876095

5988-
},{}],66:[function(require,module,exports){
6096+
},{}],67:[function(require,module,exports){
59896097
function errorPrefix(resourceName) {
59906098
return 'DS.linkAll(' + resourceName + '[, params][, relations]): ';
59916099
}
@@ -6109,7 +6217,7 @@ function linkAll(resourceName, params, relations) {
61096217

61106218
module.exports = linkAll;
61116219

6112-
},{}],67:[function(require,module,exports){
6220+
},{}],68:[function(require,module,exports){
61136221
function errorPrefix(resourceName) {
61146222
return 'DS.linkInverse(' + resourceName + ', id[, relations]): ';
61156223
}
@@ -6205,7 +6313,7 @@ function linkInverse(resourceName, id, relations) {
62056313

62066314
module.exports = linkInverse;
62076315

6208-
},{}],68:[function(require,module,exports){
6316+
},{}],69:[function(require,module,exports){
62096317
function errorPrefix(resourceName, id) {
62106318
return 'DS.previous(' + resourceName + '[, ' + id + ']): ';
62116319
}
@@ -6260,7 +6368,7 @@ function previous(resourceName, id) {
62606368

62616369
module.exports = previous;
62626370

6263-
},{}],69:[function(require,module,exports){
6371+
},{}],70:[function(require,module,exports){
62646372
/**
62656373
* @doc function
62666374
* @id errors.types:IllegalArgumentError
@@ -6393,7 +6501,7 @@ module.exports = [function () {
63936501
};
63946502
}];
63956503

6396-
},{}],70:[function(require,module,exports){
6504+
},{}],71:[function(require,module,exports){
63976505
(function (window, angular, undefined) {
63986506
'use strict';
63996507

@@ -6476,7 +6584,7 @@ module.exports = [function () {
64766584

64776585
})(window, window.angular);
64786586

6479-
},{"./adapters/http":36,"./adapters/localStorage":37,"./datastore":49,"./errors":69,"./utils":71}],71:[function(require,module,exports){
6587+
},{"./adapters/http":36,"./adapters/localStorage":37,"./datastore":49,"./errors":70,"./utils":72}],72:[function(require,module,exports){
64806588
module.exports = [function () {
64816589
return {
64826590
isBoolean: require('mout/lang/isBoolean'),
@@ -6561,4 +6669,4 @@ module.exports = [function () {
65616669
};
65626670
}];
65636671

6564-
},{"mout/array/contains":2,"mout/array/filter":3,"mout/array/slice":7,"mout/array/sort":8,"mout/array/toLookup":9,"mout/lang/isBoolean":14,"mout/lang/isEmpty":15,"mout/object/deepMixIn":22,"mout/object/forOwn":24,"mout/object/pick":27,"mout/object/set":28,"mout/string/makePath":31,"mout/string/pascalCase":32,"mout/string/upperCase":35}]},{},[70]);
6672+
},{"mout/array/contains":2,"mout/array/filter":3,"mout/array/slice":7,"mout/array/sort":8,"mout/array/toLookup":9,"mout/lang/isBoolean":14,"mout/lang/isEmpty":15,"mout/object/deepMixIn":22,"mout/object/forOwn":24,"mout/object/pick":27,"mout/object/set":28,"mout/string/makePath":31,"mout/string/pascalCase":32,"mout/string/upperCase":35}]},{},[71]);

‎dist/angular-data.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/adapters/http.js

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ function DSHttpAdapterProvider() {
5454
* ```js
5555
* angular.module('myApp').config(function (DSHttpAdapterProvider) {
5656
* angular.extend(DSHttpAdapterProvider.defaults.$httpConfig, {
57-
* interceptor: [...],
5857
* headers: {
5958
* Authorization: 'Basic YmVlcDpib29w'
6059
* },

0 commit comments

Comments
 (0)
Please sign in to comment.