@@ -13,13 +13,21 @@ function _inject(definition, resource, attrs) {
1313 var DS = this ;
1414 var $log = DS . $log ;
1515
16- function _react ( added , removed , changed , oldValueFn ) {
16+ function _react ( added , removed , changed , oldValueFn , firstTime ) {
1717 var target = this ;
1818 var item ;
1919 var innerId = ( oldValueFn && oldValueFn ( definition . idAttribute ) ) ? oldValueFn ( definition . idAttribute ) : target [ definition . idAttribute ] ;
2020
21- resource . modified [ innerId ] = DS . utils . updateTimestamp ( resource . modified [ innerId ] ) ;
22- resource . collectionModified = DS . utils . updateTimestamp ( resource . collectionModified ) ;
21+ DS . utils . forEach ( definition . relationFields , function ( field ) {
22+ delete added [ field ] ;
23+ delete removed [ field ] ;
24+ delete changed [ field ] ;
25+ } ) ;
26+
27+ if ( ! DS . utils . isEmpty ( added ) || ! DS . utils . isEmpty ( removed ) || ! DS . utils . isEmpty ( changed ) || firstTime ) {
28+ resource . modified [ innerId ] = DS . utils . updateTimestamp ( resource . modified [ innerId ] ) ;
29+ resource . collectionModified = DS . utils . updateTimestamp ( resource . collectionModified ) ;
30+ }
2331
2432 if ( definition . computed ) {
2533 item = DS . get ( definition . name , innerId ) ;
@@ -40,17 +48,10 @@ function _inject(definition, resource, attrs) {
4048
4149 if ( definition . relations ) {
4250 item = DS . get ( definition . name , innerId ) ;
43- DS . utils . forOwn ( definition . relations , function ( relatedModels ) {
44- DS . utils . forOwn ( relatedModels , function ( defs , relationName ) {
45- if ( ! DS . utils . isArray ( defs ) ) {
46- defs = [ defs ] ;
47- }
48- defs . forEach ( function ( def ) {
49- if ( item [ def . localField ] && ( def . localKey in added || def . localKey in removed || def . localKey in changed ) ) {
50- DS . link ( definition . name , item [ definition . idAttribute ] , [ relationName ] ) ;
51- }
52- } ) ;
53- } ) ;
51+ DS . utils . forEach ( definition . relationList , function ( def ) {
52+ if ( item [ def . localField ] && ( def . localKey in added || def . localKey in removed || def . localKey in changed ) ) {
53+ DS . link ( definition . name , item [ definition . idAttribute ] , [ def . relation ] ) ;
54+ }
5455 } ) ;
5556 }
5657
@@ -109,7 +110,7 @@ function _inject(definition, resource, attrs) {
109110 resource . observers [ id ] . open ( _react , item ) ;
110111 resource . index . put ( id , item ) ;
111112
112- _react . call ( item , { } , { } , { } ) ;
113+ _react . call ( item , { } , { } , { } , null , true ) ;
113114 } else {
114115 DS . utils . deepMixIn ( item , attrs ) ;
115116 if ( typeof resource . index . touch === 'function' ) {
@@ -133,57 +134,43 @@ function _inject(definition, resource, attrs) {
133134
134135function _injectRelations ( definition , injected , options ) {
135136 var DS = this ;
136- DS . utils . forOwn ( definition . relations , function ( relatedModels , type ) {
137- DS . utils . forOwn ( relatedModels , function ( defs , relationName ) {
138- if ( ! DS . utils . isArray ( defs ) ) {
139- defs = [ defs ] ;
140- }
141137
142- function _process ( def , injected ) {
143- if ( DS . definitions [ relationName ] && injected [ def . localField ] && ! data . injectedSoFar [ relationName + injected [ def . localField ] [ DS . definitions [ relationName ] . idAttribute ] ] ) {
144- try {
145- data . injectedSoFar [ relationName + injected [ def . localField ] [ DS . definitions [ relationName ] . idAttribute ] ] = 1 ;
146- injected [ def . localField ] = DS . inject ( relationName , injected [ def . localField ] , options ) ;
147- } catch ( err ) {
148- DS . $log . error ( errorPrefix ( definition . name ) + 'Failed to inject ' + type + ' relation: "' + relationName + '"!' , err ) ;
149- }
150- } else if ( options . findBelongsTo && type === 'belongsTo' ) {
151- if ( DS . utils . isArray ( injected ) ) {
152- DS . utils . forEach ( injected , function ( injectedItem ) {
153- DS . link ( definition . name , injectedItem [ definition . idAttribute ] , [ relationName ] ) ;
154- } ) ;
155- } else {
156- DS . link ( definition . name , injected [ definition . idAttribute ] , [ relationName ] ) ;
157- }
158- } else if ( options . findHasMany && type === 'hasMany' ) {
159- if ( DS . utils . isArray ( injected ) ) {
160- DS . utils . forEach ( injected , function ( injectedItem ) {
161- DS . link ( definition . name , injectedItem [ definition . idAttribute ] , [ relationName ] ) ;
162- } ) ;
163- } else {
164- DS . link ( definition . name , injected [ definition . idAttribute ] , [ relationName ] ) ;
165- }
166- } else if ( options . findHasOne && type === 'hasOne' ) {
167- if ( DS . utils . isArray ( injected ) ) {
168- DS . utils . forEach ( injected , function ( injectedItem ) {
169- DS . link ( definition . name , injectedItem [ definition . idAttribute ] , [ relationName ] ) ;
170- } ) ;
171- } else {
172- DS . link ( definition . name , injected [ definition . idAttribute ] , [ relationName ] ) ;
173- }
174- }
138+ function _process ( def , relationName , injected ) {
139+ var relationDef = DS . definitions [ relationName ] ;
140+ if ( relationDef && injected [ def . localField ] && ! data . injectedSoFar [ relationName + injected [ def . localField ] [ relationDef . idAttribute ] ] ) {
141+ try {
142+ data . injectedSoFar [ relationName + injected [ def . localField ] [ relationDef . idAttribute ] ] = 1 ;
143+ injected [ def . localField ] = DS . inject ( relationName , injected [ def . localField ] , options ) ;
144+ } catch ( err ) {
145+ DS . $log . error ( errorPrefix ( definition . name ) + 'Failed to inject ' + def . type + ' relation: "' + relationName + '"!' , err ) ;
146+ }
147+ } else if ( options . findBelongsTo && def . type === 'belongsTo' ) {
148+ if ( DS . utils . isArray ( injected ) ) {
149+ DS . utils . forEach ( injected , function ( injectedItem ) {
150+ DS . link ( definition . name , injectedItem [ definition . idAttribute ] , [ relationName ] ) ;
151+ } ) ;
152+ } else {
153+ DS . link ( definition . name , injected [ definition . idAttribute ] , [ relationName ] ) ;
154+ }
155+ } else if ( ( options . findHasMany && def . type === 'hasMany' ) || ( options . findHasOne && def . type === 'hasOne' ) ) {
156+ if ( DS . utils . isArray ( injected ) ) {
157+ DS . utils . forEach ( injected , function ( injectedItem ) {
158+ DS . link ( definition . name , injectedItem [ definition . idAttribute ] , [ relationName ] ) ;
159+ } ) ;
160+ } else {
161+ DS . link ( definition . name , injected [ definition . idAttribute ] , [ relationName ] ) ;
175162 }
163+ }
164+ }
176165
177- defs . forEach ( function ( def ) {
178- if ( DS . utils . isArray ( injected ) ) {
179- DS . utils . forEach ( injected , function ( injectedI ) {
180- _process ( def , injectedI ) ;
181- } ) ;
182- } else {
183- _process ( def , injected ) ;
184- }
166+ DS . utils . forEach ( definition . relationList , function ( def ) {
167+ if ( DS . utils . isArray ( injected ) ) {
168+ DS . utils . forEach ( injected , function ( injectedI ) {
169+ _process ( def , def . relation , injectedI ) ;
185170 } ) ;
186- } ) ;
171+ } else {
172+ _process ( def , def . relation , injected ) ;
173+ }
187174 } ) ;
188175}
189176
@@ -236,6 +223,7 @@ function _injectRelations(definition, injected, options) {
236223 *
237224 * - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
238225 * - `{boolean=}` - `findHasMany` - Find and attach any existing "hasMany" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
226+ * - `{boolean=}` - `findHasOne` - Find and attach any existing "hasOne" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
239227 * - `{boolean=}` - `linkInverse` - Look in the data store for relations of the injected item(s) and update their links to the injected. Potentially expensive if enabled. Default: `false`.
240228 *
241229 * @returns {object|array } A reference to the item that was injected into the data store or an array of references to
0 commit comments