Backbone集合提取所有模型,但只设置一个

时间:2014-10-21 02:27:16

标签: javascript ajax backbone.js coffeescript backbone-collections

我之前已经问过这个问题,但其他答案并没有解决我的问题:

查看:

initialize: ->
   @dataFields = new app.collections.DataFields([], {campaign_id:@id})
   @loadAndRender()

 loadAndRender: ->
   @dataFields.fetch {
   success: ((model, response) ->
     debugger
     @render()
   ), this
   error: ->
}

收藏和型号:

window.app.models.DataField = Backbone.Model.extend(
  initialize: (models, options) ->
    @campaign_id = options.campaign_id
    @id = options.id

  url: ->
    if @id
      '/campaigns/' + @campaign_id + '/data_fields/' + @id
    else
      '/campaigns/' + @campaign_id + '/data_fields'
)

window.app.collections.DataFields = Backbone.Collection.extend(
  model: window.app.models.DataField
  parse : (response, options) ->
    debugger
    response

  initialize: (models, options) ->
    @campaign_id = options.campaign_id

  url: ->
    '/campaigns/' + @campaign_id + '/data_fields'
)

网络日志显示所有'data_fields'都是从远程服务中正确提取的。

第一个块中的第一个调试器语句允许我验证尽管collect中的所有记录都是通过线路进行的,但只有第一个模型被保存到集合中。

第二个块中的第二个调试器语句允许我验证'response'对象确实包含所有模型的json。从服务返回的数据如下所示:

[{"id":78,"name":"campaign_provider_id","campaign_id":197,"description":"campaign specific identifier","data_type":"string","required_flag":true,"unique_flag":null,"system_flag":true,"display_name":"CampaignProviderID","display_order":1},{"id":79,"name":"campaign_api_key","campaign_id":197,"description":"campaign specific API key","data_type":"string","required_flag":true,"unique_flag":null,"system_flag":true,"display_name":"CampaignAPIKey","display_order":2},{"id":80,"name":"provider_api_key","campaign_id":197,"description":"provider specific API key","data_type":"string","required_flag":true,"unique_flag":null,"system_flag":true,"display_name":"ProviderAPIKey","display_order":3},{"id":81,"name":"email","campaign_id":197,"description":"valid email address of individual","data_type":"email","required_flag":true,"unique_flag":true,"system_flag":null,"display_name":"Email","display_order":4},{"id":82,"name":"first_name","campaign_id":197,"description":"individual's surname","data_type":"string","required_flag":null,"unique_flag":null,"system_flag":null,"display_name":"FirstName","display_order":5},{"id":83,"name":"last_name","campaign_id":197,"description":"individual's family name","data_type":"string","required_flag":null,"unique_flag":null,"system_flag":null,"display_name":"LastName","display_order":6},{"id":84,"name":"company","campaign_id":197,"description":"company or organization that the individual represents","data_type":"string","required_flag":true,"unique_flag":null,"system_flag":null,"display_name":"Company","display_order":7},{"id":85,"name":"country","campaign_id":197,"description":"country where individual or company resides","data_type":"string","required_flag":true,"unique_flag":null,"system_flag":null,"display_name":"Country","display_order":8},{"id":86,"name":"postal_code","campaign_id":197,"description":"zip or postal code where individual or company resides","data_type":"string","required_flag":true,"unique_flag":null,"system_flag":null,"display_name":"ZipCode","display_order":9},{"id":87,"name":"network_size","campaign_id":197,"description":"estimate of the number of individuals in company or organization","data_type":"integer","required_flag":true,"unique_flag":null,"system_flag":null,"display_name":"NetworkSize","display_order":10},{"id":88,"name":"asset_name","campaign_id":197,"description":"name of entity (product, event, service) associated with this lead","data_type":"email","required_flag":true,"unique_flag":null,"system_flag":null,"display_name":"AssetName","display_order":11}]

如果您将数据放入ison解析器,您会注意到没有具有相同ID值的模型......

为什么我的集合不包含从服务中提取的所有模型?

更新基于'mu is to short'答案:


根据mu的答案,我将收藏模式改为:

window.app.models.DataField = Backbone.Model.extend(
  url: ->
      '/campaigns/' + @get('campaign_id') + '/data_fields/' + @get('id')
)

window.app.collections.DataFields = Backbone.Collection.extend(
  model: window.app.models.DataField
  url: ->
    '/campaigns/' + @get('campaign_id') + '/data_fields'
)

当我打电话给以下人员时:

app.views.DataFields = Backbone.View.extend(
  events:
    'submit #delete-campaign form': 'onSubmitDeleteCampaign'
    'click .data-field-row [name="required_flag"]' : 'onClickRequiredFlag'
    'click .data-field-row [name="unique-radios"]' : 'onClickUniqueFlag'

  initialize: ->
    @dataFields = new app.collections.DataFields(campaign_id:@id)
    @loadAndRender()

  loadAndRender: ->
    @dataFields.fetch {
      success: (model, response) =>
        debugger
        @render()
      , this
      error: ->
    }

现在,当我调用@ dataFields.fetch时,它会获取/ campaigns / undefined / data_fields。在实际正确创建包含广告系列ID的网址字符串之前。问题是,虽然响应正确地返回了所有模型,但集合只是在列表中设置了第一个模型。

1 个答案:

答案 0 :(得分:2)

模型的initialize的参数是错误的。你说的是:

initialize: (models, options) ->

但是they're actually

  

构造函数/初始化 new Model([attributes], [options])

这意味着您总是说@id = options.id这只是一种复杂的说法@id = undefined,所有模型最终都会使用相同的@id(恰好是undefined {1}})。

此外,Backbone将自行处理@id,您不应该尝试自己设置。所以你的initialize看起来应该更像这样:

initialize: (attributes, options) ->
  @campaign_id = attributes.campaign_id

您也可以完全跳过@campaign_id并使用@get('campaign_id')(或@attributes.campaign_id,如果您认为@get太慢但请不要浪费您的时间微优化);如果你这样做,那么你根本不需要initialize


当我在这里时,Backbone的extend和CoffeeScript的extends一起工作,所以你可以说:

class window.app.models.DataField extends Backbone.Model

并且正确的事情将会发生。

另外,你通常在CoffeeScript中使用=>而不是_.bind,所以你会说:

loadAndRender: ->
  @dataFields.fetch(
    success: (model, response) =>
      debugger
      @render()
  )

您当然不必这样做,有时即使在CoffeeScript中_.bind也有意义。