ember-rails没有加载hasMany Association

时间:2014-06-11 15:19:22

标签: ruby-on-rails ruby-on-rails-4 ember.js ember-rails

使用的宝石:

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

模板quotes.handlebars:

<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
            }
         ]
      }
   ]
}

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

解决

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

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

1 个答案:

答案 0 :(得分: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
      },
      ....
   ]
}

以下是使用extractArray http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractArray

的示例

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

companiesQuotesCount: (->
  this.get('companiesQuotes.length')
 ).property('companiesQuotes.length')