-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
72 lines (63 loc) · 1.34 KB
/
example.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
// Import
var Backbone = require('backbone');
var BackboneNestyModel = require('./').BackboneNestyModel;
// Eye Model
var EyeModel = Backbone.Model.extend({
attributes: {
color: null,
open: false
}
});
// Eye Collection
var EyeCollection = Backbone.Collection.extend({
model: EyeModel
});
// Mouth Model
var MouthModel = Backbone.Model.extend({
attributes: {
open: false
}
});
// Head Model
var HeadModel = BackboneNestyModel.extend({
// Define our nested collections
collections: {
eyes: EyeCollection
},
// Define our nested models
models: {
mouth: MouthModel
}
});
// Instantiate our head with our nested data
var myHead = new HeadModel({
// will create a mouth model with this data
mouth: {
open: true
},
// will create an eyes collection with this data
eyes: [
// will create an eye model with this data
{
id: 'left',
color: 'green',
open: true
},
// will create an eye model with this data
{
id: 'right',
color: 'green',
open: true
}
]
});
// Check
console.log(myHead.toJSON());
console.log(myHead.get('eyes.left.open')); // true
// ^ equiv to myHeader.get('eyes').get('left').get('open')
// Nested Setter
myHead.set('eyes.left.open', false);
// ^ equiv to myHeader.get('eyes').get('right').set('open', false)
// Check
console.log(myHead.toJSON());
console.log(myHead.get('eyes.left.open')); // false