Skip to content

Commit a4fda44

Browse files
committed
loadRelations from collections.
1 parent 1e21413 commit a4fda44

File tree

2 files changed

+59
-29
lines changed

2 files changed

+59
-29
lines changed

src/datastore/async_methods/loadRelations.js

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ function loadRelations(resourceName, instance, relations, options) {
7474
relations = [relations];
7575
}
7676

77+
if (DSUtils.isArray(instance)) {
78+
var instances = [];
79+
angular.forEach(instance, function(object) {
80+
instances.push(DS.get(resourceName, object));
81+
});
82+
instance = instances;
83+
}
84+
7785
if (!definition) {
7886
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
7987
} else if (!DSUtils.isObject(instance)) {
@@ -96,40 +104,46 @@ function loadRelations(resourceName, instance, relations, options) {
96104
var tasks = [];
97105
var fields = [];
98106

99-
DSUtils.forEach(definition.relationList, function (def) {
100-
var relationName = def.relation;
101-
if (DSUtils.contains(relations, relationName)) {
102-
var task;
103-
var params = {};
104-
if (options.allowSimpleWhere) {
105-
params[def.foreignKey] = instance[definition.idAttribute];
106-
} else {
107-
params.where = {};
108-
params.where[def.foreignKey] = {
109-
'==': instance[definition.idAttribute]
110-
};
111-
}
107+
if (DSUtils.isArray(instance)) {
108+
angular.forEach(instance, function (object) {
109+
tasks.push(DS.loadRelations(resourceName, object, relations, options));
110+
});
111+
} else {
112+
DSUtils.forEach(definition.relationList, function (def) {
113+
var relationName = def.relation;
114+
if (DSUtils.contains(relations, relationName)) {
115+
var task;
116+
var params = {};
117+
if (options.allowSimpleWhere) {
118+
params[def.foreignKey] = instance[definition.idAttribute];
119+
} else {
120+
params.where = {};
121+
params.where[def.foreignKey] = {
122+
'==': instance[definition.idAttribute]
123+
};
124+
}
112125

113-
if (def.type === 'hasMany' && params[def.foreignKey]) {
114-
task = DS.findAll(relationName, params, options);
115-
} else if (def.type === 'hasOne') {
116-
if (def.localKey && instance[def.localKey]) {
126+
if (def.type === 'hasMany' && params[def.foreignKey]) {
127+
task = DS.findAll(relationName, params, options);
128+
} else if (def.type === 'hasOne') {
129+
if (def.localKey && instance[def.localKey]) {
130+
task = DS.find(relationName, instance[def.localKey], options);
131+
} else if (def.foreignKey && params[def.foreignKey]) {
132+
task = DS.findAll(relationName, params, options).then(function (hasOnes) {
133+
return hasOnes.length ? hasOnes[0] : null;
134+
});
135+
}
136+
} else if (instance[def.localKey]) {
117137
task = DS.find(relationName, instance[def.localKey], options);
118-
} else if (def.foreignKey && params[def.foreignKey]) {
119-
task = DS.findAll(relationName, params, options).then(function (hasOnes) {
120-
return hasOnes.length ? hasOnes[0] : null;
121-
});
122138
}
123-
} else if (instance[def.localKey]) {
124-
task = DS.find(relationName, instance[def.localKey], options);
125-
}
126139

127-
if (task) {
128-
tasks.push(task);
129-
fields.push(def.localField);
140+
if (task) {
141+
tasks.push(task);
142+
fields.push(def.localField);
143+
}
130144
}
131-
}
132-
});
145+
});
146+
}
133147

134148
deferred.resolve();
135149

test/integration/datastore/async_methods/loadRelations.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
207207
fail('Should not have succeeded!');
208208
});
209209

210+
$httpBackend.flush();
211+
});
212+
it('should load relations from array of objects', function () {
213+
DS.inject('user', user10);
214+
DS.inject('user', user16);
215+
216+
$httpBackend.expectGET('http://test.angular-cache.com/organization/14').respond(200, organization14);
217+
$httpBackend.expectGET('http://test.angular-cache.com/organization/15').respond(200, organization15);
218+
219+
DS.loadRelations('user', [10, 16], ['organization']).then(function (users) {
220+
assert.isObject(users[0].organization);
221+
assert.equal(users[0].organization.id, organization14.id);
222+
assert.isObject(users[1].organization);
223+
assert.equal(users[1].organization.id, organization15.id);
224+
}, fail);
225+
210226
$httpBackend.flush();
211227
});
212228
});

0 commit comments

Comments
 (0)