Skip to content

Commit ffc3e05

Browse files
committed
Fixes #188.
1 parent 42acdad commit ffc3e05

File tree

5 files changed

+68
-85
lines changed

5 files changed

+68
-85
lines changed

dist/angular-data.js

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6446,6 +6446,10 @@ function _inject(definition, resource, attrs, options) {
64466446
resource.index.put(id, item);
64476447

64486448
_react.call(item, {}, {}, {}, null, true);
6449+
6450+
if (definition.relations) {
6451+
_injectRelations.call(DS, definition, item, options);
6452+
}
64496453
} else {
64506454
DS.utils.deepMixIn(item, attrs);
64516455
if (definition.resetHistoryOnInject) {
@@ -6480,41 +6484,15 @@ function _inject(definition, resource, attrs, options) {
64806484
function _injectRelations(definition, injected, options) {
64816485
var DS = this;
64826486

6483-
function _process(def, relationName, injected) {
6487+
DS.utils.forEach(definition.relationList, function (def) {
6488+
var relationName = def.relation;
64846489
var relationDef = DS.definitions[relationName];
6485-
if (relationDef && injected[def.localField] && !data.injectedSoFar[relationName + injected[def.localField][relationDef.idAttribute]]) {
6490+
if (relationDef && injected[def.localField]) {
64866491
try {
6487-
data.injectedSoFar[relationName + injected[def.localField][relationDef.idAttribute]] = 1;
64886492
injected[def.localField] = DS.inject(relationName, injected[def.localField], options);
64896493
} catch (err) {
64906494
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + def.type + ' relation: "' + relationName + '"!', err);
64916495
}
6492-
} else if (options.findBelongsTo && def.type === 'belongsTo') {
6493-
if (DS.utils.isArray(injected)) {
6494-
DS.utils.forEach(injected, function (injectedItem) {
6495-
DS.link(definition.name, injectedItem[definition.idAttribute], [relationName]);
6496-
});
6497-
} else {
6498-
DS.link(definition.name, injected[definition.idAttribute], [relationName]);
6499-
}
6500-
} else if ((options.findHasMany && def.type === 'hasMany') || (options.findHasOne && def.type === 'hasOne')) {
6501-
if (DS.utils.isArray(injected)) {
6502-
DS.utils.forEach(injected, function (injectedItem) {
6503-
DS.link(definition.name, injectedItem[definition.idAttribute], [relationName]);
6504-
});
6505-
} else {
6506-
DS.link(definition.name, injected[definition.idAttribute], [relationName]);
6507-
}
6508-
}
6509-
}
6510-
6511-
DS.utils.forEach(definition.relationList, function (def) {
6512-
if (DS.utils.isArray(injected)) {
6513-
DS.utils.forEach(injected, function (injectedI) {
6514-
_process(def, def.relation, injectedI);
6515-
});
6516-
} else {
6517-
_process(def, def.relation, injected);
65186496
}
65196497
});
65206498
}
@@ -6592,8 +6570,6 @@ function inject(resourceName, attrs, options) {
65926570
var resource = DS.store[resourceName];
65936571
var injected;
65946572

6595-
stack++;
6596-
65976573
try {
65986574
if (!('useClass' in options)) {
65996575
options.useClass = definition.useClass;
@@ -6605,9 +6581,6 @@ function inject(resourceName, attrs, options) {
66056581
} else {
66066582
injected = _inject.call(DS, definition, resource, attrs, options);
66076583
}
6608-
if (definition.relations) {
6609-
_injectRelations.call(DS, definition, injected, options);
6610-
}
66116584

66126585
if (options.linkInverse) {
66136586
if (DS.utils.isArray(injected) && injected.length) {
@@ -6617,18 +6590,32 @@ function inject(resourceName, attrs, options) {
66176590
}
66186591
}
66196592

6593+
if (DS.utils.isArray(injected)) {
6594+
DS.utils.forEach(injected, function (injectedI) {
6595+
DS.utils.forEach(definition.relationList, function (def) {
6596+
if (options.findBelongsTo && def.type === 'belongsTo' && injectedI[definition.idAttribute]) {
6597+
DS.link(definition.name, injectedI[definition.idAttribute], [def.relation]);
6598+
} else if ((options.findHasMany && def.type === 'hasMany') || (options.findHasOne && def.type === 'hasOne')) {
6599+
DS.link(definition.name, injectedI[definition.idAttribute], [def.relation]);
6600+
}
6601+
});
6602+
});
6603+
} else {
6604+
DS.utils.forEach(definition.relationList, function (def) {
6605+
if (options.findBelongsTo && def.type === 'belongsTo' && injected[definition.idAttribute]) {
6606+
DS.link(definition.name, injected[definition.idAttribute], [def.relation]);
6607+
} else if ((options.findHasMany && def.type === 'hasMany') || (options.findHasOne && def.type === 'hasOne')) {
6608+
DS.link(definition.name, injected[definition.idAttribute], [def.relation]);
6609+
}
6610+
});
6611+
}
6612+
66206613
DS.notify(definition, 'inject', injected);
66216614

6622-
stack--;
66236615
} catch (err) {
6624-
stack--;
66256616
throw err;
66266617
}
66276618

6628-
if (!stack) {
6629-
data.injectedSoFar = {};
6630-
}
6631-
66326619
return injected;
66336620
}
66346621

dist/angular-data.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datastore/sync_methods/inject.js

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ function _inject(definition, resource, attrs, options) {
125125
resource.index.put(id, item);
126126

127127
_react.call(item, {}, {}, {}, null, true);
128+
129+
if (definition.relations) {
130+
_injectRelations.call(DS, definition, item, options);
131+
}
128132
} else {
129133
DS.utils.deepMixIn(item, attrs);
130134
if (definition.resetHistoryOnInject) {
@@ -159,41 +163,15 @@ function _inject(definition, resource, attrs, options) {
159163
function _injectRelations(definition, injected, options) {
160164
var DS = this;
161165

162-
function _process(def, relationName, injected) {
166+
DS.utils.forEach(definition.relationList, function (def) {
167+
var relationName = def.relation;
163168
var relationDef = DS.definitions[relationName];
164-
if (relationDef && injected[def.localField] && !data.injectedSoFar[relationName + injected[def.localField][relationDef.idAttribute]]) {
169+
if (relationDef && injected[def.localField]) {
165170
try {
166-
data.injectedSoFar[relationName + injected[def.localField][relationDef.idAttribute]] = 1;
167171
injected[def.localField] = DS.inject(relationName, injected[def.localField], options);
168172
} catch (err) {
169173
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + def.type + ' relation: "' + relationName + '"!', err);
170174
}
171-
} else if (options.findBelongsTo && def.type === 'belongsTo') {
172-
if (DS.utils.isArray(injected)) {
173-
DS.utils.forEach(injected, function (injectedItem) {
174-
DS.link(definition.name, injectedItem[definition.idAttribute], [relationName]);
175-
});
176-
} else {
177-
DS.link(definition.name, injected[definition.idAttribute], [relationName]);
178-
}
179-
} else if ((options.findHasMany && def.type === 'hasMany') || (options.findHasOne && def.type === 'hasOne')) {
180-
if (DS.utils.isArray(injected)) {
181-
DS.utils.forEach(injected, function (injectedItem) {
182-
DS.link(definition.name, injectedItem[definition.idAttribute], [relationName]);
183-
});
184-
} else {
185-
DS.link(definition.name, injected[definition.idAttribute], [relationName]);
186-
}
187-
}
188-
}
189-
190-
DS.utils.forEach(definition.relationList, function (def) {
191-
if (DS.utils.isArray(injected)) {
192-
DS.utils.forEach(injected, function (injectedI) {
193-
_process(def, def.relation, injectedI);
194-
});
195-
} else {
196-
_process(def, def.relation, injected);
197175
}
198176
});
199177
}
@@ -271,8 +249,6 @@ function inject(resourceName, attrs, options) {
271249
var resource = DS.store[resourceName];
272250
var injected;
273251

274-
stack++;
275-
276252
try {
277253
if (!('useClass' in options)) {
278254
options.useClass = definition.useClass;
@@ -284,9 +260,6 @@ function inject(resourceName, attrs, options) {
284260
} else {
285261
injected = _inject.call(DS, definition, resource, attrs, options);
286262
}
287-
if (definition.relations) {
288-
_injectRelations.call(DS, definition, injected, options);
289-
}
290263

291264
if (options.linkInverse) {
292265
if (DS.utils.isArray(injected) && injected.length) {
@@ -296,18 +269,32 @@ function inject(resourceName, attrs, options) {
296269
}
297270
}
298271

272+
if (DS.utils.isArray(injected)) {
273+
DS.utils.forEach(injected, function (injectedI) {
274+
DS.utils.forEach(definition.relationList, function (def) {
275+
if (options.findBelongsTo && def.type === 'belongsTo' && injectedI[definition.idAttribute]) {
276+
DS.link(definition.name, injectedI[definition.idAttribute], [def.relation]);
277+
} else if ((options.findHasMany && def.type === 'hasMany') || (options.findHasOne && def.type === 'hasOne')) {
278+
DS.link(definition.name, injectedI[definition.idAttribute], [def.relation]);
279+
}
280+
});
281+
});
282+
} else {
283+
DS.utils.forEach(definition.relationList, function (def) {
284+
if (options.findBelongsTo && def.type === 'belongsTo' && injected[definition.idAttribute]) {
285+
DS.link(definition.name, injected[definition.idAttribute], [def.relation]);
286+
} else if ((options.findHasMany && def.type === 'hasMany') || (options.findHasOne && def.type === 'hasOne')) {
287+
DS.link(definition.name, injected[definition.idAttribute], [def.relation]);
288+
}
289+
});
290+
}
291+
299292
DS.notify(definition, 'inject', injected);
300293

301-
stack--;
302294
} catch (err) {
303-
stack--;
304295
throw err;
305296
}
306297

307-
if (!stack) {
308-
data.injectedSoFar = {};
309-
}
310-
311298
return injected;
312299
}
313300

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
146146
$httpBackend.expectGET('http://test.angular-cache.com/organization/14/user').respond(200, [copy]);
147147

148148
DS.loadRelations('organization', organization, ['user']).then(function (organization) {
149-
assert.isTrue(organization === organization.users[0].organization);
149+
assert.equal(organization.users[0].id, 10);
150150

151151
$httpBackend.expectGET('http://test.angular-cache.com/user/10/comment').respond(200, [comment11, comment12]);
152152

@@ -157,7 +157,8 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
157157
}, function () {
158158
fail('Should not have succeeded!');
159159
});
160-
}, function () {
160+
}, function (err) {
161+
console.log(err.stack);
161162
fail('Should not have succeeded!');
162163
});
163164

@@ -176,7 +177,7 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ',
176177
]);
177178

178179
DS.loadRelations('organization', organization, ['user']).then(function (organization) {
179-
assert.isTrue(organization === organization.users[0].organization);
180+
assert.equal(organization.users[0].id, 1);
180181

181182
$httpBackend.expectGET('http://test.angular-cache.com/user/1/comment').respond(200, [
182183
{

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,5 +267,13 @@ describe('DS.inject(resourceName, attrs[, options])', function () {
267267
assert.equal(injected[0].children[0].children[1].id, 5);
268268
assert.equal(injected[0].children[1].children[0].id, 6);
269269
assert.equal(injected[0].children[1].children[1].id, 7);
270+
271+
assert.isDefined(DS.get('foo', 1));
272+
assert.isDefined(DS.get('foo', 2));
273+
assert.isDefined(DS.get('foo', 3));
274+
assert.isDefined(DS.get('foo', 4));
275+
assert.isDefined(DS.get('foo', 5));
276+
assert.isDefined(DS.get('foo', 6));
277+
assert.isDefined(DS.get('foo', 7));
270278
});
271279
});

0 commit comments

Comments
 (0)