我正在将 Backbone 与 Backbone-relational 一起使用。我有两个模型,Appointment并且Client,其中 aClient可以有很多Appointments. 这是我的Client模型定义(在 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'
这是我的Appointment模型定义:
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'
这就是问题所在:因为Clientreferences Appointment,我需要在Appointment文件之前包含Client文件,这样Snip.Models.Appointment当我引用它时该类就会存在。但是,Appointment 也引用Client,所以这是一种 catch-22 情况。我不知道该怎么办。