11/**
22* @author Jason Dobry <jason.dobry@gmail.com>
33* @file angular-data.js
4- * @version 1.0.0-beta.1 - Homepage <http://angular-data.pseudobry.com/>
4+ * @version 1.0.0-beta.2 - Homepage <http://angular-data.pseudobry.com/>
55* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
66* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
77*
@@ -5418,6 +5418,11 @@ module.exports = {
54185418
54195419} , { "./bindAll" :50 , "./bindOne" :51 , "./changes" :52 , "./createInstance" :53 , "./defineResource" :54 , "./digest" :55 , "./eject" :56 , "./ejectAll" :57 , "./filter" :58 , "./get" :59 , "./hasChanges" :60 , "./inject" :62 , "./lastModified" :63 , "./lastSaved" :64 , "./previous" :65 } ] , 62 :[ function ( require , module , exports ) {
54205420var observe = require ( '../../../lib/observe-js/observe-js' ) ;
5421+ var stack = 0 ;
5422+ var data = {
5423+ injectedSoFar : { }
5424+ } ;
5425+
54215426function errorPrefix ( resourceName ) {
54225427 return 'DS.inject(' + resourceName + ', attrs[, options]): ' ;
54235428}
@@ -5532,7 +5537,7 @@ function _inject(definition, resource, attrs) {
55325537 return injected ;
55335538}
55345539
5535- function _injectRelations ( definition , injected , options ) {
5540+ function _injectRelations ( definition , injected , options , injectedSoFar ) {
55365541 var DS = this ;
55375542 DS . utils . forOwn ( definition . relations , function ( relatedModels , type ) {
55385543 DS . utils . forOwn ( relatedModels , function ( defs , relationName ) {
@@ -5541,9 +5546,10 @@ function _injectRelations(definition, injected, options) {
55415546 }
55425547
55435548 function _process ( def , injected ) {
5544- if ( DS . definitions [ relationName ] && injected [ def . localField ] ) {
5549+ if ( DS . definitions [ relationName ] && injected [ def . localField ] && ! injectedSoFar [ relationName + injected [ def . localField ] [ DS . definitions [ relationName ] . idAttribute ] ] ) {
55455550 try {
5546- injected [ def . localField ] = DS . inject ( relationName , injected [ def . localField ] , options ) ;
5551+ injectedSoFar [ relationName + injected [ def . localField ] [ DS . definitions [ relationName ] . idAttribute ] ] = 1 ;
5552+ injected [ def . localField ] = DS . inject ( relationName , injected [ def . localField ] , options , injectedSoFar ) ;
55475553 } catch ( err ) {
55485554 DS . $log . error ( errorPrefix ( definition . name ) + 'Failed to inject ' + type + ' relation: "' + relationName + '"!' , err ) ;
55495555 }
@@ -5631,39 +5637,52 @@ function _injectRelations(definition, injected, options) {
56315637 * @returns {object|array } A reference to the item that was injected into the data store or an array of references to
56325638 * the items that were injected into the data store.
56335639 */
5634- function inject ( resourceName , attrs , options ) {
5640+ function inject ( resourceName , attrs , options , injectedSoFar ) {
56355641 var DS = this ;
56365642 var IA = DS . errors . IA ;
5643+ var definition = DS . definitions [ resourceName ] ;
56375644
56385645 options = options || { } ;
56395646
5640- if ( ! DS . definitions [ resourceName ] ) {
5647+ if ( ! definition ) {
56415648 throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
56425649 } else if ( ! DS . utils . isObject ( attrs ) && ! DS . utils . isArray ( attrs ) ) {
56435650 throw new IA ( errorPrefix ( resourceName ) + 'attrs: Must be an object or an array!' ) ;
56445651 } else if ( ! DS . utils . isObject ( options ) ) {
56455652 throw new IA ( errorPrefix ( resourceName ) + 'options: Must be an object!' ) ;
56465653 }
5647- var definition = DS . definitions [ resourceName ] ;
56485654 var resource = DS . store [ resourceName ] ;
56495655 var injected ;
56505656
56515657 if ( ! ( 'findBelongsTo' in options ) ) {
56525658 options . findBelongsTo = true ;
56535659 }
56545660
5655- if ( ! DS . $rootScope . $$phase ) {
5656- DS . $rootScope . $apply ( function ( ) {
5657- injected = _inject . call ( DS , definition , resource , attrs ) ;
5658- } ) ;
5659- } else {
5660- injected = _inject . call ( DS , definition , resource , attrs ) ;
5661- }
5662- if ( definition . relations ) {
5663- _injectRelations . call ( DS , definition , injected , options ) ;
5661+ stack ++ ;
5662+
5663+ try {
5664+ if ( ! DS . $rootScope . $$phase ) {
5665+ DS . $rootScope . $apply ( function ( ) {
5666+ injected = _inject . call ( DS , definition , resource , attrs , injectedSoFar || data . injectedSoFar ) ;
5667+ } ) ;
5668+ } else {
5669+ injected = _inject . call ( DS , definition , resource , attrs , injectedSoFar || data . injectedSoFar ) ;
5670+ }
5671+ if ( definition . relations ) {
5672+ _injectRelations . call ( DS , definition , injected , options , injectedSoFar || data . injectedSoFar ) ;
5673+ }
5674+
5675+ DS . notify ( definition , 'inject' , injected ) ;
5676+
5677+ stack -- ;
5678+ } catch ( err ) {
5679+ stack -- ;
5680+ throw err ;
56645681 }
56655682
5666- DS . notify ( definition , 'inject' , injected ) ;
5683+ if ( ! stack ) {
5684+ data . injectedSoFar = { } ;
5685+ }
56675686
56685687 return injected ;
56695688}
@@ -5986,7 +6005,7 @@ module.exports = [function () {
59866005 * @id angular-data
59876006 * @name angular-data
59886007 * @description
5989- * __Version:__ 1.0.0-beta.1
6008+ * __Version:__ 1.0.0-beta.2
59906009 *
59916010 * ## Install
59926011 *
0 commit comments