@@ -13,13 +13,21 @@ function _inject(definition, resource, attrs) {
13
13
var DS = this ;
14
14
var $log = DS . $log ;
15
15
16
- function _react ( added , removed , changed , oldValueFn ) {
16
+ function _react ( added , removed , changed , oldValueFn , firstTime ) {
17
17
var target = this ;
18
18
var item ;
19
19
var innerId = ( oldValueFn && oldValueFn ( definition . idAttribute ) ) ? oldValueFn ( definition . idAttribute ) : target [ definition . idAttribute ] ;
20
20
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
+ }
23
31
24
32
if ( definition . computed ) {
25
33
item = DS . get ( definition . name , innerId ) ;
@@ -40,17 +48,10 @@ function _inject(definition, resource, attrs) {
40
48
41
49
if ( definition . relations ) {
42
50
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
+ }
54
55
} ) ;
55
56
}
56
57
@@ -109,7 +110,7 @@ function _inject(definition, resource, attrs) {
109
110
resource . observers [ id ] . open ( _react , item ) ;
110
111
resource . index . put ( id , item ) ;
111
112
112
- _react . call ( item , { } , { } , { } ) ;
113
+ _react . call ( item , { } , { } , { } , null , true ) ;
113
114
} else {
114
115
DS . utils . deepMixIn ( item , attrs ) ;
115
116
if ( typeof resource . index . touch === 'function' ) {
@@ -133,57 +134,43 @@ function _inject(definition, resource, attrs) {
133
134
134
135
function _injectRelations ( definition , injected , options ) {
135
136
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
- }
141
137
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 ] ) ;
175
162
}
163
+ }
164
+ }
176
165
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 ) ;
185
170
} ) ;
186
- } ) ;
171
+ } else {
172
+ _process ( def , def . relation , injected ) ;
173
+ }
187
174
} ) ;
188
175
}
189
176
@@ -236,6 +223,7 @@ function _injectRelations(definition, injected, options) {
236
223
*
237
224
* - `{boolean=}` - `findBelongsTo` - Find and attach any existing "belongsTo" relationships to the newly injected item. Potentially expensive if enabled. Default: `false`.
238
225
* - `{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`.
239
227
* - `{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`.
240
228
*
241
229
* @returns {object|array } A reference to the item that was injected into the data store or an array of references to
0 commit comments