|
1 /* vim: set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */ |
|
2 /** |
|
3 * Backbone-relational.js 0.9.0 |
|
4 * (c) 2011-2014 Paul Uithol and contributors (https://github.com/PaulUithol/Backbone-relational/graphs/contributors) |
|
5 * |
|
6 * Backbone-relational may be freely distributed under the MIT license; see the accompanying LICENSE.txt. |
|
7 * For details and documentation: https://github.com/PaulUithol/Backbone-relational. |
|
8 * Depends on Backbone (and thus on Underscore as well): https://github.com/documentcloud/backbone. |
|
9 * |
|
10 * Example: |
|
11 * |
|
12 Zoo = Backbone.RelationalModel.extend({ |
|
13 relations: [ { |
|
14 type: Backbone.HasMany, |
|
15 key: 'animals', |
|
16 relatedModel: 'Animal', |
|
17 reverseRelation: { |
|
18 key: 'livesIn', |
|
19 includeInJSON: 'id' |
|
20 // 'relatedModel' is automatically set to 'Zoo'; the 'relationType' to 'HasOne'. |
|
21 } |
|
22 } ], |
|
23 |
|
24 toString: function() { |
|
25 return this.get( 'name' ); |
|
26 } |
|
27 }); |
|
28 |
|
29 Animal = Backbone.RelationalModel.extend({ |
|
30 toString: function() { |
|
31 return this.get( 'species' ); |
|
32 } |
|
33 }); |
|
34 |
|
35 // Creating the zoo will give it a collection with one animal in it: the monkey. |
|
36 // The animal created after that has a relation `livesIn` that points to the zoo it's currently associated with. |
|
37 // If you instantiate (or fetch) the zebra later, it will automatically be added. |
|
38 |
|
39 var zoo = new Zoo({ |
|
40 name: 'Artis', |
|
41 animals: [ { id: 'monkey-1', species: 'Chimp' }, 'lion-1', 'zebra-1' ] |
|
42 }); |
|
43 |
|
44 var lion = new Animal( { id: 'lion-1', species: 'Lion' } ), |
|
45 monkey = zoo.get( 'animals' ).first(), |
|
46 sameZoo = lion.get( 'livesIn' ); |
|
47 */ |
|
48 ( function( root, factory ) { |
|
49 // Set up Backbone-relational for the environment. Start with AMD. |
|
50 if ( typeof define === 'function' && define.amd ) { |
|
51 define( [ 'exports', 'backbone', 'underscore' ], factory ); |
|
52 } |
|
53 // Next for Node.js or CommonJS. |
|
54 else if ( typeof exports !== 'undefined' ) { |
|
55 factory( exports, require( 'backbone' ), require( 'underscore' ) ); |
|
56 } |
|
57 // Finally, as a browser global. Use `root` here as it references `window`. |
|
58 else { |
|
59 factory( root, root.Backbone, root._ ); |
|
60 } |
|
61 }( this, function( exports, Backbone, _ ) { |
|
62 "use strict"; |
|
63 |
|
64 Backbone.Relational = { |
|
65 showWarnings: true |
|
66 }; |
|
67 |
|
68 /** |
|
69 * Semaphore mixin; can be used as both binary and counting. |
|
70 **/ |
|
71 Backbone.Semaphore = { |
|
72 _permitsAvailable: null, |
|
73 _permitsUsed: 0, |
|
74 |
|
75 acquire: function() { |
|
76 if ( this._permitsAvailable && this._permitsUsed >= this._permitsAvailable ) { |
|
77 throw new Error( 'Max permits acquired' ); |
|
78 } |
|
79 else { |
|
80 this._permitsUsed++; |
|
81 } |
|
82 }, |
|
83 |
|
84 release: function() { |
|
85 if ( this._permitsUsed === 0 ) { |
|
86 throw new Error( 'All permits released' ); |
|
87 } |
|
88 else { |
|
89 this._permitsUsed--; |
|
90 } |
|
91 }, |
|
92 |
|
93 isLocked: function() { |
|
94 return this._permitsUsed > 0; |
|
95 }, |
|
96 |
|
97 setAvailablePermits: function( amount ) { |
|
98 if ( this._permitsUsed > amount ) { |
|
99 throw new Error( 'Available permits cannot be less than used permits' ); |
|
100 } |
|
101 this._permitsAvailable = amount; |
|
102 } |
|
103 }; |
|
104 |
|
105 /** |
|
106 * A BlockingQueue that accumulates items while blocked (via 'block'), |
|
107 * and processes them when unblocked (via 'unblock'). |
|
108 * Process can also be called manually (via 'process'). |
|
109 */ |
|
110 Backbone.BlockingQueue = function() { |
|
111 this._queue = []; |
|
112 }; |
|
113 _.extend( Backbone.BlockingQueue.prototype, Backbone.Semaphore, { |
|
114 _queue: null, |
|
115 |
|
116 add: function( func ) { |
|
117 if ( this.isBlocked() ) { |
|
118 this._queue.push( func ); |
|
119 } |
|
120 else { |
|
121 func(); |
|
122 } |
|
123 }, |
|
124 |
|
125 // Some of the queued events may trigger other blocking events. By |
|
126 // copying the queue here it allows queued events to process closer to |
|
127 // the natural order. |
|
128 // |
|
129 // queue events [ 'A', 'B', 'C' ] |
|
130 // A handler of 'B' triggers 'D' and 'E' |
|
131 // By copying `this._queue` this executes: |
|
132 // [ 'A', 'B', 'D', 'E', 'C' ] |
|
133 // The same order the would have executed if they didn't have to be |
|
134 // delayed and queued. |
|
135 process: function() { |
|
136 var queue = this._queue; |
|
137 this._queue = []; |
|
138 while ( queue && queue.length ) { |
|
139 queue.shift()(); |
|
140 } |
|
141 }, |
|
142 |
|
143 block: function() { |
|
144 this.acquire(); |
|
145 }, |
|
146 |
|
147 unblock: function() { |
|
148 this.release(); |
|
149 if ( !this.isBlocked() ) { |
|
150 this.process(); |
|
151 } |
|
152 }, |
|
153 |
|
154 isBlocked: function() { |
|
155 return this.isLocked(); |
|
156 } |
|
157 }); |
|
158 /** |
|
159 * Global event queue. Accumulates external events ('add:<key>', 'remove:<key>' and 'change:<key>') |
|
160 * until the top-level object is fully initialized (see 'Backbone.RelationalModel'). |
|
161 */ |
|
162 Backbone.Relational.eventQueue = new Backbone.BlockingQueue(); |
|
163 |
|
164 /** |
|
165 * Backbone.Store keeps track of all created (and destruction of) Backbone.RelationalModel. |
|
166 * Handles lookup for relations. |
|
167 */ |
|
168 Backbone.Store = function() { |
|
169 this._collections = []; |
|
170 this._reverseRelations = []; |
|
171 this._orphanRelations = []; |
|
172 this._subModels = []; |
|
173 this._modelScopes = [ exports ]; |
|
174 }; |
|
175 _.extend( Backbone.Store.prototype, Backbone.Events, { |
|
176 /** |
|
177 * Create a new `Relation`. |
|
178 * @param {Backbone.RelationalModel} [model] |
|
179 * @param {Object} relation |
|
180 * @param {Object} [options] |
|
181 */ |
|
182 initializeRelation: function( model, relation, options ) { |
|
183 var type = !_.isString( relation.type ) ? relation.type : Backbone[ relation.type ] || this.getObjectByName( relation.type ); |
|
184 if ( type && type.prototype instanceof Backbone.Relation ) { |
|
185 var rel = new type( model, relation, options ); // Also pushes the new Relation into `model._relations` |
|
186 } |
|
187 else { |
|
188 Backbone.Relational.showWarnings && typeof console !== 'undefined' && console.warn( 'Relation=%o; missing or invalid relation type!', relation ); |
|
189 } |
|
190 }, |
|
191 |
|
192 /** |
|
193 * Add a scope for `getObjectByName` to look for model types by name. |
|
194 * @param {Object} scope |
|
195 */ |
|
196 addModelScope: function( scope ) { |
|
197 this._modelScopes.push( scope ); |
|
198 }, |
|
199 |
|
200 /** |
|
201 * Remove a scope. |
|
202 * @param {Object} scope |
|
203 */ |
|
204 removeModelScope: function( scope ) { |
|
205 this._modelScopes = _.without( this._modelScopes, scope ); |
|
206 }, |
|
207 |
|
208 /** |
|
209 * Add a set of subModelTypes to the store, that can be used to resolve the '_superModel' |
|
210 * for a model later in 'setupSuperModel'. |
|
211 * |
|
212 * @param {Backbone.RelationalModel} subModelTypes |
|
213 * @param {Backbone.RelationalModel} superModelType |
|
214 */ |
|
215 addSubModels: function( subModelTypes, superModelType ) { |
|
216 this._subModels.push({ |
|
217 'superModelType': superModelType, |
|
218 'subModels': subModelTypes |
|
219 }); |
|
220 }, |
|
221 |
|
222 /** |
|
223 * Check if the given modelType is registered as another model's subModel. If so, add it to the super model's |
|
224 * '_subModels', and set the modelType's '_superModel', '_subModelTypeName', and '_subModelTypeAttribute'. |
|
225 * |
|
226 * @param {Backbone.RelationalModel} modelType |
|
227 */ |
|
228 setupSuperModel: function( modelType ) { |
|
229 _.find( this._subModels, function( subModelDef ) { |
|
230 return _.filter( subModelDef.subModels || [], function( subModelTypeName, typeValue ) { |
|
231 var subModelType = this.getObjectByName( subModelTypeName ); |
|
232 |
|
233 if ( modelType === subModelType ) { |
|
234 // Set 'modelType' as a child of the found superModel |
|
235 subModelDef.superModelType._subModels[ typeValue ] = modelType; |
|
236 |
|
237 // Set '_superModel', '_subModelTypeValue', and '_subModelTypeAttribute' on 'modelType'. |
|
238 modelType._superModel = subModelDef.superModelType; |
|
239 modelType._subModelTypeValue = typeValue; |
|
240 modelType._subModelTypeAttribute = subModelDef.superModelType.prototype.subModelTypeAttribute; |
|
241 return true; |
|
242 } |
|
243 }, this ).length; |
|
244 }, this ); |
|
245 }, |
|
246 |
|
247 /** |
|
248 * Add a reverse relation. Is added to the 'relations' property on model's prototype, and to |
|
249 * existing instances of 'model' in the store as well. |
|
250 * @param {Object} relation |
|
251 * @param {Backbone.RelationalModel} relation.model |
|
252 * @param {String} relation.type |
|
253 * @param {String} relation.key |
|
254 * @param {String|Object} relation.relatedModel |
|
255 */ |
|
256 addReverseRelation: function( relation ) { |
|
257 var exists = _.any( this._reverseRelations, function( rel ) { |
|
258 return _.all( relation || [], function( val, key ) { |
|
259 return val === rel[ key ]; |
|
260 }); |
|
261 }); |
|
262 |
|
263 if ( !exists && relation.model && relation.type ) { |
|
264 this._reverseRelations.push( relation ); |
|
265 this._addRelation( relation.model, relation ); |
|
266 this.retroFitRelation( relation ); |
|
267 } |
|
268 }, |
|
269 |
|
270 /** |
|
271 * Deposit a `relation` for which the `relatedModel` can't be resolved at the moment. |
|
272 * |
|
273 * @param {Object} relation |
|
274 */ |
|
275 addOrphanRelation: function( relation ) { |
|
276 var exists = _.any( this._orphanRelations, function( rel ) { |
|
277 return _.all( relation || [], function( val, key ) { |
|
278 return val === rel[ key ]; |
|
279 }); |
|
280 }); |
|
281 |
|
282 if ( !exists && relation.model && relation.type ) { |
|
283 this._orphanRelations.push( relation ); |
|
284 } |
|
285 }, |
|
286 |
|
287 /** |
|
288 * Try to initialize any `_orphanRelation`s |
|
289 */ |
|
290 processOrphanRelations: function() { |
|
291 // Make sure to operate on a copy since we're removing while iterating |
|
292 _.each( this._orphanRelations.slice( 0 ), function( rel ) { |
|
293 var relatedModel = Backbone.Relational.store.getObjectByName( rel.relatedModel ); |
|
294 if ( relatedModel ) { |
|
295 this.initializeRelation( null, rel ); |
|
296 this._orphanRelations = _.without( this._orphanRelations, rel ); |
|
297 } |
|
298 }, this ); |
|
299 }, |
|
300 |
|
301 /** |
|
302 * |
|
303 * @param {Backbone.RelationalModel.constructor} type |
|
304 * @param {Object} relation |
|
305 * @private |
|
306 */ |
|
307 _addRelation: function( type, relation ) { |
|
308 if ( !type.prototype.relations ) { |
|
309 type.prototype.relations = []; |
|
310 } |
|
311 type.prototype.relations.push( relation ); |
|
312 |
|
313 _.each( type._subModels || [], function( subModel ) { |
|
314 this._addRelation( subModel, relation ); |
|
315 }, this ); |
|
316 }, |
|
317 |
|
318 /** |
|
319 * Add a 'relation' to all existing instances of 'relation.model' in the store |
|
320 * @param {Object} relation |
|
321 */ |
|
322 retroFitRelation: function( relation ) { |
|
323 var coll = this.getCollection( relation.model, false ); |
|
324 coll && coll.each( function( model ) { |
|
325 if ( !( model instanceof relation.model ) ) { |
|
326 return; |
|
327 } |
|
328 |
|
329 var rel = new relation.type( model, relation ); |
|
330 }, this ); |
|
331 }, |
|
332 |
|
333 /** |
|
334 * Find the Store's collection for a certain type of model. |
|
335 * @param {Backbone.RelationalModel} type |
|
336 * @param {Boolean} [create=true] Should a collection be created if none is found? |
|
337 * @return {Backbone.Collection} A collection if found (or applicable for 'model'), or null |
|
338 */ |
|
339 getCollection: function( type, create ) { |
|
340 if ( type instanceof Backbone.RelationalModel ) { |
|
341 type = type.constructor; |
|
342 } |
|
343 |
|
344 var rootModel = type; |
|
345 while ( rootModel._superModel ) { |
|
346 rootModel = rootModel._superModel; |
|
347 } |
|
348 |
|
349 var coll = _.find( this._collections, function( item ) { |
|
350 return item.model === rootModel; |
|
351 }); |
|
352 |
|
353 if ( !coll && create !== false ) { |
|
354 coll = this._createCollection( rootModel ); |
|
355 } |
|
356 |
|
357 return coll; |
|
358 }, |
|
359 |
|
360 /** |
|
361 * Find a model type on one of the modelScopes by name. Names are split on dots. |
|
362 * @param {String} name |
|
363 * @return {Object} |
|
364 */ |
|
365 getObjectByName: function( name ) { |
|
366 var parts = name.split( '.' ), |
|
367 type = null; |
|
368 |
|
369 _.find( this._modelScopes, function( scope ) { |
|
370 type = _.reduce( parts || [], function( memo, val ) { |
|
371 return memo ? memo[ val ] : undefined; |
|
372 }, scope ); |
|
373 |
|
374 if ( type && type !== scope ) { |
|
375 return true; |
|
376 } |
|
377 }, this ); |
|
378 |
|
379 return type; |
|
380 }, |
|
381 |
|
382 _createCollection: function( type ) { |
|
383 var coll; |
|
384 |
|
385 // If 'type' is an instance, take its constructor |
|
386 if ( type instanceof Backbone.RelationalModel ) { |
|
387 type = type.constructor; |
|
388 } |
|
389 |
|
390 // Type should inherit from Backbone.RelationalModel. |
|
391 if ( type.prototype instanceof Backbone.RelationalModel ) { |
|
392 coll = new Backbone.Collection(); |
|
393 coll.model = type; |
|
394 |
|
395 this._collections.push( coll ); |
|
396 } |
|
397 |
|
398 return coll; |
|
399 }, |
|
400 |
|
401 /** |
|
402 * Find the attribute that is to be used as the `id` on a given object |
|
403 * @param type |
|
404 * @param {String|Number|Object|Backbone.RelationalModel} item |
|
405 * @return {String|Number} |
|
406 */ |
|
407 resolveIdForItem: function( type, item ) { |
|
408 var id = _.isString( item ) || _.isNumber( item ) ? item : null; |
|
409 |
|
410 if ( id === null ) { |
|
411 if ( item instanceof Backbone.RelationalModel ) { |
|
412 id = item.id; |
|
413 } |
|
414 else if ( _.isObject( item ) ) { |
|
415 id = item[ type.prototype.idAttribute ]; |
|
416 } |
|
417 } |
|
418 |
|
419 // Make all falsy values `null` (except for 0, which could be an id.. see '/issues/179') |
|
420 if ( !id && id !== 0 ) { |
|
421 id = null; |
|
422 } |
|
423 |
|
424 return id; |
|
425 }, |
|
426 |
|
427 /** |
|
428 * Find a specific model of a certain `type` in the store |
|
429 * @param type |
|
430 * @param {String|Number|Object|Backbone.RelationalModel} item |
|
431 */ |
|
432 find: function( type, item ) { |
|
433 var id = this.resolveIdForItem( type, item ), |
|
434 coll = this.getCollection( type ); |
|
435 |
|
436 // Because the found object could be of any of the type's superModel |
|
437 // types, only return it if it's actually of the type asked for. |
|
438 if ( coll ) { |
|
439 var obj = coll.get( id ); |
|
440 |
|
441 if ( obj instanceof type ) { |
|
442 return obj; |
|
443 } |
|
444 } |
|
445 |
|
446 return null; |
|
447 }, |
|
448 |
|
449 /** |
|
450 * Add a 'model' to its appropriate collection. Retain the original contents of 'model.collection'. |
|
451 * @param {Backbone.RelationalModel} model |
|
452 */ |
|
453 register: function( model ) { |
|
454 var coll = this.getCollection( model ); |
|
455 |
|
456 if ( coll ) { |
|
457 var modelColl = model.collection; |
|
458 coll.add( model ); |
|
459 model.collection = modelColl; |
|
460 } |
|
461 }, |
|
462 |
|
463 /** |
|
464 * Check if the given model may use the given `id` |
|
465 * @param model |
|
466 * @param [id] |
|
467 */ |
|
468 checkId: function( model, id ) { |
|
469 var coll = this.getCollection( model ), |
|
470 duplicate = coll && coll.get( id ); |
|
471 |
|
472 if ( duplicate && model !== duplicate ) { |
|
473 if ( Backbone.Relational.showWarnings && typeof console !== 'undefined' ) { |
|
474 console.warn( 'Duplicate id! Old RelationalModel=%o, new RelationalModel=%o', duplicate, model ); |
|
475 } |
|
476 |
|
477 throw new Error( "Cannot instantiate more than one Backbone.RelationalModel with the same id per type!" ); |
|
478 } |
|
479 }, |
|
480 |
|
481 /** |
|
482 * Explicitly update a model's id in its store collection |
|
483 * @param {Backbone.RelationalModel} model |
|
484 */ |
|
485 update: function( model ) { |
|
486 var coll = this.getCollection( model ); |
|
487 |
|
488 // Register a model if it isn't yet (which happens if it was created without an id). |
|
489 if ( !coll.contains( model ) ) { |
|
490 this.register( model ); |
|
491 } |
|
492 |
|
493 // This triggers updating the lookup indices kept in a collection |
|
494 coll._onModelEvent( 'change:' + model.idAttribute, model, coll ); |
|
495 |
|
496 // Trigger an event on model so related models (having the model's new id in their keyContents) can add it. |
|
497 model.trigger( 'relational:change:id', model, coll ); |
|
498 }, |
|
499 |
|
500 /** |
|
501 * Unregister from the store: a specific model, a collection, or a model type. |
|
502 * @param {Backbone.RelationalModel|Backbone.RelationalModel.constructor|Backbone.Collection} type |
|
503 */ |
|
504 unregister: function( type ) { |
|
505 var coll, |
|
506 models; |
|
507 |
|
508 if ( type instanceof Backbone.Model ) { |
|
509 coll = this.getCollection( type ); |
|
510 models = [ type ]; |
|
511 } |
|
512 else if ( type instanceof Backbone.Collection ) { |
|
513 coll = this.getCollection( type.model ); |
|
514 models = _.clone( type.models ); |
|
515 } |
|
516 else { |
|
517 coll = this.getCollection( type ); |
|
518 models = _.clone( coll.models ); |
|
519 } |
|
520 |
|
521 _.each( models, function( model ) { |
|
522 this.stopListening( model ); |
|
523 _.invoke( model.getRelations(), 'stopListening' ); |
|
524 }, this ); |
|
525 |
|
526 |
|
527 // If we've unregistered an entire store collection, reset the collection (which is much faster). |
|
528 // Otherwise, remove each model one by one. |
|
529 if ( _.contains( this._collections, type ) ) { |
|
530 coll.reset( [] ); |
|
531 } |
|
532 else { |
|
533 _.each( models, function( model ) { |
|
534 if ( coll.get( model ) ) { |
|
535 coll.remove( model ); |
|
536 } |
|
537 else { |
|
538 coll.trigger( 'relational:remove', model, coll ); |
|
539 } |
|
540 }, this ); |
|
541 } |
|
542 }, |
|
543 |
|
544 /** |
|
545 * Reset the `store` to it's original state. The `reverseRelations` are kept though, since attempting to |
|
546 * re-initialize these on models would lead to a large amount of warnings. |
|
547 */ |
|
548 reset: function() { |
|
549 this.stopListening(); |
|
550 |
|
551 // Unregister each collection to remove event listeners |
|
552 _.each( this._collections, function( coll ) { |
|
553 this.unregister( coll ); |
|
554 }, this ); |
|
555 |
|
556 this._collections = []; |
|
557 this._subModels = []; |
|
558 this._modelScopes = [ exports ]; |
|
559 } |
|
560 }); |
|
561 Backbone.Relational.store = new Backbone.Store(); |
|
562 |
|
563 /** |
|
564 * The main Relation class, from which 'HasOne' and 'HasMany' inherit. Internally, 'relational:<key>' events |
|
565 * are used to regulate addition and removal of models from relations. |
|
566 * |
|
567 * @param {Backbone.RelationalModel} [instance] Model that this relation is created for. If no model is supplied, |
|
568 * Relation just tries to instantiate it's `reverseRelation` if specified, and bails out after that. |
|
569 * @param {Object} options |
|
570 * @param {string} options.key |
|
571 * @param {Backbone.RelationalModel.constructor} options.relatedModel |
|
572 * @param {Boolean|String} [options.includeInJSON=true] Serialize the given attribute for related model(s)' in toJSON, or just their ids. |
|
573 * @param {Boolean} [options.createModels=true] Create objects from the contents of keys if the object is not found in Backbone.store. |
|
574 * @param {Object} [options.reverseRelation] Specify a bi-directional relation. If provided, Relation will reciprocate |
|
575 * the relation to the 'relatedModel'. Required and optional properties match 'options', except that it also needs |
|
576 * {Backbone.Relation|String} type ('HasOne' or 'HasMany'). |
|
577 * @param {Object} opts |
|
578 */ |
|
579 Backbone.Relation = function( instance, options, opts ) { |
|
580 this.instance = instance; |
|
581 // Make sure 'options' is sane, and fill with defaults from subclasses and this object's prototype |
|
582 options = _.isObject( options ) ? options : {}; |
|
583 this.reverseRelation = _.defaults( options.reverseRelation || {}, this.options.reverseRelation ); |
|
584 this.options = _.defaults( options, this.options, Backbone.Relation.prototype.options ); |
|
585 |
|
586 this.reverseRelation.type = !_.isString( this.reverseRelation.type ) ? this.reverseRelation.type : |
|
587 Backbone[ this.reverseRelation.type ] || Backbone.Relational.store.getObjectByName( this.reverseRelation.type ); |
|
588 |
|
589 this.key = this.options.key; |
|
590 this.keySource = this.options.keySource || this.key; |
|
591 this.keyDestination = this.options.keyDestination || this.keySource || this.key; |
|
592 |
|
593 this.model = this.options.model || this.instance.constructor; |
|
594 |
|
595 this.relatedModel = this.options.relatedModel; |
|
596 |
|
597 // No 'relatedModel' is interpreted as self-referential |
|
598 if ( _.isUndefined( this.relatedModel ) ) { |
|
599 this.relatedModel = this.model; |
|
600 } |
|
601 |
|
602 // Otherwise, try to resolve the given value to an object |
|
603 if ( _.isFunction( this.relatedModel ) && !( this.relatedModel.prototype instanceof Backbone.RelationalModel ) ) { |
|
604 this.relatedModel = _.result( this, 'relatedModel' ); |
|
605 } |
|
606 if ( _.isString( this.relatedModel ) ) { |
|
607 this.relatedModel = Backbone.Relational.store.getObjectByName( this.relatedModel ); |
|
608 } |
|
609 |
|
610 |
|
611 if ( !this.checkPreconditions() ) { |
|
612 return; |
|
613 } |
|
614 |
|
615 // Add the reverse relation on 'relatedModel' to the store's reverseRelations |
|
616 if ( !this.options.isAutoRelation && this.reverseRelation.type && this.reverseRelation.key ) { |
|
617 Backbone.Relational.store.addReverseRelation( _.defaults( { |
|
618 isAutoRelation: true, |
|
619 model: this.relatedModel, |
|
620 relatedModel: this.model, |
|
621 reverseRelation: this.options // current relation is the 'reverseRelation' for its own reverseRelation |
|
622 }, |
|
623 this.reverseRelation // Take further properties from this.reverseRelation (type, key, etc.) |
|
624 ) ); |
|
625 } |
|
626 |
|
627 if ( instance ) { |
|
628 var contentKey = this.keySource; |
|
629 if ( contentKey !== this.key && _.isObject( this.instance.get( this.key ) ) ) { |
|
630 contentKey = this.key; |
|
631 } |
|
632 |
|
633 this.setKeyContents( this.instance.get( contentKey ) ); |
|
634 this.relatedCollection = Backbone.Relational.store.getCollection( this.relatedModel ); |
|
635 |
|
636 // Explicitly clear 'keySource', to prevent a leaky abstraction if 'keySource' differs from 'key'. |
|
637 if ( this.keySource !== this.key ) { |
|
638 delete this.instance.attributes[ this.keySource ]; |
|
639 } |
|
640 |
|
641 // Add this Relation to instance._relations |
|
642 this.instance._relations[ this.key ] = this; |
|
643 |
|
644 this.initialize( opts ); |
|
645 |
|
646 if ( this.options.autoFetch ) { |
|
647 this.instance.getAsync( this.key, _.isObject( this.options.autoFetch ) ? this.options.autoFetch : {} ); |
|
648 } |
|
649 |
|
650 // When 'relatedModel' are created or destroyed, check if it affects this relation. |
|
651 this.listenTo( this.instance, 'destroy', this.destroy ) |
|
652 .listenTo( this.relatedCollection, 'relational:add relational:change:id', this.tryAddRelated ) |
|
653 .listenTo( this.relatedCollection, 'relational:remove', this.removeRelated ); |
|
654 } |
|
655 }; |
|
656 // Fix inheritance :\ |
|
657 Backbone.Relation.extend = Backbone.Model.extend; |
|
658 // Set up all inheritable **Backbone.Relation** properties and methods. |
|
659 _.extend( Backbone.Relation.prototype, Backbone.Events, Backbone.Semaphore, { |
|
660 options: { |
|
661 createModels: true, |
|
662 includeInJSON: true, |
|
663 isAutoRelation: false, |
|
664 autoFetch: false, |
|
665 parse: false |
|
666 }, |
|
667 |
|
668 instance: null, |
|
669 key: null, |
|
670 keyContents: null, |
|
671 relatedModel: null, |
|
672 relatedCollection: null, |
|
673 reverseRelation: null, |
|
674 related: null, |
|
675 |
|
676 /** |
|
677 * Check several pre-conditions. |
|
678 * @return {Boolean} True if pre-conditions are satisfied, false if they're not. |
|
679 */ |
|
680 checkPreconditions: function() { |
|
681 var i = this.instance, |
|
682 k = this.key, |
|
683 m = this.model, |
|
684 rm = this.relatedModel, |
|
685 warn = Backbone.Relational.showWarnings && typeof console !== 'undefined'; |
|
686 |
|
687 if ( !m || !k || !rm ) { |
|
688 warn && console.warn( 'Relation=%o: missing model, key or relatedModel (%o, %o, %o).', this, m, k, rm ); |
|
689 return false; |
|
690 } |
|
691 // Check if the type in 'model' inherits from Backbone.RelationalModel |
|
692 if ( !( m.prototype instanceof Backbone.RelationalModel ) ) { |
|
693 warn && console.warn( 'Relation=%o: model does not inherit from Backbone.RelationalModel (%o).', this, i ); |
|
694 return false; |
|
695 } |
|
696 // Check if the type in 'relatedModel' inherits from Backbone.RelationalModel |
|
697 if ( !( rm.prototype instanceof Backbone.RelationalModel ) ) { |
|
698 warn && console.warn( 'Relation=%o: relatedModel does not inherit from Backbone.RelationalModel (%o).', this, rm ); |
|
699 return false; |
|
700 } |
|
701 // Check if this is not a HasMany, and the reverse relation is HasMany as well |
|
702 if ( this instanceof Backbone.HasMany && this.reverseRelation.type === Backbone.HasMany ) { |
|
703 warn && console.warn( 'Relation=%o: relation is a HasMany, and the reverseRelation is HasMany as well.', this ); |
|
704 return false; |
|
705 } |
|
706 // Check if we're not attempting to create a relationship on a `key` that's already used. |
|
707 if ( i && _.keys( i._relations ).length ) { |
|
708 var existing = _.find( i._relations, function( rel ) { |
|
709 return rel.key === k; |
|
710 }, this ); |
|
711 |
|
712 if ( existing ) { |
|
713 warn && console.warn( 'Cannot create relation=%o on %o for model=%o: already taken by relation=%o.', |
|
714 this, k, i, existing ); |
|
715 return false; |
|
716 } |
|
717 } |
|
718 |
|
719 return true; |
|
720 }, |
|
721 |
|
722 /** |
|
723 * Set the related model(s) for this relation |
|
724 * @param {Backbone.Model|Backbone.Collection} related |
|
725 */ |
|
726 setRelated: function( related ) { |
|
727 this.related = related; |
|
728 this.instance.attributes[ this.key ] = related; |
|
729 }, |
|
730 |
|
731 /** |
|
732 * Determine if a relation (on a different RelationalModel) is the reverse |
|
733 * relation of the current one. |
|
734 * @param {Backbone.Relation} relation |
|
735 * @return {Boolean} |
|
736 */ |
|
737 _isReverseRelation: function( relation ) { |
|
738 return relation.instance instanceof this.relatedModel && this.reverseRelation.key === relation.key && |
|
739 this.key === relation.reverseRelation.key; |
|
740 }, |
|
741 |
|
742 /** |
|
743 * Get the reverse relations (pointing back to 'this.key' on 'this.instance') for the currently related model(s). |
|
744 * @param {Backbone.RelationalModel} [model] Get the reverse relations for a specific model. |
|
745 * If not specified, 'this.related' is used. |
|
746 * @return {Backbone.Relation[]} |
|
747 */ |
|
748 getReverseRelations: function( model ) { |
|
749 var reverseRelations = []; |
|
750 // Iterate over 'model', 'this.related.models' (if this.related is a Backbone.Collection), or wrap 'this.related' in an array. |
|
751 var models = !_.isUndefined( model ) ? [ model ] : this.related && ( this.related.models || [ this.related ] ), |
|
752 relations = null, |
|
753 relation = null; |
|
754 |
|
755 for( var i = 0; i < ( models || [] ).length; i++ ) { |
|
756 relations = models[ i ].getRelations() || []; |
|
757 |
|
758 for( var j = 0; j < relations.length; j++ ) { |
|
759 relation = relations[ j ]; |
|
760 |
|
761 if ( this._isReverseRelation( relation ) ) { |
|
762 reverseRelations.push( relation ); |
|
763 } |
|
764 } |
|
765 } |
|
766 |
|
767 return reverseRelations; |
|
768 }, |
|
769 |
|
770 /** |
|
771 * When `this.instance` is destroyed, cleanup our relations. |
|
772 * Get reverse relation, call removeRelated on each. |
|
773 */ |
|
774 destroy: function() { |
|
775 this.stopListening(); |
|
776 |
|
777 if ( this instanceof Backbone.HasOne ) { |
|
778 this.setRelated( null ); |
|
779 } |
|
780 else if ( this instanceof Backbone.HasMany ) { |
|
781 this.setRelated( this._prepareCollection() ); |
|
782 } |
|
783 |
|
784 _.each( this.getReverseRelations(), function( relation ) { |
|
785 relation.removeRelated( this.instance ); |
|
786 }, this ); |
|
787 } |
|
788 }); |
|
789 |
|
790 Backbone.HasOne = Backbone.Relation.extend({ |
|
791 options: { |
|
792 reverseRelation: { type: 'HasMany' } |
|
793 }, |
|
794 |
|
795 initialize: function( opts ) { |
|
796 this.listenTo( this.instance, 'relational:change:' + this.key, this.onChange ); |
|
797 |
|
798 var related = this.findRelated( opts ); |
|
799 this.setRelated( related ); |
|
800 |
|
801 // Notify new 'related' object of the new relation. |
|
802 _.each( this.getReverseRelations(), function( relation ) { |
|
803 relation.addRelated( this.instance, opts ); |
|
804 }, this ); |
|
805 }, |
|
806 |
|
807 /** |
|
808 * Find related Models. |
|
809 * @param {Object} [options] |
|
810 * @return {Backbone.Model} |
|
811 */ |
|
812 findRelated: function( options ) { |
|
813 var related = null; |
|
814 |
|
815 options = _.defaults( { parse: this.options.parse }, options ); |
|
816 |
|
817 if ( this.keyContents instanceof this.relatedModel ) { |
|
818 related = this.keyContents; |
|
819 } |
|
820 else if ( this.keyContents || this.keyContents === 0 ) { // since 0 can be a valid `id` as well |
|
821 var opts = _.defaults( { create: this.options.createModels }, options ); |
|
822 related = this.relatedModel.findOrCreate( this.keyContents, opts ); |
|
823 } |
|
824 |
|
825 // Nullify `keyId` if we have a related model; in case it was already part of the relation |
|
826 if ( related ) { |
|
827 this.keyId = null; |
|
828 } |
|
829 |
|
830 return related; |
|
831 }, |
|
832 |
|
833 /** |
|
834 * Normalize and reduce `keyContents` to an `id`, for easier comparison |
|
835 * @param {String|Number|Backbone.Model} keyContents |
|
836 */ |
|
837 setKeyContents: function( keyContents ) { |
|
838 this.keyContents = keyContents; |
|
839 this.keyId = Backbone.Relational.store.resolveIdForItem( this.relatedModel, this.keyContents ); |
|
840 }, |
|
841 |
|
842 /** |
|
843 * Event handler for `change:<key>`. |
|
844 * If the key is changed, notify old & new reverse relations and initialize the new relation. |
|
845 */ |
|
846 onChange: function( model, attr, options ) { |
|
847 // Don't accept recursive calls to onChange (like onChange->findRelated->findOrCreate->initializeRelations->addRelated->onChange) |
|
848 if ( this.isLocked() ) { |
|
849 return; |
|
850 } |
|
851 this.acquire(); |
|
852 options = options ? _.clone( options ) : {}; |
|
853 |
|
854 // 'options.__related' is set by 'addRelated'/'removeRelated'. If it is set, the change |
|
855 // is the result of a call from a relation. If it's not, the change is the result of |
|
856 // a 'set' call on this.instance. |
|
857 var changed = _.isUndefined( options.__related ), |
|
858 oldRelated = changed ? this.related : options.__related; |
|
859 |
|
860 if ( changed ) { |
|
861 this.setKeyContents( attr ); |
|
862 var related = this.findRelated( options ); |
|
863 this.setRelated( related ); |
|
864 } |
|
865 |
|
866 // Notify old 'related' object of the terminated relation |
|
867 if ( oldRelated && this.related !== oldRelated ) { |
|
868 _.each( this.getReverseRelations( oldRelated ), function( relation ) { |
|
869 relation.removeRelated( this.instance, null, options ); |
|
870 }, this ); |
|
871 } |
|
872 |
|
873 // Notify new 'related' object of the new relation. Note we do re-apply even if this.related is oldRelated; |
|
874 // that can be necessary for bi-directional relations if 'this.instance' was created after 'this.related'. |
|
875 // In that case, 'this.instance' will already know 'this.related', but the reverse might not exist yet. |
|
876 _.each( this.getReverseRelations(), function( relation ) { |
|
877 relation.addRelated( this.instance, options ); |
|
878 }, this ); |
|
879 |
|
880 // Fire the 'change:<key>' event if 'related' was updated |
|
881 if ( !options.silent && this.related !== oldRelated ) { |
|
882 var dit = this; |
|
883 this.changed = true; |
|
884 Backbone.Relational.eventQueue.add( function() { |
|
885 dit.instance.trigger( 'change:' + dit.key, dit.instance, dit.related, options, true ); |
|
886 dit.changed = false; |
|
887 }); |
|
888 } |
|
889 this.release(); |
|
890 }, |
|
891 |
|
892 /** |
|
893 * If a new 'this.relatedModel' appears in the 'store', try to match it to the last set 'keyContents' |
|
894 */ |
|
895 tryAddRelated: function( model, coll, options ) { |
|
896 if ( ( this.keyId || this.keyId === 0 ) && model.id === this.keyId ) { // since 0 can be a valid `id` as well |
|
897 this.addRelated( model, options ); |
|
898 this.keyId = null; |
|
899 } |
|
900 }, |
|
901 |
|
902 addRelated: function( model, options ) { |
|
903 // Allow 'model' to set up its relations before proceeding. |
|
904 // (which can result in a call to 'addRelated' from a relation of 'model') |
|
905 var dit = this; |
|
906 model.queue( function() { |
|
907 if ( model !== dit.related ) { |
|
908 var oldRelated = dit.related || null; |
|
909 dit.setRelated( model ); |
|
910 dit.onChange( dit.instance, model, _.defaults( { __related: oldRelated }, options ) ); |
|
911 } |
|
912 }); |
|
913 }, |
|
914 |
|
915 removeRelated: function( model, coll, options ) { |
|
916 if ( !this.related ) { |
|
917 return; |
|
918 } |
|
919 |
|
920 if ( model === this.related ) { |
|
921 var oldRelated = this.related || null; |
|
922 this.setRelated( null ); |
|
923 this.onChange( this.instance, model, _.defaults( { __related: oldRelated }, options ) ); |
|
924 } |
|
925 } |
|
926 }); |
|
927 |
|
928 Backbone.HasMany = Backbone.Relation.extend({ |
|
929 collectionType: null, |
|
930 |
|
931 options: { |
|
932 reverseRelation: { type: 'HasOne' }, |
|
933 collectionType: Backbone.Collection, |
|
934 collectionKey: true, |
|
935 collectionOptions: {} |
|
936 }, |
|
937 |
|
938 initialize: function( opts ) { |
|
939 this.listenTo( this.instance, 'relational:change:' + this.key, this.onChange ); |
|
940 |
|
941 // Handle a custom 'collectionType' |
|
942 this.collectionType = this.options.collectionType; |
|
943 if ( _.isFunction( this.collectionType ) && this.collectionType !== Backbone.Collection && !( this.collectionType.prototype instanceof Backbone.Collection ) ) { |
|
944 this.collectionType = _.result( this, 'collectionType' ); |
|
945 } |
|
946 if ( _.isString( this.collectionType ) ) { |
|
947 this.collectionType = Backbone.Relational.store.getObjectByName( this.collectionType ); |
|
948 } |
|
949 if ( this.collectionType !== Backbone.Collection && !( this.collectionType.prototype instanceof Backbone.Collection ) ) { |
|
950 throw new Error( '`collectionType` must inherit from Backbone.Collection' ); |
|
951 } |
|
952 |
|
953 var related = this.findRelated( opts ); |
|
954 this.setRelated( related ); |
|
955 }, |
|
956 |
|
957 /** |
|
958 * Bind events and setup collectionKeys for a collection that is to be used as the backing store for a HasMany. |
|
959 * If no 'collection' is supplied, a new collection will be created of the specified 'collectionType' option. |
|
960 * @param {Backbone.Collection} [collection] |
|
961 * @return {Backbone.Collection} |
|
962 */ |
|
963 _prepareCollection: function( collection ) { |
|
964 if ( this.related ) { |
|
965 this.stopListening( this.related ); |
|
966 } |
|
967 |
|
968 if ( !collection || !( collection instanceof Backbone.Collection ) ) { |
|
969 var options = _.isFunction( this.options.collectionOptions ) ? |
|
970 this.options.collectionOptions( this.instance ) : this.options.collectionOptions; |
|
971 |
|
972 collection = new this.collectionType( null, options ); |
|
973 } |
|
974 |
|
975 collection.model = this.relatedModel; |
|
976 |
|
977 if ( this.options.collectionKey ) { |
|
978 var key = this.options.collectionKey === true ? this.options.reverseRelation.key : this.options.collectionKey; |
|
979 |
|
980 if ( collection[ key ] && collection[ key ] !== this.instance ) { |
|
981 if ( Backbone.Relational.showWarnings && typeof console !== 'undefined' ) { |
|
982 console.warn( 'Relation=%o; collectionKey=%s already exists on collection=%o', this, key, this.options.collectionKey ); |
|
983 } |
|
984 } |
|
985 else if ( key ) { |
|
986 collection[ key ] = this.instance; |
|
987 } |
|
988 } |
|
989 |
|
990 this.listenTo( collection, 'relational:add', this.handleAddition ) |
|
991 .listenTo( collection, 'relational:remove', this.handleRemoval ) |
|
992 .listenTo( collection, 'relational:reset', this.handleReset ); |
|
993 |
|
994 return collection; |
|
995 }, |
|
996 |
|
997 /** |
|
998 * Find related Models. |
|
999 * @param {Object} [options] |
|
1000 * @return {Backbone.Collection} |
|
1001 */ |
|
1002 findRelated: function( options ) { |
|
1003 var related = null; |
|
1004 |
|
1005 options = _.defaults( { parse: this.options.parse }, options ); |
|
1006 |
|
1007 // Replace 'this.related' by 'this.keyContents' if it is a Backbone.Collection |
|
1008 if ( this.keyContents instanceof Backbone.Collection ) { |
|
1009 this._prepareCollection( this.keyContents ); |
|
1010 related = this.keyContents; |
|
1011 } |
|
1012 // Otherwise, 'this.keyContents' should be an array of related object ids. |
|
1013 // Re-use the current 'this.related' if it is a Backbone.Collection; otherwise, create a new collection. |
|
1014 else { |
|
1015 var toAdd = []; |
|
1016 |
|
1017 _.each( this.keyContents, function( attributes ) { |
|
1018 var model = null; |
|
1019 |
|
1020 if ( attributes instanceof this.relatedModel ) { |
|
1021 model = attributes; |
|
1022 } |
|
1023 else { |
|
1024 // If `merge` is true, update models here, instead of during update. |
|
1025 model = this.relatedModel.findOrCreate( attributes, |
|
1026 _.extend( { merge: true }, options, { create: this.options.createModels } ) |
|
1027 ); |
|
1028 } |
|
1029 |
|
1030 model && toAdd.push( model ); |
|
1031 }, this ); |
|
1032 |
|
1033 if ( this.related instanceof Backbone.Collection ) { |
|
1034 related = this.related; |
|
1035 } |
|
1036 else { |
|
1037 related = this._prepareCollection(); |
|
1038 } |
|
1039 |
|
1040 // By now, both `merge` and `parse` will already have been executed for models if they were specified. |
|
1041 // Disable them to prevent additional calls. |
|
1042 related.set( toAdd, _.defaults( { merge: false, parse: false }, options ) ); |
|
1043 } |
|
1044 |
|
1045 // Remove entries from `keyIds` that were already part of the relation (and are thus 'unchanged') |
|
1046 this.keyIds = _.difference( this.keyIds, _.pluck( related.models, 'id' ) ); |
|
1047 |
|
1048 return related; |
|
1049 }, |
|
1050 |
|
1051 /** |
|
1052 * Normalize and reduce `keyContents` to a list of `ids`, for easier comparison |
|
1053 * @param {String|Number|String[]|Number[]|Backbone.Collection} keyContents |
|
1054 */ |
|
1055 setKeyContents: function( keyContents ) { |
|
1056 this.keyContents = keyContents instanceof Backbone.Collection ? keyContents : null; |
|
1057 this.keyIds = []; |
|
1058 |
|
1059 if ( !this.keyContents && ( keyContents || keyContents === 0 ) ) { // since 0 can be a valid `id` as well |
|
1060 // Handle cases the an API/user supplies just an Object/id instead of an Array |
|
1061 this.keyContents = _.isArray( keyContents ) ? keyContents : [ keyContents ]; |
|
1062 |
|
1063 _.each( this.keyContents, function( item ) { |
|
1064 var itemId = Backbone.Relational.store.resolveIdForItem( this.relatedModel, item ); |
|
1065 if ( itemId || itemId === 0 ) { |
|
1066 this.keyIds.push( itemId ); |
|
1067 } |
|
1068 }, this ); |
|
1069 } |
|
1070 }, |
|
1071 |
|
1072 /** |
|
1073 * Event handler for `change:<key>`. |
|
1074 * If the contents of the key are changed, notify old & new reverse relations and initialize the new relation. |
|
1075 */ |
|
1076 onChange: function( model, attr, options ) { |
|
1077 options = options ? _.clone( options ) : {}; |
|
1078 this.setKeyContents( attr ); |
|
1079 this.changed = false; |
|
1080 |
|
1081 var related = this.findRelated( options ); |
|
1082 this.setRelated( related ); |
|
1083 |
|
1084 if ( !options.silent ) { |
|
1085 var dit = this; |
|
1086 Backbone.Relational.eventQueue.add( function() { |
|
1087 // The `changed` flag can be set in `handleAddition` or `handleRemoval` |
|
1088 if ( dit.changed ) { |
|
1089 dit.instance.trigger( 'change:' + dit.key, dit.instance, dit.related, options, true ); |
|
1090 dit.changed = false; |
|
1091 } |
|
1092 }); |
|
1093 } |
|
1094 }, |
|
1095 |
|
1096 /** |
|
1097 * When a model is added to a 'HasMany', trigger 'add' on 'this.instance' and notify reverse relations. |
|
1098 * (should be 'HasOne', must set 'this.instance' as their related). |
|
1099 */ |
|
1100 handleAddition: function( model, coll, options ) { |
|
1101 //console.debug('handleAddition called; args=%o', arguments); |
|
1102 options = options ? _.clone( options ) : {}; |
|
1103 this.changed = true; |
|
1104 |
|
1105 _.each( this.getReverseRelations( model ), function( relation ) { |
|
1106 relation.addRelated( this.instance, options ); |
|
1107 }, this ); |
|
1108 |
|
1109 // Only trigger 'add' once the newly added model is initialized (so, has its relations set up) |
|
1110 var dit = this; |
|
1111 !options.silent && Backbone.Relational.eventQueue.add( function() { |
|
1112 dit.instance.trigger( 'add:' + dit.key, model, dit.related, options ); |
|
1113 }); |
|
1114 }, |
|
1115 |
|
1116 /** |
|
1117 * When a model is removed from a 'HasMany', trigger 'remove' on 'this.instance' and notify reverse relations. |
|
1118 * (should be 'HasOne', which should be nullified) |
|
1119 */ |
|
1120 handleRemoval: function( model, coll, options ) { |
|
1121 //console.debug('handleRemoval called; args=%o', arguments); |
|
1122 options = options ? _.clone( options ) : {}; |
|
1123 this.changed = true; |
|
1124 |
|
1125 _.each( this.getReverseRelations( model ), function( relation ) { |
|
1126 relation.removeRelated( this.instance, null, options ); |
|
1127 }, this ); |
|
1128 |
|
1129 var dit = this; |
|
1130 !options.silent && Backbone.Relational.eventQueue.add( function() { |
|
1131 dit.instance.trigger( 'remove:' + dit.key, model, dit.related, options ); |
|
1132 }); |
|
1133 }, |
|
1134 |
|
1135 handleReset: function( coll, options ) { |
|
1136 var dit = this; |
|
1137 options = options ? _.clone( options ) : {}; |
|
1138 !options.silent && Backbone.Relational.eventQueue.add( function() { |
|
1139 dit.instance.trigger( 'reset:' + dit.key, dit.related, options ); |
|
1140 }); |
|
1141 }, |
|
1142 |
|
1143 tryAddRelated: function( model, coll, options ) { |
|
1144 var item = _.contains( this.keyIds, model.id ); |
|
1145 |
|
1146 if ( item ) { |
|
1147 this.addRelated( model, options ); |
|
1148 this.keyIds = _.without( this.keyIds, model.id ); |
|
1149 } |
|
1150 }, |
|
1151 |
|
1152 addRelated: function( model, options ) { |
|
1153 // Allow 'model' to set up its relations before proceeding. |
|
1154 // (which can result in a call to 'addRelated' from a relation of 'model') |
|
1155 var dit = this; |
|
1156 model.queue( function() { |
|
1157 if ( dit.related && !dit.related.get( model ) ) { |
|
1158 dit.related.add( model, _.defaults( { parse: false }, options ) ); |
|
1159 } |
|
1160 }); |
|
1161 }, |
|
1162 |
|
1163 removeRelated: function( model, coll, options ) { |
|
1164 if ( this.related.get( model ) ) { |
|
1165 this.related.remove( model, options ); |
|
1166 } |
|
1167 } |
|
1168 }); |
|
1169 |
|
1170 /** |
|
1171 * A type of Backbone.Model that also maintains relations to other models and collections. |
|
1172 * New events when compared to the original: |
|
1173 * - 'add:<key>' (model, related collection, options) |
|
1174 * - 'remove:<key>' (model, related collection, options) |
|
1175 * - 'change:<key>' (model, related model or collection, options) |
|
1176 */ |
|
1177 Backbone.RelationalModel = Backbone.Model.extend({ |
|
1178 relations: null, // Relation descriptions on the prototype |
|
1179 _relations: null, // Relation instances |
|
1180 _isInitialized: false, |
|
1181 _deferProcessing: false, |
|
1182 _queue: null, |
|
1183 _attributeChangeFired: false, // Keeps track of `change` event firing under some conditions (like nested `set`s) |
|
1184 |
|
1185 subModelTypeAttribute: 'type', |
|
1186 subModelTypes: null, |
|
1187 |
|
1188 constructor: function( attributes, options ) { |
|
1189 // Nasty hack, for cases like 'model.get( <HasMany key> ).add( item )'. |
|
1190 // Defer 'processQueue', so that when 'Relation.createModels' is used we trigger 'HasMany' |
|
1191 // collection events only after the model is really fully set up. |
|
1192 // Example: event for "p.on( 'add:jobs' )" -> "p.get('jobs').add( { company: c.id, person: p.id } )". |
|
1193 if ( options && options.collection ) { |
|
1194 var dit = this, |
|
1195 collection = this.collection = options.collection; |
|
1196 |
|
1197 // Prevent `collection` from cascading down to nested models; they shouldn't go into this `if` clause. |
|
1198 delete options.collection; |
|
1199 |
|
1200 this._deferProcessing = true; |
|
1201 |
|
1202 var processQueue = function( model ) { |
|
1203 if ( model === dit ) { |
|
1204 dit._deferProcessing = false; |
|
1205 dit.processQueue(); |
|
1206 collection.off( 'relational:add', processQueue ); |
|
1207 } |
|
1208 }; |
|
1209 collection.on( 'relational:add', processQueue ); |
|
1210 |
|
1211 // So we do process the queue eventually, regardless of whether this model actually gets added to 'options.collection'. |
|
1212 _.defer( function() { |
|
1213 processQueue( dit ); |
|
1214 }); |
|
1215 } |
|
1216 |
|
1217 Backbone.Relational.store.processOrphanRelations(); |
|
1218 Backbone.Relational.store.listenTo( this, 'relational:unregister', Backbone.Relational.store.unregister ); |
|
1219 |
|
1220 this._queue = new Backbone.BlockingQueue(); |
|
1221 this._queue.block(); |
|
1222 Backbone.Relational.eventQueue.block(); |
|
1223 |
|
1224 try { |
|
1225 Backbone.Model.apply( this, arguments ); |
|
1226 } |
|
1227 finally { |
|
1228 // Try to run the global queue holding external events |
|
1229 Backbone.Relational.eventQueue.unblock(); |
|
1230 } |
|
1231 }, |
|
1232 |
|
1233 /** |
|
1234 * Override 'trigger' to queue 'change' and 'change:*' events |
|
1235 */ |
|
1236 trigger: function( eventName ) { |
|
1237 if ( eventName.length > 5 && eventName.indexOf( 'change' ) === 0 ) { |
|
1238 var dit = this, |
|
1239 args = arguments; |
|
1240 |
|
1241 if ( !Backbone.Relational.eventQueue.isLocked() ) { |
|
1242 // If we're not in a more complicated nested scenario, fire the change event right away |
|
1243 Backbone.Model.prototype.trigger.apply( dit, args ); |
|
1244 } |
|
1245 else { |
|
1246 Backbone.Relational.eventQueue.add( function() { |
|
1247 // Determine if the `change` event is still valid, now that all relations are populated |
|
1248 var changed = true; |
|
1249 if ( eventName === 'change' ) { |
|
1250 // `hasChanged` may have gotten reset by nested calls to `set`. |
|
1251 changed = dit.hasChanged() || dit._attributeChangeFired; |
|
1252 dit._attributeChangeFired = false; |
|
1253 } |
|
1254 else { |
|
1255 var attr = eventName.slice( 7 ), |
|
1256 rel = dit.getRelation( attr ); |
|
1257 |
|
1258 if ( rel ) { |
|
1259 // If `attr` is a relation, `change:attr` get triggered from `Relation.onChange`. |
|
1260 // These take precedence over `change:attr` events triggered by `Model.set`. |
|
1261 // The relation sets a fourth attribute to `true`. If this attribute is present, |
|
1262 // continue triggering this event; otherwise, it's from `Model.set` and should be stopped. |
|
1263 changed = ( args[ 4 ] === true ); |
|
1264 |
|
1265 // If this event was triggered by a relation, set the right value in `this.changed` |
|
1266 // (a Collection or Model instead of raw data). |
|
1267 if ( changed ) { |
|
1268 dit.changed[ attr ] = args[ 2 ]; |
|
1269 } |
|
1270 // Otherwise, this event is from `Model.set`. If the relation doesn't report a change, |
|
1271 // remove attr from `dit.changed` so `hasChanged` doesn't take it into account. |
|
1272 else if ( !rel.changed ) { |
|
1273 delete dit.changed[ attr ]; |
|
1274 } |
|
1275 } |
|
1276 else if ( changed ) { |
|
1277 dit._attributeChangeFired = true; |
|
1278 } |
|
1279 } |
|
1280 |
|
1281 changed && Backbone.Model.prototype.trigger.apply( dit, args ); |
|
1282 }); |
|
1283 } |
|
1284 } |
|
1285 else if ( eventName === 'destroy' ) { |
|
1286 Backbone.Model.prototype.trigger.apply( this, arguments ); |
|
1287 Backbone.Relational.store.unregister( this ); |
|
1288 } |
|
1289 else { |
|
1290 Backbone.Model.prototype.trigger.apply( this, arguments ); |
|
1291 } |
|
1292 |
|
1293 return this; |
|
1294 }, |
|
1295 |
|
1296 /** |
|
1297 * Initialize Relations present in this.relations; determine the type (HasOne/HasMany), then creates a new instance. |
|
1298 * Invoked in the first call so 'set' (which is made from the Backbone.Model constructor). |
|
1299 */ |
|
1300 initializeRelations: function( options ) { |
|
1301 this.acquire(); // Setting up relations often also involve calls to 'set', and we only want to enter this function once |
|
1302 this._relations = {}; |
|
1303 |
|
1304 _.each( this.relations || [], function( rel ) { |
|
1305 Backbone.Relational.store.initializeRelation( this, rel, options ); |
|
1306 }, this ); |
|
1307 |
|
1308 this._isInitialized = true; |
|
1309 this.release(); |
|
1310 this.processQueue(); |
|
1311 }, |
|
1312 |
|
1313 /** |
|
1314 * When new values are set, notify this model's relations (also if options.silent is set). |
|
1315 * (called from `set`; Relation.setRelated locks this model before calling 'set' on it to prevent loops) |
|
1316 * @param {Object} [changedAttrs] |
|
1317 * @param {Object} [options] |
|
1318 */ |
|
1319 updateRelations: function( changedAttrs, options ) { |
|
1320 if ( this._isInitialized && !this.isLocked() ) { |
|
1321 _.each( this._relations, function( rel ) { |
|
1322 if ( !changedAttrs || ( rel.keySource in changedAttrs || rel.key in changedAttrs ) ) { |
|
1323 // Fetch data in `rel.keySource` if data got set in there, or `rel.key` otherwise |
|
1324 var value = this.attributes[ rel.keySource ] || this.attributes[ rel.key ], |
|
1325 attr = changedAttrs && ( changedAttrs[ rel.keySource ] || changedAttrs[ rel.key ] ); |
|
1326 |
|
1327 // Update a relation if its value differs from this model's attributes, or it's been explicitly nullified. |
|
1328 // Which can also happen before the originally intended related model has been found (`val` is null). |
|
1329 if ( rel.related !== value || ( value === null && attr === null ) ) { |
|
1330 this.trigger( 'relational:change:' + rel.key, this, value, options || {} ); |
|
1331 } |
|
1332 } |
|
1333 |
|
1334 // Explicitly clear 'keySource', to prevent a leaky abstraction if 'keySource' differs from 'key'. |
|
1335 if ( rel.keySource !== rel.key ) { |
|
1336 delete this.attributes[ rel.keySource ]; |
|
1337 } |
|
1338 }, this ); |
|
1339 } |
|
1340 }, |
|
1341 |
|
1342 /** |
|
1343 * Either add to the queue (if we're not initialized yet), or execute right away. |
|
1344 */ |
|
1345 queue: function( func ) { |
|
1346 this._queue.add( func ); |
|
1347 }, |
|
1348 |
|
1349 /** |
|
1350 * Process _queue |
|
1351 */ |
|
1352 processQueue: function() { |
|
1353 if ( this._isInitialized && !this._deferProcessing && this._queue.isBlocked() ) { |
|
1354 this._queue.unblock(); |
|
1355 } |
|
1356 }, |
|
1357 |
|
1358 /** |
|
1359 * Get a specific relation. |
|
1360 * @param {string} attr The relation key to look for. |
|
1361 * @return {Backbone.Relation} An instance of 'Backbone.Relation', if a relation was found for 'attr', or null. |
|
1362 */ |
|
1363 getRelation: function( attr ) { |
|
1364 return this._relations[ attr ]; |
|
1365 }, |
|
1366 |
|
1367 /** |
|
1368 * Get all of the created relations. |
|
1369 * @return {Backbone.Relation[]} |
|
1370 */ |
|
1371 getRelations: function() { |
|
1372 return _.values( this._relations ); |
|
1373 }, |
|
1374 |
|
1375 |
|
1376 /** |
|
1377 * Get a list of ids that will be fetched on a call to `getAsync`. |
|
1378 * @param {string|Backbone.Relation} attr The relation key to fetch models for. |
|
1379 * @param [refresh=false] Add ids for models that are already in the relation, refreshing them? |
|
1380 * @return {Array} An array of ids that need to be fetched. |
|
1381 */ |
|
1382 getIdsToFetch: function( attr, refresh ) { |
|
1383 var rel = attr instanceof Backbone.Relation ? attr : this.getRelation( attr ), |
|
1384 ids = rel ? ( rel.keyIds && rel.keyIds.slice( 0 ) ) || ( ( rel.keyId || rel.keyId === 0 ) ? [ rel.keyId ] : [] ) : []; |
|
1385 |
|
1386 // On `refresh`, add the ids for current models in the relation to `idsToFetch` |
|
1387 if ( refresh ) { |
|
1388 var models = rel.related && ( rel.related.models || [ rel.related ] ); |
|
1389 _.each( models, function( model ) { |
|
1390 if ( model.id || model.id === 0 ) { |
|
1391 ids.push( model.id ); |
|
1392 } |
|
1393 }); |
|
1394 } |
|
1395 |
|
1396 return ids; |
|
1397 }, |
|
1398 |
|
1399 /** |
|
1400 * Get related objects. Returns a single promise, which can either resolve immediately (if the related model[s]) |
|
1401 * are already present locally, or after fetching the contents of the requested attribute. |
|
1402 * @param {string} attr The relation key to fetch models for. |
|
1403 * @param {Object} [options] Options for 'Backbone.Model.fetch' and 'Backbone.sync'. |
|
1404 * @param {Boolean} [options.refresh=false] Fetch existing models from the server as well (in order to update them). |
|
1405 * @return {jQuery.Deferred} A jQuery promise object. When resolved, its `done` callback will be called with |
|
1406 * contents of `attr`. |
|
1407 */ |
|
1408 getAsync: function( attr, options ) { |
|
1409 // Set default `options` for fetch |
|
1410 options = _.extend( { add: true, remove: false, refresh: false }, options ); |
|
1411 |
|
1412 var dit = this, |
|
1413 requests = [], |
|
1414 rel = this.getRelation( attr ), |
|
1415 idsToFetch = rel && this.getIdsToFetch( rel, options.refresh ), |
|
1416 coll = rel.related instanceof Backbone.Collection ? rel.related : rel.relatedCollection; |
|
1417 |
|
1418 if ( idsToFetch && idsToFetch.length ) { |
|
1419 var models = [], |
|
1420 createdModels = [], |
|
1421 setUrl, |
|
1422 createModels = function() { |
|
1423 // Find (or create) a model for each one that is to be fetched |
|
1424 models = _.map( idsToFetch, function( id ) { |
|
1425 var model = rel.relatedModel.findModel( id ); |
|
1426 |
|
1427 if ( !model ) { |
|
1428 var attrs = {}; |
|
1429 attrs[ rel.relatedModel.prototype.idAttribute ] = id; |
|
1430 model = rel.relatedModel.findOrCreate( attrs, options ); |
|
1431 createdModels.push( model ); |
|
1432 } |
|
1433 |
|
1434 return model; |
|
1435 }, this ); |
|
1436 }; |
|
1437 |
|
1438 // Try if the 'collection' can provide a url to fetch a set of models in one request. |
|
1439 // This assumes that when 'Backbone.Collection.url' is a function, it can handle building of set urls. |
|
1440 // To make sure it can, test if the url we got by supplying a list of models to fetch is different from |
|
1441 // the one supplied for the default fetch action (without args to 'url'). |
|
1442 if ( coll instanceof Backbone.Collection && _.isFunction( coll.url ) ) { |
|
1443 var defaultUrl = coll.url(); |
|
1444 setUrl = coll.url( idsToFetch ); |
|
1445 |
|
1446 if ( setUrl === defaultUrl ) { |
|
1447 createModels(); |
|
1448 setUrl = coll.url( models ); |
|
1449 |
|
1450 if ( setUrl === defaultUrl ) { |
|
1451 setUrl = null; |
|
1452 } |
|
1453 } |
|
1454 } |
|
1455 |
|
1456 if ( setUrl ) { |
|
1457 // Do a single request to fetch all models |
|
1458 var opts = _.defaults( |
|
1459 { |
|
1460 error: function() { |
|
1461 _.each( createdModels, function( model ) { |
|
1462 model.trigger( 'destroy', model, model.collection, options ); |
|
1463 }); |
|
1464 |
|
1465 options.error && options.error.apply( models, arguments ); |
|
1466 }, |
|
1467 url: setUrl |
|
1468 }, |
|
1469 options |
|
1470 ); |
|
1471 |
|
1472 requests = [ coll.fetch( opts ) ]; |
|
1473 } |
|
1474 else { |
|
1475 // Make a request per model to fetch |
|
1476 if ( !models.length ) { |
|
1477 createModels(); |
|
1478 } |
|
1479 |
|
1480 requests = _.map( models, function( model ) { |
|
1481 var opts = _.defaults( |
|
1482 { |
|
1483 error: function() { |
|
1484 if ( _.contains( createdModels, model ) ) { |
|
1485 model.trigger( 'destroy', model, model.collection, options ); |
|
1486 } |
|
1487 options.error && options.error.apply( models, arguments ); |
|
1488 } |
|
1489 }, |
|
1490 options |
|
1491 ); |
|
1492 return model.fetch( opts ); |
|
1493 }, this ); |
|
1494 } |
|
1495 } |
|
1496 |
|
1497 return $.when.apply( null, requests ).then( |
|
1498 function() { |
|
1499 return Backbone.Model.prototype.get.call( dit, attr ); |
|
1500 } |
|
1501 ); |
|
1502 }, |
|
1503 |
|
1504 set: function( key, value, options ) { |
|
1505 Backbone.Relational.eventQueue.block(); |
|
1506 |
|
1507 // Duplicate backbone's behavior to allow separate key/value parameters, instead of a single 'attributes' object |
|
1508 var attributes, |
|
1509 result; |
|
1510 |
|
1511 if ( _.isObject( key ) || key == null ) { |
|
1512 attributes = key; |
|
1513 options = value; |
|
1514 } |
|
1515 else { |
|
1516 attributes = {}; |
|
1517 attributes[ key ] = value; |
|
1518 } |
|
1519 |
|
1520 try { |
|
1521 var id = this.id, |
|
1522 newId = attributes && this.idAttribute in attributes && attributes[ this.idAttribute ]; |
|
1523 |
|
1524 // Check if we're not setting a duplicate id before actually calling `set`. |
|
1525 Backbone.Relational.store.checkId( this, newId ); |
|
1526 |
|
1527 result = Backbone.Model.prototype.set.apply( this, arguments ); |
|
1528 |
|
1529 // Ideal place to set up relations, if this is the first time we're here for this model |
|
1530 if ( !this._isInitialized && !this.isLocked() ) { |
|
1531 this.constructor.initializeModelHierarchy(); |
|
1532 |
|
1533 // Only register models that have an id. A model will be registered when/if it gets an id later on. |
|
1534 if ( newId || newId === 0 ) { |
|
1535 Backbone.Relational.store.register( this ); |
|
1536 } |
|
1537 |
|
1538 this.initializeRelations( options ); |
|
1539 } |
|
1540 // The store should know about an `id` update asap |
|
1541 else if ( newId && newId !== id ) { |
|
1542 Backbone.Relational.store.update( this ); |
|
1543 } |
|
1544 |
|
1545 if ( attributes ) { |
|
1546 this.updateRelations( attributes, options ); |
|
1547 } |
|
1548 } |
|
1549 finally { |
|
1550 // Try to run the global queue holding external events |
|
1551 Backbone.Relational.eventQueue.unblock(); |
|
1552 } |
|
1553 |
|
1554 return result; |
|
1555 }, |
|
1556 |
|
1557 clone: function() { |
|
1558 var attributes = _.clone( this.attributes ); |
|
1559 if ( !_.isUndefined( attributes[ this.idAttribute ] ) ) { |
|
1560 attributes[ this.idAttribute ] = null; |
|
1561 } |
|
1562 |
|
1563 _.each( this.getRelations(), function( rel ) { |
|
1564 delete attributes[ rel.key ]; |
|
1565 }); |
|
1566 |
|
1567 return new this.constructor( attributes ); |
|
1568 }, |
|
1569 |
|
1570 /** |
|
1571 * Convert relations to JSON, omits them when required |
|
1572 */ |
|
1573 toJSON: function( options ) { |
|
1574 // If this Model has already been fully serialized in this branch once, return to avoid loops |
|
1575 if ( this.isLocked() ) { |
|
1576 return this.id; |
|
1577 } |
|
1578 |
|
1579 this.acquire(); |
|
1580 var json = Backbone.Model.prototype.toJSON.call( this, options ); |
|
1581 |
|
1582 if ( this.constructor._superModel && !( this.constructor._subModelTypeAttribute in json ) ) { |
|
1583 json[ this.constructor._subModelTypeAttribute ] = this.constructor._subModelTypeValue; |
|
1584 } |
|
1585 |
|
1586 _.each( this._relations, function( rel ) { |
|
1587 var related = json[ rel.key ], |
|
1588 includeInJSON = rel.options.includeInJSON, |
|
1589 value = null; |
|
1590 |
|
1591 if ( includeInJSON === true ) { |
|
1592 if ( related && _.isFunction( related.toJSON ) ) { |
|
1593 value = related.toJSON( options ); |
|
1594 } |
|
1595 } |
|
1596 else if ( _.isString( includeInJSON ) ) { |
|
1597 if ( related instanceof Backbone.Collection ) { |
|
1598 value = related.pluck( includeInJSON ); |
|
1599 } |
|
1600 else if ( related instanceof Backbone.Model ) { |
|
1601 value = related.get( includeInJSON ); |
|
1602 } |
|
1603 |
|
1604 // Add ids for 'unfound' models if includeInJSON is equal to (only) the relatedModel's `idAttribute` |
|
1605 if ( includeInJSON === rel.relatedModel.prototype.idAttribute ) { |
|
1606 if ( rel instanceof Backbone.HasMany ) { |
|
1607 value = value.concat( rel.keyIds ); |
|
1608 } |
|
1609 else if ( rel instanceof Backbone.HasOne ) { |
|
1610 value = value || rel.keyId; |
|
1611 |
|
1612 if ( !value && !_.isObject( rel.keyContents ) ) { |
|
1613 value = rel.keyContents || null; |
|
1614 } |
|
1615 } |
|
1616 } |
|
1617 } |
|
1618 else if ( _.isArray( includeInJSON ) ) { |
|
1619 if ( related instanceof Backbone.Collection ) { |
|
1620 value = []; |
|
1621 related.each( function( model ) { |
|
1622 var curJson = {}; |
|
1623 _.each( includeInJSON, function( key ) { |
|
1624 curJson[ key ] = model.get( key ); |
|
1625 }); |
|
1626 value.push( curJson ); |
|
1627 }); |
|
1628 } |
|
1629 else if ( related instanceof Backbone.Model ) { |
|
1630 value = {}; |
|
1631 _.each( includeInJSON, function( key ) { |
|
1632 value[ key ] = related.get( key ); |
|
1633 }); |
|
1634 } |
|
1635 } |
|
1636 else { |
|
1637 delete json[ rel.key ]; |
|
1638 } |
|
1639 |
|
1640 // In case of `wait: true`, Backbone will simply push whatever's passed into `save` into attributes. |
|
1641 // We'll want to get this information into the JSON, even if it doesn't conform to our normal |
|
1642 // expectations of what's contained in it (no model/collection for a relation, etc). |
|
1643 if ( value === null && options && options.wait ) { |
|
1644 value = related; |
|
1645 } |
|
1646 |
|
1647 if ( includeInJSON ) { |
|
1648 json[ rel.keyDestination ] = value; |
|
1649 } |
|
1650 |
|
1651 if ( rel.keyDestination !== rel.key ) { |
|
1652 delete json[ rel.key ]; |
|
1653 } |
|
1654 }); |
|
1655 |
|
1656 this.release(); |
|
1657 return json; |
|
1658 } |
|
1659 }, |
|
1660 { |
|
1661 /** |
|
1662 * |
|
1663 * @param superModel |
|
1664 * @returns {Backbone.RelationalModel.constructor} |
|
1665 */ |
|
1666 setup: function( superModel ) { |
|
1667 // We don't want to share a relations array with a parent, as this will cause problems with reverse |
|
1668 // relations. Since `relations` may also be a property or function, only use slice if we have an array. |
|
1669 this.prototype.relations = ( this.prototype.relations || [] ).slice( 0 ); |
|
1670 |
|
1671 this._subModels = {}; |
|
1672 this._superModel = null; |
|
1673 |
|
1674 // If this model has 'subModelTypes' itself, remember them in the store |
|
1675 if ( this.prototype.hasOwnProperty( 'subModelTypes' ) ) { |
|
1676 Backbone.Relational.store.addSubModels( this.prototype.subModelTypes, this ); |
|
1677 } |
|
1678 // The 'subModelTypes' property should not be inherited, so reset it. |
|
1679 else { |
|
1680 this.prototype.subModelTypes = null; |
|
1681 } |
|
1682 |
|
1683 // Initialize all reverseRelations that belong to this new model. |
|
1684 _.each( this.prototype.relations || [], function( rel ) { |
|
1685 if ( !rel.model ) { |
|
1686 rel.model = this; |
|
1687 } |
|
1688 |
|
1689 if ( rel.reverseRelation && rel.model === this ) { |
|
1690 var preInitialize = true; |
|
1691 if ( _.isString( rel.relatedModel ) ) { |
|
1692 /** |
|
1693 * The related model might not be defined for two reasons |
|
1694 * 1. it is related to itself |
|
1695 * 2. it never gets defined, e.g. a typo |
|
1696 * 3. the model hasn't been defined yet, but will be later |
|
1697 * In neither of these cases do we need to pre-initialize reverse relations. |
|
1698 * However, for 3. (which is, to us, indistinguishable from 2.), we do need to attempt |
|
1699 * setting up this relation again later, in case the related model is defined later. |
|
1700 */ |
|
1701 var relatedModel = Backbone.Relational.store.getObjectByName( rel.relatedModel ); |
|
1702 preInitialize = relatedModel && ( relatedModel.prototype instanceof Backbone.RelationalModel ); |
|
1703 } |
|
1704 |
|
1705 if ( preInitialize ) { |
|
1706 Backbone.Relational.store.initializeRelation( null, rel ); |
|
1707 } |
|
1708 else if ( _.isString( rel.relatedModel ) ) { |
|
1709 Backbone.Relational.store.addOrphanRelation( rel ); |
|
1710 } |
|
1711 } |
|
1712 }, this ); |
|
1713 |
|
1714 return this; |
|
1715 }, |
|
1716 |
|
1717 /** |
|
1718 * Create a 'Backbone.Model' instance based on 'attributes'. |
|
1719 * @param {Object} attributes |
|
1720 * @param {Object} [options] |
|
1721 * @return {Backbone.Model} |
|
1722 */ |
|
1723 build: function( attributes, options ) { |
|
1724 // 'build' is a possible entrypoint; it's possible no model hierarchy has been determined yet. |
|
1725 this.initializeModelHierarchy(); |
|
1726 |
|
1727 // Determine what type of (sub)model should be built if applicable. |
|
1728 var model = this._findSubModelType( this, attributes ) || this; |
|
1729 |
|
1730 return new model( attributes, options ); |
|
1731 }, |
|
1732 |
|
1733 /** |
|
1734 * Determines what type of (sub)model should be built if applicable. |
|
1735 * Looks up the proper subModelType in 'this._subModels', recursing into |
|
1736 * types until a match is found. Returns the applicable 'Backbone.Model' |
|
1737 * or null if no match is found. |
|
1738 * @param {Backbone.Model} type |
|
1739 * @param {Object} attributes |
|
1740 * @return {Backbone.Model} |
|
1741 */ |
|
1742 _findSubModelType: function( type, attributes ) { |
|
1743 if ( type._subModels && type.prototype.subModelTypeAttribute in attributes ) { |
|
1744 var subModelTypeAttribute = attributes[ type.prototype.subModelTypeAttribute ]; |
|
1745 var subModelType = type._subModels[ subModelTypeAttribute ]; |
|
1746 if ( subModelType ) { |
|
1747 return subModelType; |
|
1748 } |
|
1749 else { |
|
1750 // Recurse into subModelTypes to find a match |
|
1751 for ( subModelTypeAttribute in type._subModels ) { |
|
1752 subModelType = this._findSubModelType( type._subModels[ subModelTypeAttribute ], attributes ); |
|
1753 if ( subModelType ) { |
|
1754 return subModelType; |
|
1755 } |
|
1756 } |
|
1757 } |
|
1758 } |
|
1759 |
|
1760 return null; |
|
1761 }, |
|
1762 |
|
1763 /** |
|
1764 * |
|
1765 */ |
|
1766 initializeModelHierarchy: function() { |
|
1767 // Inherit any relations that have been defined in the parent model. |
|
1768 this.inheritRelations(); |
|
1769 |
|
1770 // If we came here through 'build' for a model that has 'subModelTypes' then try to initialize the ones that |
|
1771 // haven't been resolved yet. |
|
1772 if ( this.prototype.subModelTypes ) { |
|
1773 var resolvedSubModels = _.keys( this._subModels ); |
|
1774 var unresolvedSubModels = _.omit( this.prototype.subModelTypes, resolvedSubModels ); |
|
1775 _.each( unresolvedSubModels, function( subModelTypeName ) { |
|
1776 var subModelType = Backbone.Relational.store.getObjectByName( subModelTypeName ); |
|
1777 subModelType && subModelType.initializeModelHierarchy(); |
|
1778 }); |
|
1779 } |
|
1780 }, |
|
1781 |
|
1782 inheritRelations: function() { |
|
1783 // Bail out if we've been here before. |
|
1784 if ( !_.isUndefined( this._superModel ) && !_.isNull( this._superModel ) ) { |
|
1785 return; |
|
1786 } |
|
1787 // Try to initialize the _superModel. |
|
1788 Backbone.Relational.store.setupSuperModel( this ); |
|
1789 |
|
1790 // If a superModel has been found, copy relations from the _superModel if they haven't been inherited automatically |
|
1791 // (due to a redefinition of 'relations'). |
|
1792 if ( this._superModel ) { |
|
1793 // The _superModel needs a chance to initialize its own inherited relations before we attempt to inherit relations |
|
1794 // from the _superModel. You don't want to call 'initializeModelHierarchy' because that could cause sub-models of |
|
1795 // this class to inherit their relations before this class has had chance to inherit it's relations. |
|
1796 this._superModel.inheritRelations(); |
|
1797 if ( this._superModel.prototype.relations ) { |
|
1798 // Find relations that exist on the '_superModel', but not yet on this model. |
|
1799 var inheritedRelations = _.filter( this._superModel.prototype.relations || [], function( superRel ) { |
|
1800 return !_.any( this.prototype.relations || [], function( rel ) { |
|
1801 return superRel.relatedModel === rel.relatedModel && superRel.key === rel.key; |
|
1802 }, this ); |
|
1803 }, this ); |
|
1804 |
|
1805 this.prototype.relations = inheritedRelations.concat( this.prototype.relations ); |
|
1806 } |
|
1807 } |
|
1808 // Otherwise, make sure we don't get here again for this type by making '_superModel' false so we fail the |
|
1809 // isUndefined/isNull check next time. |
|
1810 else { |
|
1811 this._superModel = false; |
|
1812 } |
|
1813 }, |
|
1814 |
|
1815 /** |
|
1816 * Find an instance of `this` type in 'Backbone.Relational.store'. |
|
1817 * A new model is created if no matching model is found, `attributes` is an object, and `options.create` is true. |
|
1818 * - If `attributes` is a string or a number, `findOrCreate` will query the `store` and return a model if found. |
|
1819 * - If `attributes` is an object and is found in the store, the model will be updated with `attributes` unless `options.merge` is `false`. |
|
1820 * @param {Object|String|Number} attributes Either a model's id, or the attributes used to create or update a model. |
|
1821 * @param {Object} [options] |
|
1822 * @param {Boolean} [options.create=true] |
|
1823 * @param {Boolean} [options.merge=true] |
|
1824 * @param {Boolean} [options.parse=false] |
|
1825 * @return {Backbone.RelationalModel} |
|
1826 */ |
|
1827 findOrCreate: function( attributes, options ) { |
|
1828 options || ( options = {} ); |
|
1829 var parsedAttributes = ( _.isObject( attributes ) && options.parse && this.prototype.parse ) ? |
|
1830 this.prototype.parse( _.clone( attributes ) ) : attributes; |
|
1831 |
|
1832 // If specified, use a custom `find` function to match up existing models to the given attributes. |
|
1833 // Otherwise, try to find an instance of 'this' model type in the store |
|
1834 var model = this.findModel( parsedAttributes ); |
|
1835 |
|
1836 // If we found an instance, update it with the data in 'item' (unless 'options.merge' is false). |
|
1837 // If not, create an instance (unless 'options.create' is false). |
|
1838 if ( _.isObject( attributes ) ) { |
|
1839 if ( model && options.merge !== false ) { |
|
1840 // Make sure `options.collection` and `options.url` doesn't cascade to nested models |
|
1841 delete options.collection; |
|
1842 delete options.url; |
|
1843 |
|
1844 model.set( parsedAttributes, options ); |
|
1845 } |
|
1846 else if ( !model && options.create !== false ) { |
|
1847 model = this.build( parsedAttributes, _.defaults( { parse: false }, options ) ); |
|
1848 } |
|
1849 } |
|
1850 |
|
1851 return model; |
|
1852 }, |
|
1853 |
|
1854 /** |
|
1855 * Find an instance of `this` type in 'Backbone.Relational.store'. |
|
1856 * - If `attributes` is a string or a number, `find` will query the `store` and return a model if found. |
|
1857 * - If `attributes` is an object and is found in the store, the model will be updated with `attributes` unless `options.merge` is `false`. |
|
1858 * @param {Object|String|Number} attributes Either a model's id, or the attributes used to create or update a model. |
|
1859 * @param {Object} [options] |
|
1860 * @param {Boolean} [options.merge=true] |
|
1861 * @param {Boolean} [options.parse=false] |
|
1862 * @return {Backbone.RelationalModel} |
|
1863 */ |
|
1864 find: function( attributes, options ) { |
|
1865 options || ( options = {} ); |
|
1866 options.create = false; |
|
1867 return this.findOrCreate( attributes, options ); |
|
1868 }, |
|
1869 |
|
1870 /** |
|
1871 * A hook to override the matching when updating (or creating) a model. |
|
1872 * The default implementation is to look up the model by id in the store. |
|
1873 * @param {Object} attributes |
|
1874 * @returns {Backbone.RelationalModel} |
|
1875 */ |
|
1876 findModel: function( attributes ) { |
|
1877 return Backbone.Relational.store.find( this, attributes ); |
|
1878 } |
|
1879 }); |
|
1880 _.extend( Backbone.RelationalModel.prototype, Backbone.Semaphore ); |
|
1881 |
|
1882 /** |
|
1883 * Override Backbone.Collection._prepareModel, so objects will be built using the correct type |
|
1884 * if the collection.model has subModels. |
|
1885 * Attempts to find a model for `attrs` in Backbone.store through `findOrCreate` |
|
1886 * (which sets the new properties on it if found), or instantiates a new model. |
|
1887 */ |
|
1888 Backbone.Collection.prototype.__prepareModel = Backbone.Collection.prototype._prepareModel; |
|
1889 Backbone.Collection.prototype._prepareModel = function( attrs, options ) { |
|
1890 var model; |
|
1891 |
|
1892 if ( attrs instanceof Backbone.Model ) { |
|
1893 if ( !attrs.collection ) { |
|
1894 attrs.collection = this; |
|
1895 } |
|
1896 model = attrs; |
|
1897 } |
|
1898 else { |
|
1899 options = options ? _.clone( options ) : {}; |
|
1900 options.collection = this; |
|
1901 |
|
1902 if ( typeof this.model.findOrCreate !== 'undefined' ) { |
|
1903 model = this.model.findOrCreate( attrs, options ); |
|
1904 } |
|
1905 else { |
|
1906 model = new this.model( attrs, options ); |
|
1907 } |
|
1908 |
|
1909 if ( model && model.validationError ) { |
|
1910 this.trigger( 'invalid', this, attrs, options ); |
|
1911 model = false; |
|
1912 } |
|
1913 } |
|
1914 |
|
1915 return model; |
|
1916 }; |
|
1917 |
|
1918 |
|
1919 /** |
|
1920 * Override Backbone.Collection.set, so we'll create objects from attributes where required, |
|
1921 * and update the existing models. Also, trigger 'relational:add'. |
|
1922 */ |
|
1923 var set = Backbone.Collection.prototype.__set = Backbone.Collection.prototype.set; |
|
1924 Backbone.Collection.prototype.set = function( models, options ) { |
|
1925 // Short-circuit if this Collection doesn't hold RelationalModels |
|
1926 if ( !( this.model.prototype instanceof Backbone.RelationalModel ) ) { |
|
1927 return set.call( this, models, options ); |
|
1928 } |
|
1929 |
|
1930 if ( options && options.parse ) { |
|
1931 models = this.parse( models, options ); |
|
1932 } |
|
1933 |
|
1934 var singular = !_.isArray( models ), |
|
1935 newModels = [], |
|
1936 toAdd = [], |
|
1937 model = null; |
|
1938 |
|
1939 models = singular ? ( models ? [ models ] : [] ) : _.clone( models ); |
|
1940 |
|
1941 //console.debug( 'calling add on coll=%o; model=%o, options=%o', this, models, options ); |
|
1942 for ( var i = 0; i < models.length; i++ ) { |
|
1943 model = models[i]; |
|
1944 if ( !( model instanceof Backbone.Model ) ) { |
|
1945 model = Backbone.Collection.prototype._prepareModel.call( this, model, options ); |
|
1946 } |
|
1947 if ( model ) { |
|
1948 toAdd.push( model ); |
|
1949 if ( !( this.get( model ) || this.get( model.cid ) ) ) { |
|
1950 newModels.push( model ); |
|
1951 } |
|
1952 // If we arrive in `add` while performing a `set` (after a create, so the model gains an `id`), |
|
1953 // we may get here before `_onModelEvent` has had the chance to update `_byId`. |
|
1954 else if ( model.id !== null && model.id !== undefined ) { |
|
1955 this._byId[ model.id ] = model; |
|
1956 } |
|
1957 } |
|
1958 } |
|
1959 |
|
1960 // Add 'models' in a single batch, so the original add will only be called once (and thus 'sort', etc). |
|
1961 // If `parse` was specified, the collection and contained models have been parsed now. |
|
1962 toAdd = singular ? ( toAdd.length ? toAdd[ 0 ] : null ) : toAdd; |
|
1963 var result = set.call( this, toAdd, _.defaults( { merge: false, parse: false }, options ) ); |
|
1964 |
|
1965 for ( i = 0; i < newModels.length; i++ ) { |
|
1966 model = newModels[i]; |
|
1967 // Fire a `relational:add` event for any model in `newModels` that has actually been added to the collection. |
|
1968 if ( this.get( model ) || this.get( model.cid ) ) { |
|
1969 this.trigger( 'relational:add', model, this, options ); |
|
1970 } |
|
1971 } |
|
1972 |
|
1973 return result; |
|
1974 }; |
|
1975 |
|
1976 /** |
|
1977 * Override 'Backbone.Collection.remove' to trigger 'relational:remove'. |
|
1978 */ |
|
1979 var remove = Backbone.Collection.prototype.__remove = Backbone.Collection.prototype.remove; |
|
1980 Backbone.Collection.prototype.remove = function( models, options ) { |
|
1981 // Short-circuit if this Collection doesn't hold RelationalModels |
|
1982 if ( !( this.model.prototype instanceof Backbone.RelationalModel ) ) { |
|
1983 return remove.call( this, models, options ); |
|
1984 } |
|
1985 |
|
1986 var singular = !_.isArray( models ), |
|
1987 toRemove = []; |
|
1988 |
|
1989 models = singular ? ( models ? [ models ] : [] ) : _.clone( models ); |
|
1990 options || ( options = {} ); |
|
1991 |
|
1992 //console.debug('calling remove on coll=%o; models=%o, options=%o', this, models, options ); |
|
1993 _.each( models, function( model ) { |
|
1994 model = this.get( model ) || ( model && this.get( model.cid ) ); |
|
1995 model && toRemove.push( model ); |
|
1996 }, this ); |
|
1997 |
|
1998 var result = remove.call( this, singular ? ( toRemove.length ? toRemove[ 0 ] : null ) : toRemove, options ); |
|
1999 |
|
2000 _.each( toRemove, function( model ) { |
|
2001 this.trigger( 'relational:remove', model, this, options ); |
|
2002 }, this ); |
|
2003 |
|
2004 return result; |
|
2005 }; |
|
2006 |
|
2007 /** |
|
2008 * Override 'Backbone.Collection.reset' to trigger 'relational:reset'. |
|
2009 */ |
|
2010 var reset = Backbone.Collection.prototype.__reset = Backbone.Collection.prototype.reset; |
|
2011 Backbone.Collection.prototype.reset = function( models, options ) { |
|
2012 options = _.extend( { merge: true }, options ); |
|
2013 var result = reset.call( this, models, options ); |
|
2014 |
|
2015 if ( this.model.prototype instanceof Backbone.RelationalModel ) { |
|
2016 this.trigger( 'relational:reset', this, options ); |
|
2017 } |
|
2018 |
|
2019 return result; |
|
2020 }; |
|
2021 |
|
2022 /** |
|
2023 * Override 'Backbone.Collection.sort' to trigger 'relational:reset'. |
|
2024 */ |
|
2025 var sort = Backbone.Collection.prototype.__sort = Backbone.Collection.prototype.sort; |
|
2026 Backbone.Collection.prototype.sort = function( options ) { |
|
2027 var result = sort.call( this, options ); |
|
2028 |
|
2029 if ( this.model.prototype instanceof Backbone.RelationalModel ) { |
|
2030 this.trigger( 'relational:reset', this, options ); |
|
2031 } |
|
2032 |
|
2033 return result; |
|
2034 }; |
|
2035 |
|
2036 /** |
|
2037 * Override 'Backbone.Collection.trigger' so 'add', 'remove' and 'reset' events are queued until relations |
|
2038 * are ready. |
|
2039 */ |
|
2040 var trigger = Backbone.Collection.prototype.__trigger = Backbone.Collection.prototype.trigger; |
|
2041 Backbone.Collection.prototype.trigger = function( eventName ) { |
|
2042 // Short-circuit if this Collection doesn't hold RelationalModels |
|
2043 if ( !( this.model.prototype instanceof Backbone.RelationalModel ) ) { |
|
2044 return trigger.apply( this, arguments ); |
|
2045 } |
|
2046 |
|
2047 if ( eventName === 'add' || eventName === 'remove' || eventName === 'reset' || eventName === 'sort' ) { |
|
2048 var dit = this, |
|
2049 args = arguments; |
|
2050 |
|
2051 if ( _.isObject( args[ 3 ] ) ) { |
|
2052 args = _.toArray( args ); |
|
2053 // the fourth argument is the option object. |
|
2054 // we need to clone it, as it could be modified while we wait on the eventQueue to be unblocked |
|
2055 args[ 3 ] = _.clone( args[ 3 ] ); |
|
2056 } |
|
2057 |
|
2058 Backbone.Relational.eventQueue.add( function() { |
|
2059 trigger.apply( dit, args ); |
|
2060 }); |
|
2061 } |
|
2062 else { |
|
2063 trigger.apply( this, arguments ); |
|
2064 } |
|
2065 |
|
2066 return this; |
|
2067 }; |
|
2068 |
|
2069 // Override .extend() to automatically call .setup() |
|
2070 Backbone.RelationalModel.extend = function( protoProps, classProps ) { |
|
2071 var child = Backbone.Model.extend.call( this, protoProps, classProps ); |
|
2072 |
|
2073 child.setup( this ); |
|
2074 |
|
2075 return child; |
|
2076 }; |
|
2077 })); |