@@ -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) {
64806484function _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
0 commit comments