server/src/main/webapp/static/lib/backbone-relational.js
author ymh <ymh.work@gmail.com>
Mon, 14 Jan 2013 16:00:07 +0100
changeset 51 3247fccfbd3f
parent 47 267d67791e05
permissions -rw-r--r--
update on renkan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
     1
/* vim: set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab: */
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
     3
 * Backbone-relational.js 0.7.0
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
     4
 * (c) 2011-2013 Paul Uithol
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * 
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * Backbone-relational may be freely distributed under the MIT license; see the accompanying LICENSE.txt.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For details and documentation: https://github.com/PaulUithol/Backbone-relational.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * Depends on Backbone (and thus on Underscore as well): https://github.com/documentcloud/backbone.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
( function( undefined ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
	"use strict";
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
	 * CommonJS shim
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
	 **/
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	var _, Backbone, exports;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	if ( typeof window === 'undefined' ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
		_ = require( 'underscore' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
		Backbone = require( 'backbone' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
		exports = module.exports = Backbone;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
	}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
	else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
		_ = window._;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
		Backbone = window.Backbone;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		exports = window;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	Backbone.Relational = {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
		showWarnings: true
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	 * Semaphore mixin; can be used as both binary and counting.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
	 **/
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	Backbone.Semaphore = {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
		_permitsAvailable: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
		_permitsUsed: 0,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
		acquire: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
			if ( this._permitsAvailable && this._permitsUsed >= this._permitsAvailable ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
				throw new Error( 'Max permits acquired' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
				this._permitsUsed++;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		release: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
			if ( this._permitsUsed === 0 ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
				throw new Error( 'All permits released' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
			else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
				this._permitsUsed--;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		isLocked: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
			return this._permitsUsed > 0;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
		setAvailablePermits: function( amount ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
			if ( this._permitsUsed > amount ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
				throw new Error( 'Available permits cannot be less than used permits' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
			this._permitsAvailable = amount;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	 * A BlockingQueue that accumulates items while blocked (via 'block'),
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	 * and processes them when unblocked (via 'unblock').
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	 * Process can also be called manually (via 'process').
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	Backbone.BlockingQueue = function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		this._queue = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	_.extend( Backbone.BlockingQueue.prototype, Backbone.Semaphore, {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		_queue: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		add: function( func ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
			if ( this.isBlocked() ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
				this._queue.push( func );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
			else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
				func();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		process: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
			while ( this._queue && this._queue.length ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
				this._queue.shift()();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
		block: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
			this.acquire();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		unblock: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			this.release();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
			if ( !this.isBlocked() ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
				this.process();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		isBlocked: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
			return this.isLocked();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	 * Global event queue. Accumulates external events ('add:<key>', 'remove:<key>' and 'update:<key>')
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
	 * until the top-level object is fully initialized (see 'Backbone.RelationalModel').
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
	Backbone.Relational.eventQueue = new Backbone.BlockingQueue();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
	 * Backbone.Store keeps track of all created (and destruction of) Backbone.RelationalModel.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	 * Handles lookup for relations.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
	Backbone.Store = function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		this._collections = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		this._reverseRelations = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		this._subModels = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		this._modelScopes = [ exports ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
	_.extend( Backbone.Store.prototype, Backbone.Events, {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
		addModelScope: function( scope ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
			this._modelScopes.push( scope );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
		 * Add a set of subModelTypes to the store, that can be used to resolve the '_superModel'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		 * for a model later in 'setupSuperModel'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
		 *
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
		 * @param {Backbone.RelationalModel} subModelTypes
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
		 * @param {Backbone.RelationalModel} superModelType
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		addSubModels: function( subModelTypes, superModelType ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
			this._subModels.push({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
				'superModelType': superModelType,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
				'subModels': subModelTypes
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
			});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
		 * Check if the given modelType is registered as another model's subModel. If so, add it to the super model's
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		 * '_subModels', and set the modelType's '_superModel', '_subModelTypeName', and '_subModelTypeAttribute'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		 *
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		 * @param {Backbone.RelationalModel} modelType
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		setupSuperModel: function( modelType ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   152
			_.find( this._subModels || [], function( subModelDef ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   153
				return _.find( subModelDef.subModels || [], function( subModelTypeName, typeValue ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
					var subModelType = this.getObjectByName( subModelTypeName );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
					if ( modelType === subModelType ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
						// Set 'modelType' as a child of the found superModel
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
						subModelDef.superModelType._subModels[ typeValue ] = modelType;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
						// Set '_superModel', '_subModelTypeValue', and '_subModelTypeAttribute' on 'modelType'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
						modelType._superModel = subModelDef.superModelType;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
						modelType._subModelTypeValue = typeValue;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
						modelType._subModelTypeAttribute = subModelDef.superModelType.prototype.subModelTypeAttribute;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
						return true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
			}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		 * Add a reverse relation. Is added to the 'relations' property on model's prototype, and to
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
		 * existing instances of 'model' in the store as well.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
		 * @param {Object} relation
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		 * @param {Backbone.RelationalModel} relation.model
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		 * @param {String} relation.type
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		 * @param {String} relation.key
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
		 * @param {String|Object} relation.relatedModel
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		addReverseRelation: function( relation ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   180
			var exists = _.any( this._reverseRelations || [], function( rel ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   181
					return _.all( relation || [], function( val, key ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
							return val === rel[ key ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
						});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
			if ( !exists && relation.model && relation.type ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
				this._reverseRelations.push( relation );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
				var addRelation = function( model, relation ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
					if ( !model.prototype.relations ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
						model.prototype.relations = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
					model.prototype.relations.push( relation );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
					
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   195
					_.each( model._subModels || [], function( subModel ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
							addRelation( subModel, relation );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
						}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
				};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
				addRelation( relation.model, relation );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
				this.retroFitRelation( relation );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		 * Add a 'relation' to all existing instances of 'relation.model' in the store
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
		 * @param {Object} relation
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		retroFitRelation: function( relation ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
			var coll = this.getCollection( relation.model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
			coll.each( function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
				if ( !( model instanceof relation.model ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
					return;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
				new relation.type( model, relation );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
			}, this);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
		 * Find the Store's collection for a certain type of model.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
		 * @param {Backbone.RelationalModel} model
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
		 * @return {Backbone.Collection} A collection if found (or applicable for 'model'), or null
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
		getCollection: function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
			if ( model instanceof Backbone.RelationalModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
				model = model.constructor;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
			var rootModel = model;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
			while ( rootModel._superModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
				rootModel = rootModel._superModel;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
			var coll = _.detect( this._collections, function( c ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
					return c.model === rootModel;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
			if ( !coll ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
				coll = this._createCollection( rootModel );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
			return coll;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
		 * Find a type on the global object by name. Splits name on dots.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
		 * @param {String} name
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
		 * @return {Object}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		getObjectByName: function( name ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
			var parts = name.split( '.' ),
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
				type = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   256
			_.find( this._modelScopes || [], function( scope ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   257
				type = _.reduce( parts || [], function( memo, val ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   258
					return memo ? memo[ val ] : undefined;
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
				}, scope );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
				if ( type && type !== scope ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
					return true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
			}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
			return type;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
		_createCollection: function( type ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
			var coll;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
			// If 'type' is an instance, take its constructor
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
			if ( type instanceof Backbone.RelationalModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
				type = type.constructor;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
			// Type should inherit from Backbone.RelationalModel.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
			if ( type.prototype instanceof Backbone.RelationalModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
				coll = new Backbone.Collection();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
				coll.model = type;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
				this._collections.push( coll );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
			return coll;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
		 * Find the attribute that is to be used as the `id` on a given object
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
		 * @param type
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
		 * @param {String|Number|Object|Backbone.RelationalModel} item
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
		 * @return {String|Number}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
		resolveIdForItem: function( type, item ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
			var id = _.isString( item ) || _.isNumber( item ) ? item : null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
			if ( id === null ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
				if ( item instanceof Backbone.RelationalModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
					id = item.id;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
				else if ( _.isObject( item ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
					id = item[ type.prototype.idAttribute ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
			// Make all falsy values `null` (except for 0, which could be an id.. see '/issues/179')
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
			if ( !id && id !== 0 ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
				id = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
			return id;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
		 *
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
		 * @param type
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
		 * @param {String|Number|Object|Backbone.RelationalModel} item
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
		find: function( type, item ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
			var id = this.resolveIdForItem( type, item );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			var coll = this.getCollection( type );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			// Because the found object could be of any of the type's superModel
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
			// types, only return it if it's actually of the type asked for.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
			if ( coll ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
				var obj = coll.get( id );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
				if ( obj instanceof type ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
					return obj;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
			return null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
		 * Add a 'model' to it's appropriate collection. Retain the original contents of 'model.collection'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
		 * @param {Backbone.RelationalModel} model
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
		register: function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
			var coll = this.getCollection( model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
			if ( coll ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
				if ( coll.get( model ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
					throw new Error( "Cannot instantiate more than one Backbone.RelationalModel with the same id per type!" );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
				var modelColl = model.collection;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
				coll.add( model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
				model.bind( 'destroy', this.unregister, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
				model.collection = modelColl;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
		 * Explicitly update a model's id in it's store collection
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
		 * @param {Backbone.RelationalModel} model
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
		update: function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
			var coll = this.getCollection( model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
			coll._onModelEvent( 'change:' + model.idAttribute, model, coll );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
		 * Remove a 'model' from the store.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
		 * @param {Backbone.RelationalModel} model
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
		unregister: function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
			model.unbind( 'destroy', this.unregister );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
			var coll = this.getCollection( model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
			coll && coll.remove( model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
	Backbone.Relational.store = new Backbone.Store();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
	 * The main Relation class, from which 'HasOne' and 'HasMany' inherit. Internally, 'relational:<key>' events
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
	 * are used to regulate addition and removal of models from relations.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
	 *
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
	 * @param {Backbone.RelationalModel} instance
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	 * @param {Object} options
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
	 * @param {string} options.key
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	 * @param {Backbone.RelationalModel.constructor} options.relatedModel
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
	 * @param {Boolean|String} [options.includeInJSON=true] Serialize the given attribute for related model(s)' in toJSON, or just their ids.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
	 * @param {Boolean} [options.createModels=true] Create objects from the contents of keys if the object is not found in Backbone.store.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
	 * @param {Object} [options.reverseRelation] Specify a bi-directional relation. If provided, Relation will reciprocate
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	 *    the relation to the 'relatedModel'. Required and optional properties match 'options', except that it also needs
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	 *    {Backbone.Relation|String} type ('HasOne' or 'HasMany').
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	Backbone.Relation = function( instance, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
		this.instance = instance;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		// Make sure 'options' is sane, and fill with defaults from subclasses and this object's prototype
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
		options = _.isObject( options ) ? options : {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
		this.reverseRelation = _.defaults( options.reverseRelation || {}, this.options.reverseRelation );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
		this.reverseRelation.type = !_.isString( this.reverseRelation.type ) ? this.reverseRelation.type :
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
			Backbone[ this.reverseRelation.type ] || Backbone.Relational.store.getObjectByName( this.reverseRelation.type );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
		this.model = options.model || this.instance.constructor;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
		this.options = _.defaults( options, this.options, Backbone.Relation.prototype.options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
		this.key = this.options.key;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
		this.keySource = this.options.keySource || this.key;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
		this.keyDestination = this.options.keyDestination || this.keySource || this.key;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
		// 'exports' should be the global object where 'relatedModel' can be found on if given as a string.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
		this.relatedModel = this.options.relatedModel;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
		if ( _.isString( this.relatedModel ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
			this.relatedModel = Backbone.Relational.store.getObjectByName( this.relatedModel );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
		if ( !this.checkPreconditions() ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   411
			return;
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
		if ( instance ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   415
			var contentKey = this.keySource;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   416
			if ( contentKey !== this.key && typeof this.instance.get( this.key ) === 'object' ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   417
				contentKey = this.key;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   418
			}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   419
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   420
			this.keyContents = this.instance.get( contentKey );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
			// Explicitly clear 'keySource', to prevent a leaky abstraction if 'keySource' differs from 'key'.
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   423
			if ( this.keySource !== this.key ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
				this.instance.unset( this.keySource, { silent: true } );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
			// Add this Relation to instance._relations
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
			this.instance._relations.push( this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
		// Add the reverse relation on 'relatedModel' to the store's reverseRelations
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
		if ( !this.options.isAutoRelation && this.reverseRelation.type && this.reverseRelation.key ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
			Backbone.Relational.store.addReverseRelation( _.defaults( {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
					isAutoRelation: true,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
					model: this.relatedModel,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
					relatedModel: this.model,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
					reverseRelation: this.options // current relation is the 'reverseRelation' for it's own reverseRelation
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
				},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
				this.reverseRelation // Take further properties from this.reverseRelation (type, key, etc.)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
			) );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
		_.bindAll( this, '_modelRemovedFromCollection', '_relatedModelAdded', '_relatedModelRemoved' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
		if ( instance ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
			this.initialize();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   448
			if ( options.autoFetch ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   449
				this.instance.fetchRelated( options.key, _.isObject( options.autoFetch ) ? options.autoFetch : {} );
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   450
			}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   451
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
			// When a model in the store is destroyed, check if it is 'this.instance'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
			Backbone.Relational.store.getCollection( this.instance )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
				.bind( 'relational:remove', this._modelRemovedFromCollection );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
			// When 'relatedModel' are created or destroyed, check if it affects this relation.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
			Backbone.Relational.store.getCollection( this.relatedModel )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
				.bind( 'relational:add', this._relatedModelAdded )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
				.bind( 'relational:remove', this._relatedModelRemoved );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
	// Fix inheritance :\
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
	Backbone.Relation.extend = Backbone.Model.extend;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
	// Set up all inheritable **Backbone.Relation** properties and methods.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
	_.extend( Backbone.Relation.prototype, Backbone.Events, Backbone.Semaphore, {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
		options: {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
			createModels: true,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
			includeInJSON: true,
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   469
			isAutoRelation: false,
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   470
			autoFetch: false
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
		instance: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
		key: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
		keyContents: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
		relatedModel: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
		reverseRelation: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
		related: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
		_relatedModelAdded: function( model, coll, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
			// Allow 'model' to set up it's relations, before calling 'tryAddRelated'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
			// (which can result in a call to 'addRelated' on a relation of 'model')
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
			var dit = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
			model.queue( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
				dit.tryAddRelated( model, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
			});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
		_relatedModelRemoved: function( model, coll, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
			this.removeRelated( model, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
		_modelRemovedFromCollection: function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
			if ( model === this.instance ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
				this.destroy();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
		 * Check several pre-conditions.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
		 * @return {Boolean} True if pre-conditions are satisfied, false if they're not.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
		checkPreconditions: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
			var i = this.instance,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
				k = this.key,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
				m = this.model,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
				rm = this.relatedModel,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
				warn = Backbone.Relational.showWarnings && typeof console !== 'undefined';
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
			if ( !m || !k || !rm ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
				warn && console.warn( 'Relation=%o; no model, key or relatedModel (%o, %o, %o)', this, m, k, rm );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
				return false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
			// Check if the type in 'model' inherits from Backbone.RelationalModel
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
			if ( !( m.prototype instanceof Backbone.RelationalModel ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
				warn && console.warn( 'Relation=%o; model does not inherit from Backbone.RelationalModel (%o)', this, i );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
				return false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
			// Check if the type in 'relatedModel' inherits from Backbone.RelationalModel
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
			if ( !( rm.prototype instanceof Backbone.RelationalModel ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
				warn && console.warn( 'Relation=%o; relatedModel does not inherit from Backbone.RelationalModel (%o)', this, rm );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
				return false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
			// Check if this is not a HasMany, and the reverse relation is HasMany as well
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
			if ( this instanceof Backbone.HasMany && this.reverseRelation.type === Backbone.HasMany ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
				warn && console.warn( 'Relation=%o; relation is a HasMany, and the reverseRelation is HasMany as well.', this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
				return false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
			// Check if we're not attempting to create a duplicate relationship
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
			if ( i && i._relations.length ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   532
				var exists = _.any( i._relations || [], function( rel ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
						var hasReverseRelation = this.reverseRelation.key && rel.reverseRelation.key;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
						return rel.relatedModel === rm && rel.key === k &&
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
							( !hasReverseRelation || this.reverseRelation.key === rel.reverseRelation.key );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
					}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
				if ( exists ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
					warn && console.warn( 'Relation=%o between instance=%o.%s and relatedModel=%o.%s already exists',
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
						this, i, k, rm, this.reverseRelation.key );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
					return false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
			return true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
		 * Set the related model(s) for this relation
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   550
		 * @param {Backbone.Model|Backbone.Collection} related
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
		 * @param {Object} [options]
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
		setRelated: function( related, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
			this.related = related;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
			this.instance.acquire();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
			this.instance.set( this.key, related, _.defaults( options || {}, { silent: true } ) );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
			this.instance.release();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
		 * Determine if a relation (on a different RelationalModel) is the reverse
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
		 * relation of the current one.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
		 * @param {Backbone.Relation} relation
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
		 * @return {Boolean}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
		_isReverseRelation: function( relation ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
			if ( relation.instance instanceof this.relatedModel && this.reverseRelation.key === relation.key &&
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
					this.key === relation.reverseRelation.key ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
				return true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
			return false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
		 * Get the reverse relations (pointing back to 'this.key' on 'this.instance') for the currently related model(s).
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
		 * @param {Backbone.RelationalModel} [model] Get the reverse relations for a specific model.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
		 *    If not specified, 'this.related' is used.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
		 * @return {Backbone.Relation[]}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
		getReverseRelations: function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
			var reverseRelations = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
			// Iterate over 'model', 'this.related.models' (if this.related is a Backbone.Collection), or wrap 'this.related' in an array.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
			var models = !_.isUndefined( model ) ? [ model ] : this.related && ( this.related.models || [ this.related ] );
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   585
			_.each( models || [], function( related ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   586
					_.each( related.getRelations() || [], function( relation ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
							if ( this._isReverseRelation( relation ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
								reverseRelations.push( relation );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
							}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
						}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
			return reverseRelations;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
		 * Rename options.silent to options.silentChange, so events propagate properly.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
		 * (for example in HasMany, from 'addRelated'->'handleAddition')
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
		 * @param {Object} [options]
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
		 * @return {Object}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
		sanitizeOptions: function( options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
			options = options ? _.clone( options ) : {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
			if ( options.silent ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
				options.silentChange = true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
				delete options.silent;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
			return options;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
		 * Rename options.silentChange to options.silent, so events are silenced as intended in Backbone's
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
		 * original functions.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
		 * @param {Object} [options]
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
		 * @return {Object}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
		unsanitizeOptions: function( options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
			options = options ? _.clone( options ) : {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
			if ( options.silentChange ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
				options.silent = true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
				delete options.silentChange;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
			return options;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
		// Cleanup. Get reverse relation, call removeRelated on each.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
		destroy: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
			Backbone.Relational.store.getCollection( this.instance )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
				.unbind( 'relational:remove', this._modelRemovedFromCollection );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
			Backbone.Relational.store.getCollection( this.relatedModel )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
				.unbind( 'relational:add', this._relatedModelAdded )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
				.unbind( 'relational:remove', this._relatedModelRemoved );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
			
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   635
			_.each( this.getReverseRelations() || [], function( relation ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
					relation.removeRelated( this.instance );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
	Backbone.HasOne = Backbone.Relation.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
		options: {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
			reverseRelation: { type: 'HasMany' }
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
		initialize: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
			_.bindAll( this, 'onChange' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
			this.instance.bind( 'relational:change:' + this.key, this.onChange );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
			var model = this.findRelated( { silent: true } );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
			this.setRelated( model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
			// Notify new 'related' object of the new relation.
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   655
			_.each( this.getReverseRelations() || [], function( relation ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
					relation.addRelated( this.instance );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
		findRelated: function( options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
			var item = this.keyContents;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
			var model = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
			if ( item instanceof this.relatedModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
				model = item;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
			else if ( item || item === 0 ) { // since 0 can be a valid `id` as well
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
				model = this.relatedModel.findOrCreate( item, { create: this.options.createModels } );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
			return model;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
		 * If the key is changed, notify old & new reverse relations and initialize the new relation
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
		onChange: function( model, attr, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
			// Don't accept recursive calls to onChange (like onChange->findRelated->findOrCreate->initializeRelations->addRelated->onChange)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
			if ( this.isLocked() ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
				return;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
			this.acquire();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
			options = this.sanitizeOptions( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
			// 'options._related' is set by 'addRelated'/'removeRelated'. If it is set, the change
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
			// is the result of a call from a relation. If it's not, the change is the result of 
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
			// a 'set' call on this.instance.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
			var changed = _.isUndefined( options._related );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
			var oldRelated = changed ? this.related : options._related;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
			if ( changed ) {	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
				this.keyContents = attr;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
				// Set new 'related'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
				if ( attr instanceof this.relatedModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
					this.related = attr;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
				else if ( attr ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
					var related = this.findRelated( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
					this.setRelated( related );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
				else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
					this.setRelated( null );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
			// Notify old 'related' object of the terminated relation
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
			if ( oldRelated && this.related !== oldRelated ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   709
				_.each( this.getReverseRelations( oldRelated ) || [], function( relation ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
						relation.removeRelated( this.instance, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
					}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
			// Notify new 'related' object of the new relation. Note we do re-apply even if this.related is oldRelated;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
			// that can be necessary for bi-directional relations if 'this.instance' was created after 'this.related'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
			// In that case, 'this.instance' will already know 'this.related', but the reverse might not exist yet.
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   717
			_.each( this.getReverseRelations() || [], function( relation ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
					relation.addRelated( this.instance, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
				}, this);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
			// Fire the 'update:<key>' event if 'related' was updated
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
			if ( !options.silentChange && this.related !== oldRelated ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
				var dit = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
				Backbone.Relational.eventQueue.add( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
					dit.instance.trigger( 'update:' + dit.key, dit.instance, dit.related, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
			this.release();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
		 * If a new 'this.relatedModel' appears in the 'store', try to match it to the last set 'keyContents'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
		tryAddRelated: function( model, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
			if ( this.related ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
				return;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
			options = this.sanitizeOptions( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
			var item = this.keyContents;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
			if ( item || item === 0 ) { // since 0 can be a valid `id` as well
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
				var id = Backbone.Relational.store.resolveIdForItem( this.relatedModel, item );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
				if ( !_.isNull( id ) && model.id === id ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
					this.addRelated( model, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
		addRelated: function( model, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
			if ( model !== this.related ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
				var oldRelated = this.related || null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
				this.setRelated( model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
				this.onChange( this.instance, model, { _related: oldRelated } );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
		removeRelated: function( model, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
			if ( !this.related ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
				return;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
			if ( model === this.related ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
				var oldRelated = this.related || null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
				this.setRelated( null );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
				this.onChange( this.instance, model, { _related: oldRelated } );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
	Backbone.HasMany = Backbone.Relation.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
		collectionType: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
		options: {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
			reverseRelation: { type: 'HasOne' },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
			collectionType: Backbone.Collection,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
			collectionKey: true,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
			collectionOptions: {}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
		initialize: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
			_.bindAll( this, 'onChange', 'handleAddition', 'handleRemoval', 'handleReset' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
			this.instance.bind( 'relational:change:' + this.key, this.onChange );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
			// Handle a custom 'collectionType'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
			this.collectionType = this.options.collectionType;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
			if ( _.isString( this.collectionType ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
				this.collectionType = Backbone.Relational.store.getObjectByName( this.collectionType );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
			if ( !this.collectionType.prototype instanceof Backbone.Collection ){
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
				throw new Error( 'collectionType must inherit from Backbone.Collection' );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
			// Handle cases where a model/relation is created with a collection passed straight into 'attributes'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
			if ( this.keyContents instanceof Backbone.Collection ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
				this.setRelated( this._prepareCollection( this.keyContents ) );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
			else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
				this.setRelated( this._prepareCollection() );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
			this.findRelated( { silent: true } );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
		_getCollectionOptions: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
			return _.isFunction( this.options.collectionOptions ) ?
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
				this.options.collectionOptions( this.instance ) :
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
				this.options.collectionOptions;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
		 * Bind events and setup collectionKeys for a collection that is to be used as the backing store for a HasMany.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
		 * If no 'collection' is supplied, a new collection will be created of the specified 'collectionType' option.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
		 * @param {Backbone.Collection} [collection]
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
		_prepareCollection: function( collection ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
			if ( this.related ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
				this.related
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
					.unbind( 'relational:add', this.handleAddition )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
					.unbind( 'relational:remove', this.handleRemoval )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
					.unbind( 'relational:reset', this.handleReset )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
			if ( !collection || !( collection instanceof Backbone.Collection ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
				collection = new this.collectionType( [], this._getCollectionOptions() );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
			collection.model = this.relatedModel;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
			if ( this.options.collectionKey ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
				var key = this.options.collectionKey === true ? this.options.reverseRelation.key : this.options.collectionKey;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
				if ( collection[ key ] && collection[ key ] !== this.instance ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
					if ( Backbone.Relational.showWarnings && typeof console !== 'undefined' ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
						console.warn( 'Relation=%o; collectionKey=%s already exists on collection=%o', this, key, this.options.collectionKey );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
				else if ( key ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
					collection[ key ] = this.instance;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
			collection
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
				.bind( 'relational:add', this.handleAddition )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
				.bind( 'relational:remove', this.handleRemoval )
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
				.bind( 'relational:reset', this.handleReset );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
			return collection;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
		findRelated: function( options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
			if ( this.keyContents ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
				var models = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
				if ( this.keyContents instanceof Backbone.Collection ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
					models = this.keyContents.models;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
				else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
					// Handle cases the an API/user supplies just an Object/id instead of an Array
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
					this.keyContents = _.isArray( this.keyContents ) ? this.keyContents : [ this.keyContents ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
					// Try to find instances of the appropriate 'relatedModel' in the store
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   862
					_.each( this.keyContents || [], function( item ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
							var model = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
							if ( item instanceof this.relatedModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
								model = item;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
							}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
							else if ( item || item === 0 ) { // since 0 can be a valid `id` as well
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
								model = this.relatedModel.findOrCreate( item, { create: this.options.createModels } );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
							}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   871
							if ( model && !this.related.get( model ) ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
								models.push( model );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
							}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
						}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
				// Add all found 'models' in on go, so 'add' will only be called once (and thus 'sort', etc.)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
				if ( models.length ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
					options = this.unsanitizeOptions( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
					this.related.add( models, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
		 * If the key is changed, notify old & new reverse relations and initialize the new relation
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
		onChange: function( model, attr, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
			options = this.sanitizeOptions( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
			this.keyContents = attr;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
			// Replace 'this.related' by 'attr' if it is a Backbone.Collection
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
			if ( attr instanceof Backbone.Collection ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
				this._prepareCollection( attr );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
				this.related = attr;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
			// Otherwise, 'attr' should be an array of related object ids.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
			// Re-use the current 'this.related' if it is a Backbone.Collection, and remove any current entries.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
			// Otherwise, create a new collection.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
			else {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   901
				var oldIds = {}, newIds = {};
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   902
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   903
				if (!_.isArray( attr ) && attr !== undefined) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   904
					attr = [ attr ];
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   905
				}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   906
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   907
				_.each( attr, function( attributes ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   908
					newIds[ attributes.id ] = true;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   909
				});
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   911
				var coll = this.related;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   912
				if ( coll instanceof Backbone.Collection ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   913
					// Make sure to operate on a copy since we're removing while iterating
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   914
					_.each( coll.models.slice(0) , function( model ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   915
						// When fetch is called with the 'keepNewModels' option, we don't want to remove
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   916
						// client-created new models when the fetch is completed.
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   917
						if ( !options.keepNewModels || !model.isNew() ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   918
							oldIds[ model.id ] = true;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   919
							coll.remove( model, { silent: (model.id in newIds) } );
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   920
						}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   921
					});
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   922
				} else {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
					coll = this._prepareCollection();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   926
				_.each( attr, function( attributes ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   927
					var model = this.relatedModel.findOrCreate( attributes, { create: this.options.createModels } );
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   928
					if (model) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   929
						coll.add( model, { silent: (attributes.id in oldIds)} );
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   930
					}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   931
				}, this);
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   932
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
				this.setRelated( coll );
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   934
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
			var dit = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
			Backbone.Relational.eventQueue.add( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
				!options.silentChange && dit.instance.trigger( 'update:' + dit.key, dit.instance, dit.related, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
			});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
		tryAddRelated: function( model, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
			options = this.sanitizeOptions( options );
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   945
			if ( !this.related.get( model ) ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
				// Check if this new model was specified in 'this.keyContents'
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   947
				var item = _.any( this.keyContents || [], function( item ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
						var id = Backbone.Relational.store.resolveIdForItem( this.relatedModel, item );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
						return !_.isNull( id ) && id === model.id;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
					}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
				if ( item ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
					this.related.add( model, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
		 * When a model is added to a 'HasMany', trigger 'add' on 'this.instance' and notify reverse relations.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
		 * (should be 'HasOne', must set 'this.instance' as their related).
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
		handleAddition: function( model, coll, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
			//console.debug('handleAddition called; args=%o', arguments);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
			// Make sure the model is in fact a valid model before continuing.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
			// (it can be invalid as a result of failing validation in Backbone.Collection._prepareModel)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
			if ( !( model instanceof Backbone.Model ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
				return;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
			options = this.sanitizeOptions( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
			
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   972
			_.each( this.getReverseRelations( model ) || [], function( relation ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
					relation.addRelated( this.instance, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
			// Only trigger 'add' once the newly added model is initialized (so, has it's relations set up)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
			var dit = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
			Backbone.Relational.eventQueue.add( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
				!options.silentChange && dit.instance.trigger( 'add:' + dit.key, model, dit.related, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
			});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
		 * When a model is removed from a 'HasMany', trigger 'remove' on 'this.instance' and notify reverse relations.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
		 * (should be 'HasOne', which should be nullified)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
		handleRemoval: function( model, coll, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
			//console.debug('handleRemoval called; args=%o', arguments);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
			if ( !( model instanceof Backbone.Model ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
				return;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
			options = this.sanitizeOptions( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
			
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   995
			_.each( this.getReverseRelations( model ) || [], function( relation ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
					relation.removeRelated( this.instance, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
			var dit = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
			Backbone.Relational.eventQueue.add( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
				!options.silentChange && dit.instance.trigger( 'remove:' + dit.key, model, dit.related, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
			});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
		handleReset: function( coll, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
			options = this.sanitizeOptions( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
			var dit = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
			Backbone.Relational.eventQueue.add( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
				!options.silentChange && dit.instance.trigger( 'reset:' + dit.key, dit.related, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
			});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
		addRelated: function( model, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
			var dit = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
			options = this.unsanitizeOptions( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
			model.queue( function() { // Queued to avoid errors for adding 'model' to the 'this.related' set twice
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1018
				if ( dit.related && !dit.related.get( model ) ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
					dit.related.add( model, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
			});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
		removeRelated: function( model, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
			options = this.unsanitizeOptions( options );
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1026
			if ( this.related.get( model ) ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
				this.related.remove( model, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
	 * A type of Backbone.Model that also maintains relations to other models and collections.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
	 * New events when compared to the original:
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
	 *  - 'add:<key>' (model, related collection, options)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
	 *  - 'remove:<key>' (model, related collection, options)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
	 *  - 'update:<key>' (model, related model or collection, options)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
	Backbone.RelationalModel = Backbone.Model.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
		relations: null, // Relation descriptions on the prototype
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
		_relations: null, // Relation instances
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
		_isInitialized: false,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
		_deferProcessing: false,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
		_queue: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
		subModelTypeAttribute: 'type',
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
		subModelTypes: null,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
		constructor: function( attributes, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
			// Nasty hack, for cases like 'model.get( <HasMany key> ).add( item )'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
			// Defer 'processQueue', so that when 'Relation.createModels' is used we:
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
			// a) Survive 'Backbone.Collection.add'; this takes care we won't error on "can't add model to a set twice"
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
			//    (by creating a model from properties, having the model add itself to the collection via one of
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
			//    it's relations, then trying to add it to the collection).
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
			// b) Trigger 'HasMany' collection events only after the model is really fully set up.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
			// Example that triggers both a and b: "p.get('jobs').add( { company: c, person: p } )".
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
			var dit = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
			if ( options && options.collection ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
				this._deferProcessing = true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
				var processQueue = function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
					if ( model === dit ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
						dit._deferProcessing = false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
						dit.processQueue();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
						options.collection.unbind( 'relational:add', processQueue );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
				};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
				options.collection.bind( 'relational:add', processQueue );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
				// So we do process the queue eventually, regardless of whether this model really gets added to 'options.collection'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
				_.defer( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
					processQueue( dit );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
			this._queue = new Backbone.BlockingQueue();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
			this._queue.block();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
			Backbone.Relational.eventQueue.block();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
			Backbone.Model.apply( this, arguments );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
			// Try to run the global queue holding external events
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
			Backbone.Relational.eventQueue.unblock();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
		 * Override 'trigger' to queue 'change' and 'change:*' events
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
		trigger: function( eventName ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
			if ( eventName.length > 5 && 'change' === eventName.substr( 0, 6 ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
				var dit = this, args = arguments;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
				Backbone.Relational.eventQueue.add( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
						Backbone.Model.prototype.trigger.apply( dit, args );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
					});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
			else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
				Backbone.Model.prototype.trigger.apply( this, arguments );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
			return this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
		 * Initialize Relations present in this.relations; determine the type (HasOne/HasMany), then creates a new instance.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
		 * Invoked in the first call so 'set' (which is made from the Backbone.Model constructor).
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
		initializeRelations: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
			this.acquire(); // Setting up relations often also involve calls to 'set', and we only want to enter this function once
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
			this._relations = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
			
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1111
			_.each( this.relations || [], function( rel ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
					var type = !_.isString( rel.type ) ? rel.type :	Backbone[ rel.type ] || Backbone.Relational.store.getObjectByName( rel.type );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
					if ( type && type.prototype instanceof Backbone.Relation ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
						new type( this, rel ); // Also pushes the new Relation into _relations
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
					else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
						Backbone.Relational.showWarnings && typeof console !== 'undefined' && console.warn( 'Relation=%o; missing or invalid type!', rel );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
			this._isInitialized = true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
			this.release();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
			this.processQueue();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
		 * When new values are set, notify this model's relations (also if options.silent is set).
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
		 * (Relation.setRelated locks this model before calling 'set' on it to prevent loops)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
		updateRelations: function( options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
			if ( this._isInitialized && !this.isLocked() ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1132
				_.each( this._relations || [], function( rel ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
					// Update from data in `rel.keySource` if set, or `rel.key` otherwise
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
					var val = this.attributes[ rel.keySource ] || this.attributes[ rel.key ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
					if ( rel.related !== val ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
						this.trigger( 'relational:change:' + rel.key, this, val, options || {} );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
		 * Either add to the queue (if we're not initialized yet), or execute right away.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		queue: function( func ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
			this._queue.add( func );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
		 * Process _queue
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
		processQueue: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
			if ( this._isInitialized && !this._deferProcessing && this._queue.isBlocked() ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
				this._queue.unblock();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
		 * Get a specific relation.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
		 * @param key {string} The relation key to look for.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
		 * @return {Backbone.Relation} An instance of 'Backbone.Relation', if a relation was found for 'key', or null.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
		getRelation: function( key ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
			return _.detect( this._relations, function( rel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
				if ( rel.key === key ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
					return true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
			}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
		 * Get all of the created relations.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
		 * @return {Backbone.Relation[]}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
		getRelations: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
			return this._relations;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
		 * Retrieve related objects.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
		 * @param key {string} The relation key to fetch models for.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
		 * @param [options] {Object} Options for 'Backbone.Model.fetch' and 'Backbone.sync'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
		 * @param [update=false] {boolean} Whether to force a fetch from the server (updating existing models).
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
		 * @return {jQuery.when[]} An array of request objects
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
		fetchRelated: function( key, options, update ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
			options || ( options = {} );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
			var setUrl,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
				requests = [],
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
				rel = this.getRelation( key ),
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
				keyContents = rel && rel.keyContents,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
				toFetch = keyContents && _.select( _.isArray( keyContents ) ? keyContents : [ keyContents ], function( item ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
					var id = Backbone.Relational.store.resolveIdForItem( rel.relatedModel, item );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
					return !_.isNull( id ) && ( update || !Backbone.Relational.store.find( rel.relatedModel, id ) );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
			if ( toFetch && toFetch.length ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
				// Create a model for each entry in 'keyContents' that is to be fetched
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
				var models = _.map( toFetch, function( item ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
					var model;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
					if ( _.isObject( item ) ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1203
						model = rel.relatedModel.findOrCreate( item );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
					else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
						var attrs = {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
						attrs[ rel.relatedModel.prototype.idAttribute ] = item;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1208
						model = rel.relatedModel.findOrCreate( attrs );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
					return model;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
				// Try if the 'collection' can provide a url to fetch a set of models in one request.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
				if ( rel.related instanceof Backbone.Collection && _.isFunction( rel.related.url ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
					setUrl = rel.related.url( models );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
				// An assumption is that when 'Backbone.Collection.url' is a function, it can handle building of set urls.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
				// To make sure it can, test if the url we got by supplying a list of models to fetch is different from
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
				// the one supplied for the default fetch action (without args to 'url').
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
				if ( setUrl && setUrl !== rel.related.url() ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
					var opts = _.defaults(
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
						{
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
							error: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
								var args = arguments;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1227
								_.each( models || [], function( model ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
										model.trigger( 'destroy', model, model.collection, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
										options.error && options.error.apply( model, args );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
									});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
							},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
							url: setUrl
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
						},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
						options,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
						{ add: true }
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
					);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1238
					requests = [ rel.related.fetch( opts ) ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
				else {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1241
					requests = _.map( models || [], function( model ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
						var opts = _.defaults(
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
							{
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
								error: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
									model.trigger( 'destroy', model, model.collection, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
									options.error && options.error.apply( model, arguments );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
								}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
							},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
							options
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
						);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
						return model.fetch( opts );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
					}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
			return requests;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
		set: function( key, value, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
			Backbone.Relational.eventQueue.block();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1262
			// Duplicate backbone's behavior to allow separate key/value parameters, instead of a single 'attributes' object
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1263
			var attributes;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1264
			if ( _.isObject( key ) || key == null ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1265
				attributes = key;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1266
				options = value;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
			else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
				attributes = {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
				attributes[ key ] = value;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
			var result = Backbone.Model.prototype.set.apply( this, arguments );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
			// Ideal place to set up relations :)
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
			if ( !this._isInitialized && !this.isLocked() ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
				this.constructor.initializeModelHierarchy();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
				Backbone.Relational.store.register( this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
				this.initializeRelations();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
			// Update the 'idAttribute' in Backbone.store if; we don't want it to miss an 'id' update due to {silent:true}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
			else if ( attributes && this.idAttribute in attributes ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
				Backbone.Relational.store.update( this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
			if ( attributes ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1289
				this.updateRelations( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
			// Try to run the global queue holding external events
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
			Backbone.Relational.eventQueue.unblock();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
			return result;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1298
		unset: function( attribute, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1299
			Backbone.Relational.eventQueue.block();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
			var result = Backbone.Model.prototype.unset.apply( this, arguments );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
			this.updateRelations( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
			// Try to run the global queue holding external events
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
			Backbone.Relational.eventQueue.unblock();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
			return result;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
		clear: function( options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
			Backbone.Relational.eventQueue.block();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
			var result = Backbone.Model.prototype.clear.apply( this, arguments );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1314
			this.updateRelations( options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
			// Try to run the global queue holding external events
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
			Backbone.Relational.eventQueue.unblock();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
			return result;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
		 * Override 'change', so the change will only execute after 'set' has finised (relations are updated),
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
		 * and 'previousAttributes' will be available when the event is fired.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
		change: function( options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
			var dit = this, args = arguments;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
			Backbone.Relational.eventQueue.add( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
					Backbone.Model.prototype.change.apply( dit, args );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
		clone: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
			var attributes = _.clone( this.attributes );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
			if ( !_.isUndefined( attributes[ this.idAttribute ] ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
				attributes[ this.idAttribute ] = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1339
			_.each( this.getRelations() || [], function( rel ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
					delete attributes[ rel.key ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
			return new this.constructor( attributes );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1345
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1346
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1347
		 * Convert relations to JSON, omits them when required
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1348
		 */
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1349
		toJSON: function(options) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1350
			// If this Model has already been fully serialized in this branch once, return to avoid loops
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1351
			if ( this.isLocked() ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
				return this.id;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
			this.acquire();
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1356
			var json = Backbone.Model.prototype.toJSON.call( this, options );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
			if ( this.constructor._superModel && !( this.constructor._subModelTypeAttribute in json ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
				json[ this.constructor._subModelTypeAttribute ] = this.constructor._subModelTypeValue;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
			
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1362
			_.each( this._relations || [], function( rel ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
					var value = json[ rel.key ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
					if ( rel.options.includeInJSON === true) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
						if ( value && _.isFunction( value.toJSON ) ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1367
							json[ rel.keyDestination ] = value.toJSON( options );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
						else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
							json[ rel.keyDestination ] = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
					else if ( _.isString( rel.options.includeInJSON ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
						if ( value instanceof Backbone.Collection ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
							json[ rel.keyDestination ] = value.pluck( rel.options.includeInJSON );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
						else if ( value instanceof Backbone.Model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
							json[ rel.keyDestination ] = value.get( rel.options.includeInJSON );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
						}	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
						else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
							json[ rel.keyDestination ] = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
					else if ( _.isArray( rel.options.includeInJSON ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
						if ( value instanceof Backbone.Collection ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
							var valueSub = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
							value.each( function( model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
								var curJson = {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
								_.each( rel.options.includeInJSON, function( key ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
									curJson[ key ] = model.get( key );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
								});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
								valueSub.push( curJson );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
							});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
							json[ rel.keyDestination ] = valueSub;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
						else if ( value instanceof Backbone.Model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
							var valueSub = {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
							_.each( rel.options.includeInJSON, function( key ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
								valueSub[ key ] = value.get( key );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
							});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
							json[ rel.keyDestination ] = valueSub;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
						else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
							json[ rel.keyDestination ] = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
					else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
						delete json[ rel.key ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
					if ( rel.keyDestination !== rel.key ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
						delete json[ rel.key ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
			this.release();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
			return json;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
	},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
	{
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
		setup: function( superModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
			// We don't want to share a relations array with a parent, as this will cause problems with
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
			// reverse relations.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
			this.prototype.relations = ( this.prototype.relations || [] ).slice( 0 );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
			this._subModels = {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
			this._superModel = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
			// If this model has 'subModelTypes' itself, remember them in the store
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
			if ( this.prototype.hasOwnProperty( 'subModelTypes' ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
				Backbone.Relational.store.addSubModels( this.prototype.subModelTypes, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
			// The 'subModelTypes' property should not be inherited, so reset it.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
			else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
				this.prototype.subModelTypes = null;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
			// Initialize all reverseRelations that belong to this new model.
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1439
			_.each( this.prototype.relations || [], function( rel ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
					if ( !rel.model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
						rel.model = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
					if ( rel.reverseRelation && rel.model === this ) {				
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
						var preInitialize = true;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
						if ( _.isString( rel.relatedModel ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
							/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
							 * The related model might not be defined for two reasons
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
							 *  1. it never gets defined, e.g. a typo
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
							 *  2. it is related to itself
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
							 * In neither of these cases do we need to pre-initialize reverse relations.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
							 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
							var relatedModel = Backbone.Relational.store.getObjectByName( rel.relatedModel );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
							preInitialize = relatedModel && ( relatedModel.prototype instanceof Backbone.RelationalModel );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
						var type = !_.isString( rel.type ) ? rel.type : Backbone[ rel.type ] || Backbone.Relational.store.getObjectByName( rel.type );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
						if ( preInitialize && type && type.prototype instanceof Backbone.Relation ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
							new type( null, rel );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
				}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1464
			return this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1467
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1468
		 * Create a 'Backbone.Model' instance based on 'attributes'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
		 * @param {Object} attributes
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
		 * @param {Object} [options]
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1471
		 * @return {Backbone.Model}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
		build: function( attributes, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
			var model = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
			// 'build' is a possible entrypoint; it's possible no model hierarchy has been determined yet.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
			this.initializeModelHierarchy();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
			// Determine what type of (sub)model should be built if applicable.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
			// Lookup the proper subModelType in 'this._subModels'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
			if ( this._subModels && this.prototype.subModelTypeAttribute in attributes ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
				var subModelTypeAttribute = attributes[ this.prototype.subModelTypeAttribute ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
				var subModelType = this._subModels[ subModelTypeAttribute ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1484
				if ( subModelType ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1485
					model = subModelType;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1486
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1487
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
			return new model( attributes, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
		initializeModelHierarchy: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
			// If we're here for the first time, try to determine if this modelType has a 'superModel'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
			if ( _.isUndefined( this._superModel ) || _.isNull( this._superModel ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
				Backbone.Relational.store.setupSuperModel( this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
				// If a superModel has been found, copy relations from the _superModel if they haven't been
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
				// inherited automatically (due to a redefinition of 'relations').
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
				// Otherwise, make sure we don't get here again for this type by making '_superModel' false so we fail
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
				// the isUndefined/isNull check next time.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
				if ( this._superModel ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
					//
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
					if ( this._superModel.prototype.relations ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1504
						var supermodelRelationsExist = _.any( this.prototype.relations || [], function( rel ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
							return rel.model && rel.model !== this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
						}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
						if ( !supermodelRelationsExist ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
							this.prototype.relations = this._superModel.prototype.relations.concat( this.prototype.relations );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
						}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
					}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1512
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
				else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
					this._superModel = false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
			// If we came here through 'build' for a model that has 'subModelTypes', and not all of them have been resolved yet, try to resolve each.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
			if ( this.prototype.subModelTypes && _.keys( this.prototype.subModelTypes ).length !== _.keys( this._subModels ).length ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1520
				_.each( this.prototype.subModelTypes || [], function( subModelTypeName ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
					var subModelType = Backbone.Relational.store.getObjectByName( subModelTypeName );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
					subModelType && subModelType.initializeModelHierarchy();
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1526
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1527
		/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
		 * Find an instance of `this` type in 'Backbone.Relational.store'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1529
		 * - If `attributes` is a string or a number, `findOrCreate` will just query the `store` and return a model if found.
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
  1530
		 * - If `attributes` is an object and is found in the store, the model will be updated with `attributes` unless `options.update` is `false`. 
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1531
		 *   Otherwise, a new model is created with `attributes` (unless `options.create` is explicitly set to `false`).
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
		 * @param {Object|String|Number} attributes Either a model's id, or the attributes used to create or update a model.
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
  1533
		 * @param {Object} [options.create=true, options.update=true]
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1534
		 * @return {Backbone.RelationalModel}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1535
		 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1536
		findOrCreate: function( attributes, options ) {
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
  1537
			options || ( options = {} );
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1538
			var parsedAttributes = (_.isObject( attributes ) && this.prototype.parse) ? this.prototype.parse( attributes ) : attributes;
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
			// Try to find an instance of 'this' model type in the store
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1540
			var model = Backbone.Relational.store.find( this, parsedAttributes );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
  1542
			// If we found an instance, update it with the data in 'item' (unless 'options.update' is false).
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
  1543
			// If not, create an instance (unless 'options.create' is false).
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
			if ( _.isObject( attributes ) ) {
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
  1545
				if ( model && options.update !== false ) {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1546
					model.set( parsedAttributes, options );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
				}
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
  1548
				else if ( !model && options.create !== false ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
					model = this.build( attributes, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
			return model;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
	_.extend( Backbone.RelationalModel.prototype, Backbone.Semaphore );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
	 * Override Backbone.Collection._prepareModel, so objects will be built using the correct type
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
	 * if the collection.model has subModels.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
	Backbone.Collection.prototype.__prepareModel = Backbone.Collection.prototype._prepareModel;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1563
	Backbone.Collection.prototype._prepareModel = function ( attrs, options ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1564
		var model;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1565
		
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1566
		if ( attrs instanceof Backbone.Model ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1567
			if ( !attrs.collection ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1568
				attrs.collection = this;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1569
			}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1570
			model = attrs;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1571
		}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1572
		else {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1573
			options || (options = {});
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
			options.collection = this;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1575
			
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
			if ( typeof this.model.findOrCreate !== 'undefined' ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
				model = this.model.findOrCreate( attrs, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
			else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
				model = new this.model( attrs, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
			
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1583
			if ( !model._validate( attrs, options ) ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
				model = false;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
		return model;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1589
	};
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1590
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1591
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1592
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
	 * Override Backbone.Collection.add, so objects fetched from the server multiple times will
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
	 * update the existing Model. Also, trigger 'relational:add'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1596
	var add = Backbone.Collection.prototype.__add = Backbone.Collection.prototype.add;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1597
	Backbone.Collection.prototype.add = function( models, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1598
		options || (options = {});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
		if ( !_.isArray( models ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1600
			models = [ models ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1601
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1602
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1603
		var modelsToAdd = [];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1604
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1605
		//console.debug( 'calling add on coll=%o; model=%o, options=%o', this, models, options );
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1606
		_.each( models || [], function( model ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1607
			if ( !( model instanceof Backbone.Model ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1608
				// `_prepareModel` attempts to find `model` in Backbone.store through `findOrCreate`,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1609
				// and sets the new properties on it if is found. Otherwise, a new model is instantiated.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1610
				model = Backbone.Collection.prototype._prepareModel.call( this, model, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1611
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1612
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1613
				if ( model instanceof Backbone.Model && !this.get( model ) ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1614
					modelsToAdd.push( model );
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1615
				}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1616
			}, this );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1617
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1618
		// Add 'models' in a single batch, so the original add will only be called once (and thus 'sort', etc).
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1619
		if ( modelsToAdd.length ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1620
			add.call( this, modelsToAdd, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1621
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1622
			_.each( modelsToAdd || [], function( model ) {
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
				this.trigger( 'relational:add', model, this, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
			}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1626
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1627
		return this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1628
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1630
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1631
	 * Override 'Backbone.Collection.remove' to trigger 'relational:remove'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1633
	var remove = Backbone.Collection.prototype.__remove = Backbone.Collection.prototype.remove;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1634
	Backbone.Collection.prototype.remove = function( models, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1635
		options || (options = {});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1636
		if ( !_.isArray( models ) ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1637
			models = [ models ];
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1638
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1639
		else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1640
			models = models.slice( 0 );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1641
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1642
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1643
		//console.debug('calling remove on coll=%o; models=%o, options=%o', this, models, options );
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1644
		_.each( models || [], function( model ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1645
				model = this.get( model );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1647
				if ( model instanceof Backbone.Model ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
					remove.call( this, model, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1649
					this.trigger('relational:remove', model, this, options);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1650
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1651
			}, this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1652
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1653
		return this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1654
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1655
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1656
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1657
	 * Override 'Backbone.Collection.reset' to trigger 'relational:reset'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1658
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1659
	var reset = Backbone.Collection.prototype.__reset = Backbone.Collection.prototype.reset;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1660
	Backbone.Collection.prototype.reset = function( models, options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1661
		reset.call( this, models, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1662
		this.trigger( 'relational:reset', this, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1663
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1664
		return this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1665
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1666
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1667
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1668
	 * Override 'Backbone.Collection.sort' to trigger 'relational:reset'.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1669
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
	var sort = Backbone.Collection.prototype.__sort = Backbone.Collection.prototype.sort;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1671
	Backbone.Collection.prototype.sort = function( options ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1672
		sort.call( this, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1673
		this.trigger( 'relational:reset', this, options );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1674
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1675
		return this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1676
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1677
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1678
	/**
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1679
	 * Override 'Backbone.Collection.trigger' so 'add', 'remove' and 'reset' events are queued until relations
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1680
	 * are ready.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1681
	 */
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
	var trigger = Backbone.Collection.prototype.__trigger = Backbone.Collection.prototype.trigger;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
	Backbone.Collection.prototype.trigger = function( eventName ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1684
		if ( eventName === 'add' || eventName === 'remove' || eventName === 'reset' ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
			var dit = this, args = arguments;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
			if (eventName === 'add') {
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1688
				args = _.toArray( args );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
				// the fourth argument in case of a regular add is the option object.
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
				// we need to clone it, as it could be modified while we wait on the eventQueue to be unblocked
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1691
				if (_.isObject( args[3] ) ) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
  1692
					args[3] = _.clone( args[3] );
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1693
				}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1694
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1696
			Backbone.Relational.eventQueue.add( function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1697
					trigger.apply( dit, args );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1698
				});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1699
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
		else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1701
			trigger.apply( this, arguments );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1702
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
		return this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
	// Override .extend() to automatically call .setup()
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1708
	Backbone.RelationalModel.extend = function( protoProps, classProps ) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
		var child = Backbone.Model.extend.apply( this, arguments );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1710
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
		child.setup( this );
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1712
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1713
		return child;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1715
})();