-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathreverse-relations.js
479 lines (380 loc) · 15 KB
/
reverse-relations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
QUnit.module( "Reverse relations", { setup: require('./setup/data') } );
QUnit.test( "Add and remove", function() {
equal( ourHouse.get( 'occupants' ).length, 1, "ourHouse has 1 occupant" );
equal( person1.get( 'livesIn' ), null, "Person 1 doesn't live anywhere" );
ourHouse.get( 'occupants' ).add( person1 );
equal( ourHouse.get( 'occupants' ).length, 2, "Our House has 2 occupants" );
equal( person1.get( 'livesIn' ) && person1.get('livesIn').id, ourHouse.id, "Person 1 lives in ourHouse" );
person1.set( { 'livesIn': theirHouse } );
equal( theirHouse.get( 'occupants' ).length, 1, "theirHouse has 1 occupant" );
equal( ourHouse.get( 'occupants' ).length, 1, "ourHouse has 1 occupant" );
equal( person1.get( 'livesIn' ) && person1.get('livesIn').id, theirHouse.id, "Person 1 lives in theirHouse" );
});
QUnit.test( "Destroy removes models from reverse relations", function() {
var zoo = new Zoo( { id:1, animals: [ 2, 3, 4 ] } );
var rhino = new Animal( { id: 2, species: 'rhino' } );
var baboon = new Animal( { id: 3, species: 'baboon' } );
var hippo = new Animal( { id: 4, species: 'hippo' } );
ok( zoo.get( 'animals' ).length === 3 );
rhino.destroy();
ok( zoo.get( 'animals' ).length === 2 );
ok( zoo.get( 'animals' ).get( baboon ) === baboon );
ok( !rhino.get( 'zoo' ) );
zoo.get( 'animals' ).remove( hippo );
ok( zoo.get( 'animals' ).length === 1 );
ok( !hippo.get( 'zoo' ) );
zoo.destroy();
ok( zoo.get( 'animals' ).length === 0 );
ok( !baboon.get( 'zoo' ) );
});
QUnit.test( "HasOne relations to self (tree stucture)", function() {
var child1 = new Node({ id: '2', parent: '1', name: 'First child' });
var parent = new Node({ id: '1', name: 'Parent' });
var child2 = new Node({ id: '3', parent: '1', name: 'Second child' });
equal( parent.get( 'children' ).length, 2 );
ok( parent.get( 'children' ).include( child1 ) );
ok( parent.get( 'children' ).include( child2 ) );
ok( child1.get( 'parent' ) === parent );
equal( child1.get( 'children' ).length, 0 );
ok( child2.get( 'parent' ) === parent );
equal( child2.get( 'children' ).length, 0 );
});
QUnit.test( "Models referencing each other in the same relation", function() {
var parent = new Node({ id: 1 });
var child = new Node({ id: 2 });
child.set( 'parent', parent );
parent.save( { 'parent': child } );
ok( parent.get( 'parent' ) === child );
ok( child.get( 'parent' ) === parent );
});
QUnit.test( "HasMany relations to self (tree structure)", function() {
var child1 = new Node({ id: '2', name: 'First child' });
var parent = new Node({ id: '1', children: [ '2', '3' ], name: 'Parent' });
var child2 = new Node({ id: '3', name: 'Second child' });
equal( parent.get( 'children' ).length, 2 );
ok( parent.get( 'children' ).include( child1 ) );
ok( parent.get( 'children' ).include( child2 ) );
ok( child1.get( 'parent' ) === parent );
equal( child1.get( 'children' ).length, 0 );
ok( child2.get( 'parent' ) === parent );
equal( child2.get( 'children' ).length, 0 );
});
QUnit.test( "HasOne relations to self (cycle, directed graph structure)", function() {
var node1 = new Node({ id: '1', parent: '3', name: 'First node' });
var node2 = new Node({ id: '2', parent: '1', name: 'Second node' });
var node3 = new Node({ id: '3', parent: '2', name: 'Third node' });
ok( node1.get( 'parent' ) === node3 );
equal( node1.get( 'children' ).length, 1 );
ok( node1.get( 'children' ).at(0) === node2 );
ok( node2.get( 'parent' ) === node1 );
equal( node2.get( 'children' ).length, 1 );
ok( node2.get( 'children' ).at(0) === node3 );
ok( node3.get( 'parent' ) === node2 );
equal( node3.get( 'children' ).length, 1 );
ok( node3.get( 'children' ).at(0) === node1 );
});
QUnit.test( "New objects (no 'id' yet) have working relations", function() {
var person = new Person({
name: 'Remi'
});
person.set( { user: { login: '1', email: '1' } } );
var user1 = person.get( 'user' );
ok( user1 instanceof User, "User created on Person" );
equal( user1.get('login'), '1', "person.user is the correct User" );
var user2 = new User({
login: '2',
email: '2'
});
ok( user2.get( 'person' ) === null, "'user' doesn't belong to a 'person' yet" );
person.set( { user: user2 } );
ok( user1.get( 'person' ) === null );
ok( person.get( 'user' ) === user2 );
ok( user2.get( 'person' ) === person );
person2.set( { user: user2 } );
ok( person.get( 'user' ) === null );
ok( person2.get( 'user' ) === user2 );
ok( user2.get( 'person' ) === person2 );
});
QUnit.test( "'Save' objects (performing 'set' multiple times without and with id)", 4, function() {
person3
.on( 'add:jobs', function( model, coll ) {
console.log('got here 1');
var company = model.get('company');
ok( company instanceof Company && company.get('ceo').get('name') === 'Lunar boy' && model.get('person') === person3,
"add:jobs: Both Person and Company are set on the Job instance once the event gets fired" );
})
.on( 'remove:jobs', function( model, coll ) {
console.log('got here 2');
ok( false, "remove:jobs: 'person3' should not lose his job" );
});
// Create Models from an object. Should trigger `add:jobs` on `person3`
var company = new Company({
name: 'Luna Corp.',
ceo: {
name: 'Lunar boy'
},
employees: [ { person: 'person-3' } ]
});
company
.on( 'add:employees', function( model, coll ) {
console.log('got here 3');
var company = model.get('company');
ok( company instanceof Company && company.get('ceo').get('name') === 'Lunar boy' && model.get('person') === person3,
"add:employees: Both Person and Company are set on the Company instance once the event gets fired" );
})
.on( 'remove:employees', function( model, coll ) {
console.log('got here 4');
ok( true, "'remove:employees: person3' should lose a job once" );
});
// Backbone.save executes "model.set(model.parse(resp), options)". Set a full map over object, but now with ids.
// Should trigger `remove:employees`, `add:employees`, and `add:jobs`
company.set({
id: 'company-3',
name: 'Big Corp.',
ceo: {
id: 'person-4',
name: 'Lunar boy',
resource_uri: 'person-4'
},
employees: [ { id: 'job-1', person: 'person-3', resource_uri: 'job-1' } ],
resource_uri: 'company-3'
});
// This should not trigger additional `add`/`remove` events
company.set({
employees: [ 'job-1' ]
});
});
QUnit.test( "Set the same value a couple of time, by 'id' and object", function() {
person1.set( { likesALot: 'person-2' } );
person1.set( { likesALot: person2 } );
ok( person1.get('likesALot') === person2 );
ok( person2.get('likedALotBy' ) === person1 );
person1.set( { likesALot: 'person-2' } );
ok( person1.get('likesALot') === person2 );
ok( person2.get('likedALotBy' ) === person1 );
});
QUnit.test( "Numerical keys", function() {
var child1 = new Node({ id: 2, name: 'First child' });
var parent = new Node({ id: 1, children: [2, 3], name: 'Parent' });
var child2 = new Node({ id: 3, name: 'Second child' });
equal( parent.get('children').length, 2 );
ok( parent.get('children').include( child1 ) );
ok( parent.get('children').include( child2 ) );
ok( child1.get('parent') === parent );
equal( child1.get('children').length, 0 );
ok( child2.get('parent') === parent );
equal( child2.get('children').length, 0 );
});
QUnit.test( "Relations that use refs to other models (instead of keys)", function() {
var child1 = new Node({ id: 2, name: 'First child' });
var parent = new Node({ id: 1, children: [child1, 3], name: 'Parent' });
var child2 = new Node({ id: 3, name: 'Second child' });
ok( child1.get('parent') === parent );
equal( child1.get('children').length, 0 );
equal( parent.get('children').length, 2 );
ok( parent.get('children').include( child1 ) );
ok( parent.get('children').include( child2 ) );
var child3 = new Node({ id: 4, parent: parent, name: 'Second child' });
equal( parent.get('children').length, 3 );
ok( parent.get('children').include( child3 ) );
ok( child3.get('parent') === parent );
equal( child3.get('children').length, 0 );
});
QUnit.test( "Add an already existing model (reverseRelation shouldn't exist yet) to a relation as a hash", function() {
// This test caused a race condition to surface:
// The 'relation's constructor initializes the 'reverseRelation', which called 'relation.addRelated' in it's 'initialize'.
// However, 'relation's 'initialize' has not been executed yet, so it doesn't have a 'related' collection yet.
var Properties = Backbone.Relational.Model.extend({});
var View = Backbone.Relational.Model.extend({
relations: [
{
type: Backbone.Relational.HasMany,
key: 'properties',
relatedModel: Properties,
reverseRelation: {
type: Backbone.Relational.HasOne,
key: 'view'
}
}
]
});
var props = new Properties( { id: 1, key: 'width', value: '300px', view: 1 } );
var view = new View({
id: 1,
properties: [ { id: 1, key: 'width', value: '300px', view: 1 } ]
});
ok( props.get( 'view' ) === view );
ok( view.get( 'properties' ).include( props ) );
});
QUnit.test( "Reverse relations are found for models that have not been instantiated and use .extend()", function() {
var View = Backbone.Relational.Model.extend({ });
var Property = Backbone.Relational.Model.extend({
relations: [{
type: Backbone.Relational.HasOne,
key: 'view',
relatedModel: View,
reverseRelation: {
type: Backbone.Relational.HasMany,
key: 'properties'
}
}]
});
var view = new View({
id: 1,
properties: [ { id: 1, key: 'width', value: '300px' } ]
});
ok( view.get( 'properties' ) instanceof Backbone.Relational.Collection );
});
QUnit.test( "Reverse relations found for models that have not been instantiated and run .setup() manually", function() {
// Generated from CoffeeScript code:
// class View extends Backbone.Relational.Model
//
// View.setup()
//
// class Property extends Backbone.Relational.Model
// relations: [
// type: Backbone.Relational.HasOne
// key: 'view'
// relatedModel: View
// reverseRelation:
// type: Backbone.Relational.HasMany
// key: 'properties'
// ]
//
// Property.setup()
var Property, View,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
View = ( function( _super ) {
__extends(View, _super);
View.name = 'View';
function View() {
return View.__super__.constructor.apply( this, arguments );
}
return View;
})( Backbone.Relational.Model );
View.setup();
Property = (function(_super) {
__extends(Property, _super);
Property.name = 'Property';
function Property() {
return Property.__super__.constructor.apply(this, arguments);
}
Property.prototype.relations = [{
type: Backbone.Relational.HasOne,
key: 'view',
relatedModel: View,
reverseRelation: {
type: Backbone.Relational.HasMany,
key: 'properties'
}
}];
return Property;
})(Backbone.Relational.Model);
Property.setup();
var view = new View({
id: 1,
properties: [ { id: 1, key: 'width', value: '300px' } ]
});
ok( view.get( 'properties' ) instanceof Backbone.Relational.Collection );
});
QUnit.test( "ReverseRelations are applied retroactively", function() {
// Use brand new Model types, so we can be sure we don't have any reverse relations cached from previous tests
var NewUser = Backbone.Relational.Model.extend({});
var NewPerson = Backbone.Relational.Model.extend({
relations: [{
type: Backbone.Relational.HasOne,
key: 'user',
relatedModel: NewUser,
reverseRelation: {
type: Backbone.Relational.HasOne,
key: 'person'
}
}]
});
var user = new NewUser( { id: 'newuser-1' } );
//var user2 = new NewUser( { id: 'newuser-2', person: 'newperson-1' } );
var person = new NewPerson( { id: 'newperson-1', user: user } );
ok( person.get('user') === user );
ok( user.get('person') === person );
//console.log( person, user );
});
QUnit.test( "ReverseRelations are applied retroactively (2)", function() {
var models = {};
Backbone.Relational.store.addModelScope( models );
// Use brand new Model types, so we can be sure we don't have any reverse relations cached from previous tests
models.NewPerson = Backbone.Relational.Model.extend({
relations: [{
type: Backbone.Relational.HasOne,
key: 'user',
relatedModel: 'NewUser',
reverseRelation: {
type: Backbone.Relational.HasOne,
key: 'person'
}
}]
});
models.NewUser = Backbone.Relational.Model.extend({});
var user = new models.NewUser( { id: 'newuser-1', person: { id: 'newperson-1' } } );
equal( user.getRelations().length, 1 );
ok( user.get( 'person' ) instanceof models.NewPerson );
});
QUnit.test( "Deep reverse relation starting from a collection", function() {
var nodes = new NodeList([
{
id: 1,
children: [
{
id: 2,
children: [
{
id: 3,
children: [ 1 ]
}
]
}
]
}
]);
var parent = nodes.first();
ok( parent, 'first item accessible after resetting collection' );
ok( parent.collection === nodes, '`parent.collection` is set to `nodes`' );
var child = parent.get( 'children' ).first();
ok( child, '`child` can be retrieved from `parent`' );
ok( child.get( 'parent' ), 'reverse relation from `child` to `parent` works');
var grandchild = child.get( 'children' ).first();
ok( grandchild, '`grandchild` can be retrieved from `child`' );
ok( grandchild.get( 'parent' ), 'reverse relation from `grandchild` to `child` works');
ok( grandchild.get( 'children' ).first() === parent, 'reverse relation from `grandchild` to `parent` works');
ok( parent.get( 'parent' ) === grandchild, 'circular reference from `grandchild` to `parent` works' );
});
QUnit.test( "Deep reverse relation starting from a collection, with existing model", function() {
new Node( { id: 1 } );
var nodes = new NodeList();
nodes.set([
{
id: 1,
children: [
{
id: 2,
children: [
{
id: 3,
children: [ 1 ]
}
]
}
]
}
]);
var parent = nodes.first();
ok( parent && parent.id === 1, 'first item accessible after resetting collection' );
var child = parent.get( 'children' ).first();
ok( child, '`child` can be retrieved from `parent`' );
ok( child.get( 'parent' ), 'reverse relation from `child` to `parent` works');
var grandchild = child.get( 'children' ).first();
ok( grandchild, '`grandchild` can be retrieved from `child`' );
ok( grandchild.get( 'parent' ), 'reverse relation from `grandchild` to `child` works');
ok( grandchild.get( 'children' ).first() === parent, 'reverse relation from `grandchild` to `parent` works');
ok( parent.get( 'parent' ) === grandchild, 'circular reference from `grandchild` to `parent` works' );
});