|
277 | 277 |
|
278 | 278 | // Map `val` if a transformation function is provided.
|
279 | 279 | val = map ? map.call(this, val, collectionType ? collectionType : relatedModel) : val;
|
280 |
| - if(!val) { |
281 |
| - attributes[relationKey] = val; |
282 |
| - return; |
283 |
| - } |
284 |
| - |
285 |
| - // If `relation.type` is `Backbone.Many`, |
286 |
| - // Create `Backbone.Collection` with passed data and perform Backbone `set`. |
287 |
| - if (relation.type === Backbone.Many) { |
| 280 | + if (!isValuePresent(val)) { |
| 281 | + data = val; |
| 282 | + } else { |
| 283 | + // If `relation.type` is `Backbone.Many`, |
| 284 | + // Create `Backbone.Collection` with passed data and perform Backbone `set`. |
| 285 | + if (relation.type === Backbone.Many) { |
288 | 286 |
|
289 |
| - if (currVal) { |
290 |
| - // Setting this flag will prevent events from firing immediately. That way clients |
291 |
| - // will not get events until the entire object graph is updated. |
292 |
| - currVal._deferEvents = true; |
| 287 | + if (currVal) { |
| 288 | + // Setting this flag will prevent events from firing immediately. That way clients |
| 289 | + // will not get events until the entire object graph is updated. |
| 290 | + currVal._deferEvents = true; |
293 | 291 |
|
294 |
| - // Use Backbone.Collection's `reset` or smart `set` method |
295 |
| - currVal[relationOptions.reset ? 'reset' : 'set']( |
296 |
| - val instanceof BackboneCollection ? val.models : val, relationOptions); |
| 292 | + // Use Backbone.Collection's `reset` or smart `set` method |
| 293 | + currVal[relationOptions.reset ? 'reset' : 'set']( |
| 294 | + val instanceof BackboneCollection ? val.models : val, relationOptions); |
297 | 295 |
|
298 |
| - data = currVal; |
| 296 | + data = currVal; |
299 | 297 |
|
300 |
| - } else { |
301 |
| - newCtx = true; |
302 |
| - |
303 |
| - if (val instanceof BackboneCollection) { |
304 |
| - data = val; |
305 | 298 | } else {
|
306 |
| - data = this._createCollection( |
307 |
| - collectionType || BackboneCollection, |
308 |
| - relation.collectionOptions || (relatedModel ? {model: relatedModel} : {}) |
309 |
| - ); |
310 |
| - data[relationOptions.reset ? 'reset' : 'set'](val, relationOptions); |
| 299 | + newCtx = true; |
| 300 | + |
| 301 | + if (val instanceof BackboneCollection) { |
| 302 | + data = val; |
| 303 | + } else { |
| 304 | + data = this._createCollection( |
| 305 | + collectionType || BackboneCollection, |
| 306 | + relation.collectionOptions || (relatedModel ? {model: relatedModel} : {}) |
| 307 | + ); |
| 308 | + data[relationOptions.reset ? 'reset' : 'set'](val, relationOptions); |
| 309 | + } |
311 | 310 | }
|
312 |
| - } |
313 | 311 |
|
314 |
| - } else if (relation.type === Backbone.One) { |
315 |
| - |
316 |
| - var hasOwnProperty = (val instanceof BackboneModel) ? |
317 |
| - val.attributes.hasOwnProperty(idKey) : |
318 |
| - val.hasOwnProperty(idKey); |
319 |
| - var newIdKey = (val instanceof BackboneModel) ? |
320 |
| - val.attributes[idKey] : |
321 |
| - val[idKey]; |
322 |
| - |
323 |
| - //Is the passed in data for the same key? |
324 |
| - if (currVal && hasOwnProperty && |
325 |
| - currVal.attributes[idKey] === newIdKey) { |
326 |
| - // Setting this flag will prevent events from firing immediately. That way clients |
327 |
| - // will not get events until the entire object graph is updated. |
328 |
| - currVal._deferEvents = true; |
329 |
| - // Perform the traditional `set` operation |
330 |
| - currVal._set(val instanceof BackboneModel ? val.attributes : val, relationOptions); |
331 |
| - data = currVal; |
332 |
| - } else { |
333 |
| - newCtx = true; |
334 |
| - |
335 |
| - if (val instanceof BackboneModel) { |
336 |
| - data = val; |
| 312 | + } else if (relation.type === Backbone.One) { |
| 313 | + |
| 314 | + var hasOwnProperty = (val instanceof BackboneModel) ? |
| 315 | + val.attributes.hasOwnProperty(idKey) : |
| 316 | + val.hasOwnProperty(idKey); |
| 317 | + var newIdKey = (val instanceof BackboneModel) ? |
| 318 | + val.attributes[idKey] : |
| 319 | + val[idKey]; |
| 320 | + |
| 321 | + //Is the passed in data for the same key? |
| 322 | + if (currVal && hasOwnProperty && |
| 323 | + currVal.attributes[idKey] === newIdKey) { |
| 324 | + // Setting this flag will prevent events from firing immediately. That way clients |
| 325 | + // will not get events until the entire object graph is updated. |
| 326 | + currVal._deferEvents = true; |
| 327 | + // Perform the traditional `set` operation |
| 328 | + currVal._set(val instanceof BackboneModel ? val.attributes : val, relationOptions); |
| 329 | + data = currVal; |
337 | 330 | } else {
|
338 |
| - relationOptions.__parents__ = this; |
339 |
| - data = new relatedModel(val, relationOptions); |
340 |
| - delete relationOptions.__parents__; |
341 |
| - } |
| 331 | + newCtx = true; |
| 332 | + |
| 333 | + if (val instanceof BackboneModel) { |
| 334 | + data = val; |
| 335 | + } else { |
| 336 | + relationOptions.__parents__ = this; |
| 337 | + data = new relatedModel(val, relationOptions); |
| 338 | + delete relationOptions.__parents__; |
| 339 | + } |
342 | 340 |
|
| 341 | + } |
| 342 | + } else { |
| 343 | + throw new Error('type attribute must be specified and ' + |
| 344 | + 'have the values Backbone.One or Backbone.Many'); |
343 | 345 | }
|
344 |
| - } else { |
345 |
| - throw new Error('type attribute must be specified and ' + |
346 |
| - 'have the values Backbone.One or Backbone.Many'); |
347 | 346 | }
|
348 | 347 |
|
349 | 348 |
|
|
0 commit comments