0

我正在将 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 情况。我不知道该怎么办。

4

1 回答 1

0

首先,在使用 Backbone-Relational 时不要忘记初始化反向关系:

Snip.Models.Client.setup()

其次,在您的默认值中,键client应该是新客户端模型的 attrs(它将由 Backbone-Relational 创建)。如果您没有任何留空哈希:

client: {}
于 2012-07-09T14:33:15.350 回答