1

鉴于这些模型:

App.TransportDocument = DS.Model.extend
  number: DS.attr 'string'
  date: DS.attr 'string'
  printable_url: DS.attr 'string'
  transport_document_rows: DS.hasMany('App.TransportDocumentRow')

App.TransportDocumentRow = DS.Model.extend
  product_name: DS.attr 'string'
  quantity: DS.attr 'number'
  measure: DS.attr 'string'
  transport_document: DS.belongsTo('App.TransportDocument')

这是我的路线:

App.TransportDocumentsRoute = Ember.Route.extend
  model: -> App.TransportDocument.find()

App.TransportDocumentRoute = Ember.Route.extend
  model: (params)-> App.TransportDocument.find(params.transport_document_id)

App.TransportDocumentRowRoute = Ember.Route.extend
  model: -> App.TransportDocumentRow.find()

我有一个车把模板:

{{#each transport_document in controller}}
  td_id: {{transport_document.id}}
  {{#each transport_document_row in transport_document.transport_document_rows}}
    row_id: {{id}}
  {{/each}}
{{/each}}

我希望我的结果是:

td_id: 1 row_id: 1 row_id: 2 row_id: 3
td_id: 2 row_id: 3 row_id: 4 row_id: 5
td_id: 3 row_id: 8 row_id: 7 row_id: 6

但是是:

td_id: 1 row_id: row_id: row_id:
td_id: 2 row_id: row_id: row_id:
td_id: 3 row_id: row_id: row_id:

这是我的服务器对 /transport_documents 的响应

{"transport_documents":
  [
    {"id":1,"number":"11","date":"17/04/2013","cause":"Conto lavorazione","transport_document_row_ids":[3,2,1],"transport_document_rows":    
      [
        {"transport_document_row":{"id":3,"transport_document_id":1,"product_name":"suola puzzle verde","quantity":"1","measure":"pz","}},
        {"transport_document_row":{"id":2,"transport_document_id":1,"product_name":"lacci rossi","quantity":"2","measure":"pz"}},
        {"transport_document_row":{"id":1,"transport_document_id":1,"product_name":"Rotolo di tela","quantity":"50","measure":"m"}}
      ]},
    {"id":2,"number":"2","date":"18/04/2013","cause":"Conto lavorazione","transport_document_row_ids":[6,5,4],"transport_document_rows":
      [
        {"transport_document_row":{"id":6,"transport_document_id":2,"product_name":"suola puzzle verde","quantity":"1","measure":"pz"}},
        {"transport_document_row":{"id":5,"transport_document_id":2,"product_name":"lacci rossi","quantity":"2","measure":"pz"}},
        {"transport_document_row":{"id":4,"transport_document_id":2,"product_name":"Rotolo di tela","quantity":"50","measure":"m",}}
      ]},
    {"id":3,"number":"3","date":"19/04/2013","cause":"Conto lavorazione","transport_document_row_ids":[9,8,7],"transport_document_rows":
      [
        {"transport_document_row":{"id":9,"transport_document_id":3,"product_name":"suola puzzle verde","quantity":"1","measure":"pz"}},
        {"transport_document_row":{"id":8,"transport_document_id":3,"product_name":"lacci rossi","quantity":"2","measure":"pz"}},
        {"transport_document_row":{"id":7,"transport_document_id":3,"product_name":"Rotolo di tela","quantity":"50","measure":"m"}}]}

我想我在路线上做错了(我在哪里定义查询以查找该文档的所有行?)但我不确定。在指南中找不到类似的东西

谢谢你

4

1 回答 1

0

按照惯例,默认代码 inRoute#serialize将返回一个包含模型 id 的对象,但是属性名称不是id,而是模型名称和 id 的组合,在您的情况下,它应该是transport_document_id

serialize方法返回的对象将作为参数传递给Route#model. 这在路由指南中也有说明。

您的路线应该类似于以下内容:

App.TransportDocumentRoute = Ember.Route.extend
  model: (params)-> 
    App.TransportDocument.find(params.transport_document_id)

或者您可以覆盖serialize您的路线以提供不同的参数名称。您可以在此处找到序列化的不同实现示例。


至于查询,可以类似下面的过滤:

App.TransportDocumentRowRoute = Ember.Route.extend
  model: (params) -> App.store.filter App.TransportDocumentRow, (row) ->
    # might have to implement Route#serialize to also provide the 
    # transport_document_id property in this route
    row.get 'transport_document_id' == params.transport_document_id

但是,这只会过滤您存储的记录。如果尚未获取行记录,您还必须触发 GET 来获取它们。

顺便说一句,对不起,如果语法不是 100%。我真的不会说咖啡:)

于 2013-04-17T19:06:00.867 回答