我正在使用主幹關係的主幹。我有兩個型號,Appointment
和Client
,其中一個Client
可以有很多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'
這裏的問題:因爲Client
引用Appointment
,我需要包括Client
前Appointment
文件文件,所以Snip.Models.Appointment
類將在我引用它時存在。然而,Appointment
也引用Client
,所以這是一種catch-22的情況。我不知道該怎麼辦。
根據這個帖子(http://stackoverflow.com/questions/10060561/backbone-js-using-new-in-model-defaults-circular -reference),也許我不應該做'client:Snip.Models.Client()'。如果我不這樣做,但我在哪裏做? 'client'屬性需要是一個'Snip.Models.Client'。 – 2012-07-08 19:17:11