Skip to content

Commit c39ff05

Browse files
committed
Fixes #104. Fixes #105. Fixes #106. Closes #107. Fixes #108. Fixes #110.
Stable Version 0.10.4.
1 parent bc32728 commit c39ff05

File tree

13 files changed

+173
-80
lines changed

13 files changed

+173
-80
lines changed

CHANGELOG.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
##### 0.10.4 - xx July 2014
1+
##### 0.10.4 - 04 August 2014
2+
3+
###### Breaking API changes
4+
- #110 - `DS.refresh` now always returns a promise
25

36
###### Backwards compatible API changes
47
- #103 - Add `upsert` option to `DS.create`
8+
- #107 - Computed properties no dependencies
9+
10+
###### Backwards compatible bug fixes
11+
- #104 - Only hijack $rootScope digest when Object.observe is unavailable
12+
- #105 - prototype methods shouldn't be included in change sets
13+
- #106 - cacheResponse: false should force bypassCache to true
14+
- #108 - Computed properties array syntax and primary id
15+
- #110 - `DS.refresh` should still return a promise if the item isn't already in the data store
516

617
##### 0.10.3 - 24 July 2014
718

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
__Data store for Angular.js.__
44

5-
__Latest Release:__ [0.10.3](http://angular-data.pseudobry.com/)
5+
__Latest Release:__ [0.10.4](http://angular-data.pseudobry.com/)
66
__master:__ [0.10.4](http://angular-data-next.pseudobry.com/)
77

88
Angular-data is approaching 1.0.0 Beta. The API is stabilizing and angular-data is well tested.

dist/angular-data.js

+58-34
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@ function find(resourceName, id, options) {
24022402
var resource = this.store[resourceName];
24032403
var _this = this;
24042404

2405-
if (options.bypassCache) {
2405+
if (options.bypassCache || !options.cacheResponse) {
24062406
delete resource.completedQueries[id];
24072407
}
24082408

@@ -2472,7 +2472,7 @@ function _findAll(utils, resourceName, params, options) {
24722472
_this = this,
24732473
queryHash = utils.toJson(params);
24742474

2475-
if (options.bypassCache) {
2475+
if (options.bypassCache || !options.cacheResponse) {
24762476
delete resource.completedQueries[queryHash];
24772477
}
24782478

@@ -2861,17 +2861,19 @@ var errorPrefix = 'DS.refresh(resourceName, id[, options]): ';
28612861
*
28622862
* ```js
28632863
* // Exists in the data store, but we want a fresh copy
2864-
* DS.get('document', 'ee7f3f4d-98d5-4934-9e5a-6a559b08479f');
2864+
* DS.get('document', 5);
28652865
*
2866-
* DS.refresh('document', 'ee7f3f4d-98d5-4934-9e5a-6a559b08479f')
2866+
* DS.refresh('document', 5)
28672867
* .then(function (document) {
28682868
* document; // The fresh copy
28692869
* });
28702870
*
28712871
* // Does not exist in the data store
2872-
* DS.get('document', 'aab7ff66-e21e-46e2-8be8-264d82aee535');
2872+
* DS.get('document', 6); // undefined
28732873
*
2874-
* DS.refresh('document', 'aab7ff66-e21e-46e2-8be8-264d82aee535'); // false
2874+
* DS.refresh('document', 6).then(function (document) {
2875+
* document; // undeinfed
2876+
* }); // false
28752877
* ```
28762878
*
28772879
* ## Throws
@@ -2882,8 +2884,7 @@ var errorPrefix = 'DS.refresh(resourceName, id[, options]): ';
28822884
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
28832885
* @param {string|number} id The primary key of the item to refresh from the server.
28842886
* @param {object=} options Optional configuration passed through to `DS.find` if it is called.
2885-
* @returns {false|Promise} `false` if the item doesn't already exist in the data store. A `Promise` if the item does
2886-
* exist in the data store and is being refreshed.
2887+
* @returns {Promise} A Promise created by the $q server.
28872888
*
28882889
* ## Resolves with:
28892890
*
@@ -2911,7 +2912,9 @@ function refresh(resourceName, id, options) {
29112912
if (this.get(resourceName, id)) {
29122913
return this.find(resourceName, id, options);
29132914
} else {
2914-
return false;
2915+
var deferred = this.$q.defer();
2916+
deferred.resolve();
2917+
return deferred.promise;
29152918
}
29162919
}
29172920
}
@@ -4000,12 +4003,15 @@ function DSProvider() {
40004003
DSUtils.deepFreeze(DS.errors);
40014004
DSUtils.deepFreeze(DS.utils);
40024005

4003-
$rootScope.$watch(function () {
4004-
// Throttle angular-data's digest loop to tenths of a second
4005-
return new Date().getTime() / 100 | 0;
4006-
}, function () {
4007-
DS.digest();
4008-
});
4006+
if (typeof Object.observe !== 'function' ||
4007+
typeof Array.observe !== 'function') {
4008+
$rootScope.$watch(function () {
4009+
// Throttle angular-data's digest loop to tenths of a second
4010+
return new Date().getTime() / 100 | 0;
4011+
}, function () {
4012+
DS.digest();
4013+
});
4014+
}
40094015

40104016
return DS;
40114017
}
@@ -4210,9 +4216,21 @@ function changes(resourceName, id) {
42104216
}
42114217

42124218
var item = this.get(resourceName, id);
4219+
var _this = this;
4220+
42134221
if (item) {
42144222
this.store[resourceName].observers[id].deliver();
4215-
return this.utils.diffObjectFromOldObject(item, this.store[resourceName].previousAttributes[id]);
4223+
var diff = this.utils.diffObjectFromOldObject(item, this.store[resourceName].previousAttributes[id]);
4224+
this.utils.forOwn(diff, function (changeset, name) {
4225+
var toKeep = [];
4226+
_this.utils.forOwn(changeset, function (value, field) {
4227+
if (!angular.isFunction(value)) {
4228+
toKeep.push(field);
4229+
}
4230+
});
4231+
diff[name] = _this.utils.pick(diff[name], toKeep);
4232+
});
4233+
return diff;
42164234
}
42174235
}
42184236

@@ -4455,23 +4473,30 @@ function defineResource(definition) {
44554473
// Prepare for computed properties
44564474
if (def.computed) {
44574475
DS.utils.forOwn(def.computed, function (fn, field) {
4476+
if (angular.isFunction(fn)) {
4477+
def.computed[field] = [fn];
4478+
fn = def.computed[field];
4479+
}
44584480
if (def.methods && field in def.methods) {
44594481
DS.$log.warn(errorPrefix + 'Computed property "' + field + '" conflicts with previously defined prototype method!');
44604482
}
44614483
var deps;
4462-
if (angular.isFunction(fn)) {
4463-
var match = fn.toString().match(/function.*?\(([\s\S]*?)\)/);
4484+
if (fn.length === 1) {
4485+
var match = fn[0].toString().match(/function.*?\(([\s\S]*?)\)/);
44644486
deps = match[1].split(',');
4465-
DS.$log.warn(errorPrefix + 'Use the computed property array for compatibility with minified code!');
4466-
} else {
4467-
deps = fn.slice(0, fn.length - 1);
4487+
def.computed[field] = deps.concat(fn);
4488+
fn = def.computed[field];
4489+
if (deps.length) {
4490+
DS.$log.warn(errorPrefix + 'Use the computed property array syntax for compatibility with minified code!');
4491+
}
44684492
}
4493+
deps = fn.slice(0, fn.length - 1);
4494+
angular.forEach(deps, function (val, index) {
4495+
deps[index] = val.trim();
4496+
});
44694497
fn.deps = DS.utils.filter(deps, function (dep) {
44704498
return !!dep;
44714499
});
4472-
angular.forEach(fn.deps, function (val, index) {
4473-
fn.deps[index] = val.trim();
4474-
});
44754500
});
44764501
}
44774502

@@ -5120,17 +5145,14 @@ function _inject(definition, resource, attrs) {
51205145
compute = true;
51215146
}
51225147
});
5148+
compute = compute || !fn.deps.length;
51235149
if (compute) {
51245150
var args = [];
51255151
angular.forEach(fn.deps, function (dep) {
51265152
args.push(item[dep]);
51275153
});
51285154
// recompute property
5129-
if (angular.isFunction(fn)) {
5130-
item[field] = fn.apply(item, args);
5131-
} else {
5132-
item[field] = fn[fn.length - 1].apply(item, args);
5133-
}
5155+
item[field] = fn[fn.length - 1].apply(item, args);
51345156
}
51355157
});
51365158
}
@@ -5150,21 +5172,23 @@ function _inject(definition, resource, attrs) {
51505172
}
51515173
} else {
51525174
// check if "idAttribute" is a computed property
5153-
if (definition.computed && definition.computed[definition.idAttribute]) {
5175+
var c = definition.computed;
5176+
var idA = definition.idAttribute;
5177+
if (c && c[idA]) {
51545178
var args = [];
5155-
angular.forEach(definition.computed[definition.idAttribute].deps, function (dep) {
5179+
angular.forEach(c[idA].deps, function (dep) {
51565180
args.push(attrs[dep]);
51575181
});
5158-
attrs[definition.idAttribute] = definition.computed[definition.idAttribute].apply(attrs, args);
5182+
attrs[idA] = c[idA][c[idA].length - 1].apply(attrs, args);
51595183
}
5160-
if (!(definition.idAttribute in attrs)) {
5184+
if (!(idA in attrs)) {
51615185
var error = new _this.errors.R(errorPrefix + 'attrs: Must contain the property specified by `idAttribute`!');
51625186
$log.error(error);
51635187
throw error;
51645188
} else {
51655189
try {
51665190
definition.beforeInject(definition.name, attrs);
5167-
var id = attrs[definition.idAttribute];
5191+
var id = attrs[idA];
51685192
var item = this.get(definition.name, id);
51695193

51705194
if (!item) {

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/datastore/async_methods/find.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function find(resourceName, id, options) {
6969
var resource = this.store[resourceName];
7070
var _this = this;
7171

72-
if (options.bypassCache) {
72+
if (options.bypassCache || !options.cacheResponse) {
7373
delete resource.completedQueries[id];
7474
}
7575

src/datastore/async_methods/findAll.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function _findAll(utils, resourceName, params, options) {
3131
_this = this,
3232
queryHash = utils.toJson(params);
3333

34-
if (options.bypassCache) {
34+
if (options.bypassCache || !options.cacheResponse) {
3535
delete resource.completedQueries[queryHash];
3636
}
3737

src/datastore/async_methods/refresh.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ var errorPrefix = 'DS.refresh(resourceName, id[, options]): ';
1515
*
1616
* ```js
1717
* // Exists in the data store, but we want a fresh copy
18-
* DS.get('document', 'ee7f3f4d-98d5-4934-9e5a-6a559b08479f');
18+
* DS.get('document', 5);
1919
*
20-
* DS.refresh('document', 'ee7f3f4d-98d5-4934-9e5a-6a559b08479f')
20+
* DS.refresh('document', 5)
2121
* .then(function (document) {
2222
* document; // The fresh copy
2323
* });
2424
*
2525
* // Does not exist in the data store
26-
* DS.get('document', 'aab7ff66-e21e-46e2-8be8-264d82aee535');
26+
* DS.get('document', 6); // undefined
2727
*
28-
* DS.refresh('document', 'aab7ff66-e21e-46e2-8be8-264d82aee535'); // false
28+
* DS.refresh('document', 6).then(function (document) {
29+
* document; // undeinfed
30+
* }); // false
2931
* ```
3032
*
3133
* ## Throws
@@ -36,8 +38,7 @@ var errorPrefix = 'DS.refresh(resourceName, id[, options]): ';
3638
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
3739
* @param {string|number} id The primary key of the item to refresh from the server.
3840
* @param {object=} options Optional configuration passed through to `DS.find` if it is called.
39-
* @returns {false|Promise} `false` if the item doesn't already exist in the data store. A `Promise` if the item does
40-
* exist in the data store and is being refreshed.
41+
* @returns {Promise} A Promise created by the $q server.
4142
*
4243
* ## Resolves with:
4344
*
@@ -65,7 +66,9 @@ function refresh(resourceName, id, options) {
6566
if (this.get(resourceName, id)) {
6667
return this.find(resourceName, id, options);
6768
} else {
68-
return false;
69+
var deferred = this.$q.defer();
70+
deferred.resolve();
71+
return deferred.promise;
6972
}
7073
}
7174
}

src/datastore/index.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,15 @@ function DSProvider() {
702702
DSUtils.deepFreeze(DS.errors);
703703
DSUtils.deepFreeze(DS.utils);
704704

705-
$rootScope.$watch(function () {
706-
// Throttle angular-data's digest loop to tenths of a second
707-
return new Date().getTime() / 100 | 0;
708-
}, function () {
709-
DS.digest();
710-
});
705+
if (typeof Object.observe !== 'function' ||
706+
typeof Array.observe !== 'function') {
707+
$rootScope.$watch(function () {
708+
// Throttle angular-data's digest loop to tenths of a second
709+
return new Date().getTime() / 100 | 0;
710+
}, function () {
711+
DS.digest();
712+
});
713+
}
711714

712715
return DS;
713716
}

src/datastore/sync_methods/changes.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,21 @@ function changes(resourceName, id) {
4141
}
4242

4343
var item = this.get(resourceName, id);
44+
var _this = this;
45+
4446
if (item) {
4547
this.store[resourceName].observers[id].deliver();
46-
return this.utils.diffObjectFromOldObject(item, this.store[resourceName].previousAttributes[id]);
48+
var diff = this.utils.diffObjectFromOldObject(item, this.store[resourceName].previousAttributes[id]);
49+
this.utils.forOwn(diff, function (changeset, name) {
50+
var toKeep = [];
51+
_this.utils.forOwn(changeset, function (value, field) {
52+
if (!angular.isFunction(value)) {
53+
toKeep.push(field);
54+
}
55+
});
56+
diff[name] = _this.utils.pick(diff[name], toKeep);
57+
});
58+
return diff;
4759
}
4860
}
4961

src/datastore/sync_methods/defineResource.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,30 @@ function defineResource(definition) {
164164
// Prepare for computed properties
165165
if (def.computed) {
166166
DS.utils.forOwn(def.computed, function (fn, field) {
167+
if (angular.isFunction(fn)) {
168+
def.computed[field] = [fn];
169+
fn = def.computed[field];
170+
}
167171
if (def.methods && field in def.methods) {
168172
DS.$log.warn(errorPrefix + 'Computed property "' + field + '" conflicts with previously defined prototype method!');
169173
}
170174
var deps;
171-
if (angular.isFunction(fn)) {
172-
var match = fn.toString().match(/function.*?\(([\s\S]*?)\)/);
175+
if (fn.length === 1) {
176+
var match = fn[0].toString().match(/function.*?\(([\s\S]*?)\)/);
173177
deps = match[1].split(',');
174-
DS.$log.warn(errorPrefix + 'Use the computed property array for compatibility with minified code!');
175-
} else {
176-
deps = fn.slice(0, fn.length - 1);
178+
def.computed[field] = deps.concat(fn);
179+
fn = def.computed[field];
180+
if (deps.length) {
181+
DS.$log.warn(errorPrefix + 'Use the computed property array syntax for compatibility with minified code!');
182+
}
177183
}
184+
deps = fn.slice(0, fn.length - 1);
185+
angular.forEach(deps, function (val, index) {
186+
deps[index] = val.trim();
187+
});
178188
fn.deps = DS.utils.filter(deps, function (dep) {
179189
return !!dep;
180190
});
181-
angular.forEach(fn.deps, function (val, index) {
182-
fn.deps[index] = val.trim();
183-
});
184191
});
185192
}
186193

0 commit comments

Comments
 (0)