I’m using Backbone with Backbone-relational. I have two models, Appointment and Client, where a Client can have many Appointments. Here’s my Client model definition (in CoffeeScript):
class Snip.Models.Client extends Backbone.RelationalModel
paramRoot: 'client'
relations: [
type: Backbone.HasMany
key: 'appointments'
relatedModel: 'Snip.Models.Appointment'
collectionType: 'Snip.Collections.AppointmentsCollection'
reverseRelation:
type: Backbone.HasOne
key: 'client'
includeInJSON: 'id'
]
defaults:
name: null
phone: null
email: null
notes: null
active: null
class Snip.Collections.ClientsCollection extends Backbone.Collection
model: Snip.Models.Client
url: '/clients'
And here’s my Appointment model definition:
class Snip.Models.Appointment extends Backbone.RelationalModel
paramRoot: 'appointment'
defaults:
time_block_type_code: 'APPOINTMENT'
start_time: null
stylist: null
salon: null
client: Snip.Models.Client() # This doens't work
class Snip.Collections.AppointmentsCollection extends Backbone.Collection
model: Snip.Models.Appointment
url: '/appointments'
Here’s the problem: since Client references Appointment, I need to include the Appointment file before the Client file so the Snip.Models.Appointment class will exist by the time I reference it. However, Appointment also references Client, so it’s kind of a catch-22 situation. I don’t know what to do.
First, when using Backbone-Relational don’t forget to initialize reverse relations:
Second, in your defaults in key
clientshould be attrs for new client model (It will be created by Backbone-Relational). And if you don’t have any leave empty hash: