Skip to content

Commit 8a66f99

Browse files
committed
Merge pull request #122 from chieffancypants/i118
issue #118: Allow multiple relationships to same model
2 parents f4a390f + 39986cd commit 8a66f99

File tree

5 files changed

+68
-32
lines changed

5 files changed

+68
-32
lines changed

karma.start.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var $rootScope, $q, $log, $timeout, DSHttpAdapterProvider, DSProvider, DSLocalStorageAdapter, DS, DSUtils, DSHttpAdapter, app, $httpBackend, p1, p2, p3, p4, p5;
33

44
var user1, organization2, comment3, profile4;
5-
var comment11, comment12, comment13, organization14, profile15, user10, user16, user17, user18, organization15, user20, comment19, user22, profile21;
5+
var comment11, comment12, comment13, organization14, profile15, user10, user16, user17, user18, organization15, user19, user20, comment19, user22, profile21;
66

77
var lifecycle = {};
88

@@ -184,10 +184,16 @@ function startInjector() {
184184
name: 'comment',
185185
relations: {
186186
belongsTo: {
187-
user: {
188-
localField: 'user',
189-
localKey: 'userId'
190-
}
187+
user: [
188+
{
189+
localField: 'user',
190+
localKey: 'userId'
191+
},
192+
{
193+
localField: 'approvedByUser',
194+
localKey: 'approvedBy'
195+
}
196+
]
191197
}
192198
}
193199
});
@@ -294,13 +300,19 @@ function startInjector() {
294300
user18
295301
]
296302
};
303+
user19 = {
304+
id: 19,
305+
name: 'test user 19'
306+
};
297307
user20 = {
298308
id: 20,
299309
name: 'test user 20'
300310
};
301311
comment19 = {
302312
content: 'test comment 19',
303313
id: 19,
314+
approvedBy: 19,
315+
approvedByUser: user19,
304316
userId: 20,
305317
user: user20
306318
};

src/datastore/async_methods/loadRelations.js

+25-19
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,36 @@ function loadRelations(resourceName, instance, relations, options) {
8787
var tasks = [];
8888
var fields = [];
8989

90-
DS.utils.forOwn(definition.relations, function (relation, type) {
91-
DS.utils.forOwn(relation, function (def, relationName) {
92-
if (DS.utils.contains(relations, relationName)) {
93-
var task;
94-
var params = {};
95-
params[def.foreignKey] = instance[definition.idAttribute];
90+
DS.utils.forOwn(definition.relations, function (relatedModels, type) {
91+
DS.utils.forOwn(relatedModels, function (defs, relationName) {
92+
if (!DS.utils.isArray(defs)) {
93+
defs = [defs];
94+
}
9695

97-
if (type === 'hasMany') {
98-
task = DS.findAll(relationName, params, options);
99-
} else if (type === 'hasOne') {
100-
if (def.localKey && instance[def.localKey]) {
101-
task = DS.find(relationName, instance[def.localKey], options);
102-
} else if (def.foreignKey) {
96+
defs.forEach(function (def) {
97+
if (DS.utils.contains(relations, relationName)) {
98+
var task;
99+
var params = {};
100+
params[def.foreignKey] = instance[definition.idAttribute];
101+
102+
if (type === 'hasMany') {
103103
task = DS.findAll(relationName, params, options);
104+
} else if (type === 'hasOne') {
105+
if (def.localKey && instance[def.localKey]) {
106+
task = DS.find(relationName, instance[def.localKey], options);
107+
} else if (def.foreignKey) {
108+
task = DS.findAll(relationName, params, options);
109+
}
110+
} else {
111+
task = DS.find(relationName, instance[def.localKey], options);
104112
}
105-
} else {
106-
task = DS.find(relationName, instance[def.localKey], options);
107-
}
108113

109-
if (task) {
110-
tasks.push(task);
111-
fields.push(def.localField);
114+
if (task) {
115+
tasks.push(task);
116+
fields.push(def.localField);
117+
}
112118
}
113-
}
119+
});
114120
});
115121
});
116122

src/datastore/sync_methods/inject.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,21 @@ function _inject(definition, resource, attrs) {
115115

116116
function _injectRelations(definition, injected) {
117117
var DS = this;
118-
DS.utils.forOwn(definition.relations, function (relation, type) {
119-
DS.utils.forOwn(relation, function (def, relationName) {
120-
if (DS.definitions[relationName] && injected[def.localField]) {
121-
try {
122-
injected[def.localField] = DS.inject(relationName, injected[def.localField]);
123-
} catch (err) {
124-
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + type + ' relation: "' + relationName + '"!', err);
125-
}
118+
DS.utils.forOwn(definition.relations, function (relatedModels, type) {
119+
DS.utils.forOwn(relatedModels, function (defs, relationName) {
120+
if (!DS.utils.isArray(defs)) {
121+
defs = [defs];
126122
}
123+
124+
defs.forEach(function (def) {
125+
if (DS.definitions[relationName] && injected[def.localField]) {
126+
try {
127+
injected[def.localField] = DS.inject(relationName, injected[def.localField]);
128+
} catch (err) {
129+
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + type + ' relation: "' + relationName + '"!', err);
130+
}
131+
}
132+
});
127133
});
128134
});
129135
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
6969
}, fail);
7070

7171
$httpBackend.flush();
72+
73+
// try a comment that has a belongsTo relationship to multiple users:
74+
DS.inject('comment', comment19);
75+
$httpBackend.expectGET('http://test.angular-cache.com/user/20').respond(200, user20);
76+
$httpBackend.expectGET('http://test.angular-cache.com/user/19').respond(200, user19);
77+
DS.loadRelations('comment', 19, ['user']).then(function (comment) {
78+
assert.deepEqual(comment.user, user20);
79+
assert.deepEqual(comment.approvedByUser, user19);
80+
}, fail);
81+
$httpBackend.flush();
82+
7283
});
7384
it('should get an item from the server but not store it if cacheResponse is false', function () {
7485
DS.inject('user', {

test/integration/datastore/sync_methods/inject.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ describe('DS.inject(resourceName, attrs)', function () {
185185

186186
// comment19 relations
187187
assert.deepEqual(DS.get('user', 20), user20);
188+
assert.deepEqual(DS.get('user', 19), user19);
188189

189190
// profile21 relations
190191
assert.deepEqual(DS.get('user', 22), user22);

0 commit comments

Comments
 (0)