@@ -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
0 commit comments