Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewdeandrade/918650 to your computer and use it in GitHub Desktop.
Save andrewdeandrade/918650 to your computer and use it in GitHub Desktop.
// The following is your top-level parent model in serialized JSON format:
var myModel = new Person({
"name" : {
"first_name" : "Andrew",
"last_name" : "de Andrade"
},
"address" : {
"street_address_1" : "Avenida Paulista 1000",
"street_address_2" : "Apt. 101",
"zip_code" : "12345-0000"
},
"vehicle" : {
"make" : "Toyota",
"model" : "FJ-40 Landcruiser",
"year" : "1966",
"VIN" : "123456789ABCDEF"
}
});
// Parent Model:
Person = Backbone.Model.extend({
defaults: {
"name" : new Name,
"address" : new Address,
"car" : new Car
},
initialize: function() {
this.set({
"name" : new Name(this.get('name')),
"address" : new Address(this.get('address')),
"car" : new Car(this.get('car'))
});
}
});
// Children Models:
Name = Backbone.Model.extend({
defaults: {
"first_name" : "",
"last_name" : "",
},
initialize: function() {
}
});
Address = Backbone.Model.extend({
defaults: {
"street_address_1" : "",
"street_address_2" : "",
"zip_code" : "00000-0000"
},
initialize: function() {
}
});
Car = Backbone.Model.extend({
defaults: {
"make" : "",
"model" : "",
"year" : null
"VIN" : null
},
initialize: function() {
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment