Skip to content

Commit f760f6c

Browse files
committed
Merge branch 'master' into model-events
Conflicts: dist/angular-data.min.js
2 parents 2edb6fa + 5779709 commit f760f6c

22 files changed

+145
-8
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
- #155 - Allow deserialize and serialize to be configured per-method as well
66
- #159 - Find which items from collection have changed with lastModified
77
- #161 - Allow the http method of DSHttpAdapter methods to be configured
8+
- #166 - Add ID Resolver
89
- #167 - Default params argument of bindAll to empty object
910
- #171 - "not in" query
1011

1112
###### Backwards compatible bug fixes
1213
- #156 - cached findAll pending query doesn't get removed sometimes
14+
- #163 - loadRelations shouldn't try to load a relation if the id for it is missing
15+
- #165 - DS.hasChanges() reports changes after loading relations
1316

1417
##### 1.0.0-rc.1 - 03 September 2014
1518

dist/angular-data.js

+48-3
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,7 @@ function destroy(resourceName, id, options) {
25582558

25592559
options = options || {};
25602560

2561+
id = DS.utils.resolveId(definition, id);
25612562
if (!definition) {
25622563
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
25632564
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -3181,17 +3182,17 @@ function loadRelations(resourceName, instance, relations, options) {
31813182
var params = {};
31823183
params[def.foreignKey] = instance[definition.idAttribute];
31833184

3184-
if (def.type === 'hasMany') {
3185+
if (def.type === 'hasMany' && params[def.foreignKey]) {
31853186
task = DS.findAll(relationName, params, options);
31863187
} else if (def.type === 'hasOne') {
31873188
if (def.localKey && instance[def.localKey]) {
31883189
task = DS.find(relationName, instance[def.localKey], options);
3189-
} else if (def.foreignKey) {
3190+
} else if (def.foreignKey && params[def.foreignKey]) {
31903191
task = DS.findAll(relationName, params, options).then(function (hasOnes) {
31913192
return hasOnes.length ? hasOnes[0] : null;
31923193
});
31933194
}
3194-
} else {
3195+
} else if (instance[def.localKey]) {
31953196
task = DS.find(relationName, instance[def.localKey], options);
31963197
}
31973198

@@ -3282,6 +3283,7 @@ function refresh(resourceName, id, options) {
32823283

32833284
options = options || {};
32843285

3286+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
32853287
if (!DS.definitions[resourceName]) {
32863288
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
32873289
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -3367,6 +3369,7 @@ function save(resourceName, id, options) {
33673369

33683370
options = options || {};
33693371

3372+
id = DS.utils.resolveId(definition, id);
33703373
if (!definition) {
33713374
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
33723375
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -3519,6 +3522,7 @@ function update(resourceName, id, attrs, options) {
35193522

35203523
options = options || {};
35213524

3525+
id = DS.utils.resolveId(definition, id);
35223526
if (!definition) {
35233527
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
35243528
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -4605,6 +4609,7 @@ function bindOne(scope, expr, resourceName, id, cb) {
46054609
var DS = this;
46064610
var IA = DS.errors.IA;
46074611

4612+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
46084613
if (!DS.utils.isObject(scope)) {
46094614
throw new IA(errorPrefix(resourceName) + 'scope: Must be an object!');
46104615
} else if (!DS.utils.isString(expr)) {
@@ -4681,6 +4686,8 @@ function changeHistory(resourceName, id) {
46814686
var DSUtils = DS.utils;
46824687
var definition = DS.definitions[resourceName];
46834688
var resource = DS.store[resourceName];
4689+
4690+
id = DS.utils.resolveId(definition, id);
46844691
if (resourceName && !DS.definitions[resourceName]) {
46854692
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
46864693
} else if (id && !DSUtils.isString(id) && !DSUtils.isNumber(id)) {
@@ -4745,6 +4752,8 @@ function errorPrefix(resourceName) {
47454752
*/
47464753
function changes(resourceName, id) {
47474754
var DS = this;
4755+
4756+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
47484757
if (!DS.definitions[resourceName]) {
47494758
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
47504759
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -4764,6 +4773,11 @@ function changes(resourceName, id) {
47644773
});
47654774
diff[name] = DS.utils.pick(diff[name], toKeep);
47664775
});
4776+
DS.utils.forEach(DS.definitions[resourceName].relationFields, function (field) {
4777+
delete diff.added[field];
4778+
delete diff.removed[field];
4779+
delete diff.changed[field];
4780+
});
47674781
return diff;
47684782
}
47694783
}
@@ -4844,6 +4858,7 @@ function compute(resourceName, instance) {
48444858
var IA = DS.errors.IA;
48454859
var definition = DS.definitions[resourceName];
48464860

4861+
instance = DS.utils.resolveItem(DS.store[resourceName], instance);
48474862
if (!definition) {
48484863
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
48494864
} else if (!DS.utils.isObject(instance) && !DS.utils.isString(instance) && !DS.utils.isNumber(instance)) {
@@ -5404,6 +5419,8 @@ function _eject(definition, resource, id) {
54045419
function eject(resourceName, id) {
54055420
var DS = this;
54065421
var definition = DS.definitions[resourceName];
5422+
5423+
id = DS.utils.resolveId(definition, id);
54075424
if (!definition) {
54085425
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
54095426
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -5733,6 +5750,8 @@ function diffIsEmpty(utils, diff) {
57335750
*/
57345751
function hasChanges(resourceName, id) {
57355752
var DS = this;
5753+
5754+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
57365755
if (!DS.definitions[resourceName]) {
57375756
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
57385757
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -6320,6 +6339,8 @@ function errorPrefix(resourceName, id) {
63206339
function lastModified(resourceName, id) {
63216340
var DS = this;
63226341
var resource = DS.store[resourceName];
6342+
6343+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
63236344
if (!DS.definitions[resourceName]) {
63246345
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
63256346
} else if (id && !DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -6385,6 +6406,8 @@ function errorPrefix(resourceName, id) {
63856406
function lastSaved(resourceName, id) {
63866407
var DS = this;
63876408
var resource = DS.store[resourceName];
6409+
6410+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
63886411
if (!DS.definitions[resourceName]) {
63896412
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
63906413
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -6475,6 +6498,7 @@ function link(resourceName, id, relations) {
64756498

64766499
relations = relations || [];
64776500

6501+
id = DS.utils.resolveId(definition, id);
64786502
if (!definition) {
64796503
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
64806504
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -6688,6 +6712,8 @@ function linkInverse(resourceName, id, relations) {
66886712

66896713
relations = relations || [];
66906714

6715+
6716+
id = DS.utils.resolveId(definition, id);
66916717
if (!definition) {
66926718
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
66936719
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -6755,6 +6781,8 @@ function errorPrefix(resourceName, id) {
67556781
*/
67566782
function previous(resourceName, id) {
67576783
var DS = this;
6784+
6785+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
67586786
if (!DS.definitions[resourceName]) {
67596787
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
67606788
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -6841,6 +6869,7 @@ function unlinkInverse(resourceName, id, relations) {
68416869

68426870
relations = relations || [];
68436871

6872+
id = DS.utils.resolveId(definition, id);
68446873
if (!definition) {
68456874
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
68466875
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -7156,6 +7185,22 @@ module.exports = [function () {
71567185
remove: require('mout/array/remove'),
71577186
slice: require('mout/array/slice'),
71587187
sort: require('mout/array/sort'),
7188+
resolveItem: function (resource, idOrInstance) {
7189+
if (resource && (this.isString(idOrInstance) || this.isNumber(idOrInstance))) {
7190+
return resource.index[idOrInstance] || idOrInstance;
7191+
} else {
7192+
return idOrInstance;
7193+
}
7194+
},
7195+
resolveId: function (definition, idOrInstance) {
7196+
if (this.isString(idOrInstance) || this.isNumber(idOrInstance)) {
7197+
return idOrInstance;
7198+
} else if (idOrInstance && definition) {
7199+
return idOrInstance[definition.idAttribute] || idOrInstance;
7200+
} else {
7201+
return idOrInstance;
7202+
}
7203+
},
71597204
updateTimestamp: function (timestamp) {
71607205
var newTimestamp = typeof Date.now === 'function' ? Date.now() : new Date().getTime();
71617206
if (timestamp && newTimestamp <= timestamp) {

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/destroy.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function destroy(resourceName, id, options) {
5454

5555
options = options || {};
5656

57+
id = DS.utils.resolveId(definition, id);
5758
if (!definition) {
5859
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
5960
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/async_methods/loadRelations.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ function loadRelations(resourceName, instance, relations, options) {
101101
var params = {};
102102
params[def.foreignKey] = instance[definition.idAttribute];
103103

104-
if (def.type === 'hasMany') {
104+
if (def.type === 'hasMany' && params[def.foreignKey]) {
105105
task = DS.findAll(relationName, params, options);
106106
} else if (def.type === 'hasOne') {
107107
if (def.localKey && instance[def.localKey]) {
108108
task = DS.find(relationName, instance[def.localKey], options);
109-
} else if (def.foreignKey) {
109+
} else if (def.foreignKey && params[def.foreignKey]) {
110110
task = DS.findAll(relationName, params, options).then(function (hasOnes) {
111111
return hasOnes.length ? hasOnes[0] : null;
112112
});
113113
}
114-
} else {
114+
} else if (instance[def.localKey]) {
115115
task = DS.find(relationName, instance[def.localKey], options);
116116
}
117117

src/datastore/async_methods/refresh.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function refresh(resourceName, id, options) {
5757

5858
options = options || {};
5959

60+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
6061
if (!DS.definitions[resourceName]) {
6162
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
6263
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/async_methods/save.js

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function save(resourceName, id, options) {
6161

6262
options = options || {};
6363

64+
id = DS.utils.resolveId(definition, id);
6465
if (!definition) {
6566
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
6667
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/async_methods/update.js

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function update(resourceName, id, attrs, options) {
6363

6464
options = options || {};
6565

66+
id = DS.utils.resolveId(definition, id);
6667
if (!definition) {
6768
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
6869
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/sync_methods/bindOne.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function bindOne(scope, expr, resourceName, id, cb) {
3737
var DS = this;
3838
var IA = DS.errors.IA;
3939

40+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
4041
if (!DS.utils.isObject(scope)) {
4142
throw new IA(errorPrefix(resourceName) + 'scope: Must be an object!');
4243
} else if (!DS.utils.isString(expr)) {

src/datastore/sync_methods/changeHistory.js

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ function changeHistory(resourceName, id) {
4242
var DSUtils = DS.utils;
4343
var definition = DS.definitions[resourceName];
4444
var resource = DS.store[resourceName];
45+
46+
id = DS.utils.resolveId(definition, id);
4547
if (resourceName && !DS.definitions[resourceName]) {
4648
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
4749
} else if (id && !DSUtils.isString(id) && !DSUtils.isNumber(id)) {

src/datastore/sync_methods/changes.js

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ function errorPrefix(resourceName) {
3939
*/
4040
function changes(resourceName, id) {
4141
var DS = this;
42+
43+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
4244
if (!DS.definitions[resourceName]) {
4345
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
4446
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {
@@ -58,6 +60,11 @@ function changes(resourceName, id) {
5860
});
5961
diff[name] = DS.utils.pick(diff[name], toKeep);
6062
});
63+
DS.utils.forEach(DS.definitions[resourceName].relationFields, function (field) {
64+
delete diff.added[field];
65+
delete diff.removed[field];
66+
delete diff.changed[field];
67+
});
6168
return diff;
6269
}
6370
}

src/datastore/sync_methods/compute.js

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function compute(resourceName, instance) {
7171
var IA = DS.errors.IA;
7272
var definition = DS.definitions[resourceName];
7373

74+
instance = DS.utils.resolveItem(DS.store[resourceName], instance);
7475
if (!definition) {
7576
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
7677
} else if (!DS.utils.isObject(instance) && !DS.utils.isString(instance) && !DS.utils.isNumber(instance)) {

src/datastore/sync_methods/eject.js

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ function _eject(definition, resource, id) {
7575
function eject(resourceName, id) {
7676
var DS = this;
7777
var definition = DS.definitions[resourceName];
78+
79+
id = DS.utils.resolveId(definition, id);
7880
if (!definition) {
7981
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
8082
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/sync_methods/hasChanges.js

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function diffIsEmpty(utils, diff) {
4444
*/
4545
function hasChanges(resourceName, id) {
4646
var DS = this;
47+
48+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
4749
if (!DS.definitions[resourceName]) {
4850
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
4951
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/sync_methods/lastModified.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function errorPrefix(resourceName, id) {
3838
function lastModified(resourceName, id) {
3939
var DS = this;
4040
var resource = DS.store[resourceName];
41+
42+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
4143
if (!DS.definitions[resourceName]) {
4244
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
4345
} else if (id && !DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/sync_methods/lastSaved.js

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ function errorPrefix(resourceName, id) {
4646
function lastSaved(resourceName, id) {
4747
var DS = this;
4848
var resource = DS.store[resourceName];
49+
50+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
4951
if (!DS.definitions[resourceName]) {
5052
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
5153
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/sync_methods/link.js

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function link(resourceName, id, relations) {
7474

7575
relations = relations || [];
7676

77+
id = DS.utils.resolveId(definition, id);
7778
if (!definition) {
7879
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
7980
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/sync_methods/linkInverse.js

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ function linkInverse(resourceName, id, relations) {
6969

7070
relations = relations || [];
7171

72+
73+
id = DS.utils.resolveId(definition, id);
7274
if (!definition) {
7375
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
7476
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/sync_methods/previous.js

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ function errorPrefix(resourceName, id) {
4040
*/
4141
function previous(resourceName, id) {
4242
var DS = this;
43+
44+
id = DS.utils.resolveId(DS.definitions[resourceName], id);
4345
if (!DS.definitions[resourceName]) {
4446
throw new DS.errors.NER(errorPrefix(resourceName, id) + resourceName);
4547
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/datastore/sync_methods/unlinkInverse.js

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function unlinkInverse(resourceName, id, relations) {
7171

7272
relations = relations || [];
7373

74+
id = DS.utils.resolveId(definition, id);
7475
if (!definition) {
7576
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
7677
} else if (!DS.utils.isString(id) && !DS.utils.isNumber(id)) {

src/utils.js

+16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ module.exports = [function () {
6767
remove: require('mout/array/remove'),
6868
slice: require('mout/array/slice'),
6969
sort: require('mout/array/sort'),
70+
resolveItem: function (resource, idOrInstance) {
71+
if (resource && (this.isString(idOrInstance) || this.isNumber(idOrInstance))) {
72+
return resource.index[idOrInstance] || idOrInstance;
73+
} else {
74+
return idOrInstance;
75+
}
76+
},
77+
resolveId: function (definition, idOrInstance) {
78+
if (this.isString(idOrInstance) || this.isNumber(idOrInstance)) {
79+
return idOrInstance;
80+
} else if (idOrInstance && definition) {
81+
return idOrInstance[definition.idAttribute] || idOrInstance;
82+
} else {
83+
return idOrInstance;
84+
}
85+
},
7086
updateTimestamp: function (timestamp) {
7187
var newTimestamp = typeof Date.now === 'function' ? Date.now() : new Date().getTime();
7288
if (timestamp && newTimestamp <= timestamp) {

0 commit comments

Comments
 (0)