server/src/main/webapp/static/lib/backbone-relational.js
changeset 51 3247fccfbd3f
parent 47 267d67791e05
equal deleted inserted replaced
50:7b517a54b708 51:3247fccfbd3f
     1 /* vim: set tabstop=4:softtabstop=4:shiftwidth=4:noexpandtab */
     1 /* vim: set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */
     2 /**
     2 /**
     3  * Backbone-relational.js 0.7.0
     3  * Backbone-relational.js 0.7.0
     4  * (c) 2011-2013 Paul Uithol
     4  * (c) 2011-2013 Paul Uithol
     5  * 
     5  * 
     6  * Backbone-relational may be freely distributed under the MIT license; see the accompanying LICENSE.txt.
     6  * Backbone-relational may be freely distributed under the MIT license; see the accompanying LICENSE.txt.
  1525 		},
  1525 		},
  1526 
  1526 
  1527 		/**
  1527 		/**
  1528 		 * Find an instance of `this` type in 'Backbone.Relational.store'.
  1528 		 * Find an instance of `this` type in 'Backbone.Relational.store'.
  1529 		 * - If `attributes` is a string or a number, `findOrCreate` will just query the `store` and return a model if found.
  1529 		 * - If `attributes` is a string or a number, `findOrCreate` will just query the `store` and return a model if found.
  1530 		 * - If `attributes` is an object, the model will be updated with `attributes` if found.
  1530 		 * - If `attributes` is an object and is found in the store, the model will be updated with `attributes` unless `options.update` is `false`. 
  1531 		 *   Otherwise, a new model is created with `attributes` (unless `options.create` is explicitly set to `false`).
  1531 		 *   Otherwise, a new model is created with `attributes` (unless `options.create` is explicitly set to `false`).
  1532 		 * @param {Object|String|Number} attributes Either a model's id, or the attributes used to create or update a model.
  1532 		 * @param {Object|String|Number} attributes Either a model's id, or the attributes used to create or update a model.
  1533 		 * @param {Object} [options]
  1533 		 * @param {Object} [options.create=true, options.update=true]
  1534 		 * @param {Boolean} [options.create=true]
       
  1535 		 * @return {Backbone.RelationalModel}
  1534 		 * @return {Backbone.RelationalModel}
  1536 		 */
  1535 		 */
  1537 		findOrCreate: function( attributes, options ) {
  1536 		findOrCreate: function( attributes, options ) {
       
  1537 			options || ( options = {} );
  1538 			var parsedAttributes = (_.isObject( attributes ) && this.prototype.parse) ? this.prototype.parse( attributes ) : attributes;
  1538 			var parsedAttributes = (_.isObject( attributes ) && this.prototype.parse) ? this.prototype.parse( attributes ) : attributes;
  1539 			// Try to find an instance of 'this' model type in the store
  1539 			// Try to find an instance of 'this' model type in the store
  1540 			var model = Backbone.Relational.store.find( this, parsedAttributes );
  1540 			var model = Backbone.Relational.store.find( this, parsedAttributes );
  1541 
  1541 
  1542 			// If we found an instance, update it with the data in 'item'; if not, create an instance
  1542 			// If we found an instance, update it with the data in 'item' (unless 'options.update' is false).
  1543 			// (unless 'options.create' is false).
  1543 			// If not, create an instance (unless 'options.create' is false).
  1544 			if ( _.isObject( attributes ) ) {
  1544 			if ( _.isObject( attributes ) ) {
  1545 				if ( model ) {
  1545 				if ( model && options.update !== false ) {
  1546 					model.set( parsedAttributes, options );
  1546 					model.set( parsedAttributes, options );
  1547 				}
  1547 				}
  1548 				else if ( !options || ( options && options.create !== false ) ) {
  1548 				else if ( !model && options.create !== false ) {
  1549 					model = this.build( attributes, options );
  1549 					model = this.build( attributes, options );
  1550 				}
  1550 				}
  1551 			}
  1551 			}
  1552 
  1552 
  1553 			return model;
  1553 			return model;