0

使用的宝石:

Using ember-source (1.5.1.1)
Using ember-data-source (1.0.0.beta.7)
Using ember-rails (0.15.0)
Using handlebars-source (1.3.0)
Using active_model_serializers (0.8.1)

这是 ember 应用程序代码:

window.App = Ember.Application.create
  LOG_TRANSITIONS: true
  rootElement: '#app-ember-root'

App.store = DS.Store.extend({});

App.ApplicationAdapter = DS.ActiveModelAdapter.extend({});

App.Router.map ()->
  @resource 'quotes',
    path: '/'

App.QuotesRoute = Ember.Route.extend
  model: ()->
    this.store.find 'quote'

App.CompaniesQuote = DS.Model.extend
  quote: DS.belongsTo 'quote'

App.Quote = DS.Model.extend
  message: DS.attr 'string'
  createdAt: DS.attr 'date'
  companiesQuotes: DS.hasMany('companiesQuote')
  companiesQuotesCount: (->
    this.get('companiesQuotes.length')
  ).property('companiesQuotes')

序列化器:

class QuoteSerializer < ActiveModel::Serializer
  attributes :id, :message, :created_at

  has_many :companies_quotes
end

class CompaniesQuoteSerializer < ActiveModel::Serializer
  attributes :id, :company_id
end

模板报价。车把:

<div class="quotes-list">
  <div class="quote-container">
    {{#each}}  
      {{ companiesQuotesCount }} companies quote for this quote {{ id }}
    {{/each }}
  </div>
</div>

/引用 JSON 响应:

{
   "quotes":[
      {
         "id":10,
         "message":"Quote 10!",
         "created_at":"2014-06-04T17:00:01.000Z",
         "companies_quotes":[
            {
               "id":27,
               "company_id":1
            },
            {
               "id":28,
               "company_id":2
            },
            {
               "id":26,
               "company_id":3
            }
         ]
      },
      {
         "id":11,
         "message":"Quote 11!",
         "created_at":"2014-06-11T14:45:02.000Z",
         "companies_quotes":[
            {
               "id":30,
               "company_id":1
            },
            {
               "id":31,
               "company_id":2
            },
            {
               "id":29,
               "company_id":3
            }
         ]
      }
   ]
}

使用此环境/代码,该属性companiesQuotesCount始终为 0。我错过了什么?

解决了

使用@kingpin2k 的响应,我更改了 JSON 结构修改QuoteSerializer

class QuoteSerializer < ActiveModel::Serializer
  embed :ids, include: true
  attributes :id, :message, :created_at
  has_many :companies_quotes
end
4

1 回答 1

1

Ember Data 默认不做嵌入记录。

您要么需要使用客户端序列化程序修复 json,要么在服务器端修复它。

它应该是这种格式:

{
   "quotes":[
      {
         "id":10,
         "message":"Quote 10!",
         "created_at":"2014-06-04T17:00:01.000Z",
         "companies_quotes":[27, 28, 29]
      },
      {
         "id":11,
         "message":"Quote 11!",
         "created_at":"2014-06-11T14:45:02.000Z",
         "companies_quotes":[30, 31, 32]
         ]
      }
   ],
   companies_quotes:[
      {
         "id":27,
         "company_id":1
      },
      {
        "id":28,
        "company_id":2
      },
      {
        "id":26,
        "company_id":3
      },
      ....
   ]
}

这是使用http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractArray的示例extractArray

此外,您的计算属性只是观察参考,它不会随着长度的变化而改变。

companiesQuotesCount: (->
  this.get('companiesQuotes.length')
 ).property('companiesQuotes.length')
于 2014-06-11T16:08:12.920 回答