diff --git a/karma.start.js b/karma.start.js index fed372f..91bd1e6 100644 --- a/karma.start.js +++ b/karma.start.js @@ -2,7 +2,7 @@ var $rootScope, $q, $log, $timeout, DSHttpAdapterProvider, DSProvider, DSLocalStorageAdapter, DS, DSUtils, DSHttpAdapter, app, $httpBackend, p1, p2, p3, p4, p5; var user1, organization2, comment3, profile4; -var comment11, comment12, comment13, organization14, profile15, user10, user16, user17, user18, organization15, user20, comment19, user22, profile21; +var comment11, comment12, comment13, organization14, profile15, user10, user16, user17, user18, organization15, user19, user20, comment19, user22, profile21; var lifecycle = {}; @@ -184,10 +184,16 @@ function startInjector() { name: 'comment', relations: { belongsTo: { - user: { - localField: 'user', - localKey: 'userId' - } + user: [ + { + localField: 'user', + localKey: 'userId' + }, + { + localField: 'approvedByUser', + localKey: 'approvedBy' + } + ] } } }); @@ -294,6 +300,10 @@ function startInjector() { user18 ] }; + user19 = { + id: 19, + name: 'test user 19' + }; user20 = { id: 20, name: 'test user 20' @@ -301,6 +311,8 @@ function startInjector() { comment19 = { content: 'test comment 19', id: 19, + approvedBy: 19, + approvedByUser: user19, userId: 20, user: user20 }; diff --git a/src/datastore/async_methods/loadRelations.js b/src/datastore/async_methods/loadRelations.js index 26c1824..cf5e273 100644 --- a/src/datastore/async_methods/loadRelations.js +++ b/src/datastore/async_methods/loadRelations.js @@ -87,30 +87,36 @@ function loadRelations(resourceName, instance, relations, options) { var tasks = []; var fields = []; - DS.utils.forOwn(definition.relations, function (relation, type) { - DS.utils.forOwn(relation, function (def, relationName) { - if (DS.utils.contains(relations, relationName)) { - var task; - var params = {}; - params[def.foreignKey] = instance[definition.idAttribute]; + DS.utils.forOwn(definition.relations, function (relatedModels, type) { + DS.utils.forOwn(relatedModels, function (defs, relationName) { + if (!DS.utils.isArray(defs)) { + defs = [defs]; + } - if (type === 'hasMany') { - task = DS.findAll(relationName, params, options); - } else if (type === 'hasOne') { - if (def.localKey && instance[def.localKey]) { - task = DS.find(relationName, instance[def.localKey], options); - } else if (def.foreignKey) { + defs.forEach(function (def) { + if (DS.utils.contains(relations, relationName)) { + var task; + var params = {}; + params[def.foreignKey] = instance[definition.idAttribute]; + + if (type === 'hasMany') { task = DS.findAll(relationName, params, options); + } else if (type === 'hasOne') { + if (def.localKey && instance[def.localKey]) { + task = DS.find(relationName, instance[def.localKey], options); + } else if (def.foreignKey) { + task = DS.findAll(relationName, params, options); + } + } else { + task = DS.find(relationName, instance[def.localKey], options); } - } else { - task = DS.find(relationName, instance[def.localKey], options); - } - if (task) { - tasks.push(task); - fields.push(def.localField); + if (task) { + tasks.push(task); + fields.push(def.localField); + } } - } + }); }); }); diff --git a/src/datastore/sync_methods/inject.js b/src/datastore/sync_methods/inject.js index 7dc75b3..2ae105e 100644 --- a/src/datastore/sync_methods/inject.js +++ b/src/datastore/sync_methods/inject.js @@ -115,15 +115,21 @@ function _inject(definition, resource, attrs) { function _injectRelations(definition, injected) { var DS = this; - DS.utils.forOwn(definition.relations, function (relation, type) { - DS.utils.forOwn(relation, function (def, relationName) { - if (DS.definitions[relationName] && injected[def.localField]) { - try { - injected[def.localField] = DS.inject(relationName, injected[def.localField]); - } catch (err) { - DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + type + ' relation: "' + relationName + '"!', err); - } + DS.utils.forOwn(definition.relations, function (relatedModels, type) { + DS.utils.forOwn(relatedModels, function (defs, relationName) { + if (!DS.utils.isArray(defs)) { + defs = [defs]; } + + defs.forEach(function (def) { + if (DS.definitions[relationName] && injected[def.localField]) { + try { + injected[def.localField] = DS.inject(relationName, injected[def.localField]); + } catch (err) { + DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + type + ' relation: "' + relationName + '"!', err); + } + } + }); }); }); } diff --git a/test/integration/datastore/async_methods/loadRelations.test.js b/test/integration/datastore/async_methods/loadRelations.test.js index 4d46b87..c3868a0 100644 --- a/test/integration/datastore/async_methods/loadRelations.test.js +++ b/test/integration/datastore/async_methods/loadRelations.test.js @@ -69,6 +69,17 @@ describe('DS.loadRelations(resourceName, instance(Id), relations[, options]): ', }, fail); $httpBackend.flush(); + + // try a comment that has a belongsTo relationship to multiple users: + DS.inject('comment', comment19); + $httpBackend.expectGET('http://test.angular-cache.com/user/20').respond(200, user20); + $httpBackend.expectGET('http://test.angular-cache.com/user/19').respond(200, user19); + DS.loadRelations('comment', 19, ['user']).then(function (comment) { + assert.deepEqual(comment.user, user20); + assert.deepEqual(comment.approvedByUser, user19); + }, fail); + $httpBackend.flush(); + }); it('should get an item from the server but not store it if cacheResponse is false', function () { DS.inject('user', { diff --git a/test/integration/datastore/sync_methods/inject.test.js b/test/integration/datastore/sync_methods/inject.test.js index d75d038..5d88c12 100644 --- a/test/integration/datastore/sync_methods/inject.test.js +++ b/test/integration/datastore/sync_methods/inject.test.js @@ -185,6 +185,7 @@ describe('DS.inject(resourceName, attrs)', function () { // comment19 relations assert.deepEqual(DS.get('user', 20), user20); + assert.deepEqual(DS.get('user', 19), user19); // profile21 relations assert.deepEqual(DS.get('user', 22), user22);