@@ -159,27 +159,33 @@ function loadWithRelations (items, resourceConfig, options) {
159
159
} )
160
160
} else if ( def . type === 'belongsTo' || ( def . type === 'hasOne' && def . localKey ) ) {
161
161
if ( instance ) {
162
- task = this . find ( resourceConfig . getResource ( relationName ) , DSUtils . get ( instance , def . localKey ) , __options ) . then ( relatedItem => {
163
- instance [ def . localField ] = relatedItem
164
- return relatedItem
165
- } )
162
+ let id = DSUtils . get ( instance , def . localKey )
163
+ if ( id ) {
164
+ task = this . find ( resourceConfig . getResource ( relationName ) , DSUtils . get ( instance , def . localKey ) , __options ) . then ( relatedItem => {
165
+ instance [ def . localField ] = relatedItem
166
+ return relatedItem
167
+ } )
168
+ }
166
169
} else {
167
- task = this . findAll ( resourceConfig . getResource ( relationName ) , {
168
- where : {
169
- [ relationDef . idAttribute ] : {
170
- 'in' : DSUtils . filter ( items . map ( function ( item ) { return DSUtils . get ( item , def . localKey ) } ) , x => x )
171
- }
172
- }
173
- } , __options ) . then ( relatedItems => {
174
- DSUtils . forEach ( items , item => {
175
- DSUtils . forEach ( relatedItems , relatedItem => {
176
- if ( relatedItem [ relationDef . idAttribute ] === item [ def . localKey ] ) {
177
- item [ def . localField ] = relatedItem
170
+ let ids = DSUtils . filter ( items . map ( function ( item ) { return DSUtils . get ( item , def . localKey ) } ) , x => x )
171
+ if ( ids . length ) {
172
+ task = this . findAll ( resourceConfig . getResource ( relationName ) , {
173
+ where : {
174
+ [ relationDef . idAttribute ] : {
175
+ 'in' : ids
178
176
}
177
+ }
178
+ } , __options ) . then ( relatedItems => {
179
+ DSUtils . forEach ( items , item => {
180
+ DSUtils . forEach ( relatedItems , relatedItem => {
181
+ if ( relatedItem [ relationDef . idAttribute ] === item [ def . localKey ] ) {
182
+ item [ def . localField ] = relatedItem
183
+ }
184
+ } )
179
185
} )
186
+ return relatedItems
180
187
} )
181
- return relatedItems
182
- } )
188
+ }
183
189
}
184
190
}
185
191
0 commit comments