【问题标题】:how to get record count of nested model in Ember.js如何在 Ember.js 中获取嵌套模型的记录数
【发布时间】:2023-03-07 05:36:01
【问题描述】:

我正在构建我的第一个 rails/ember 应用程序,并使用来自教程和 ember 文档的各种资源进行研究。但是,我碰壁了,经过大量的挖掘,我仍然无法找到我正在寻找的答案。目前,我正在构建一个 GiftList Rails/Ember 应用程序,其中索引页面是关系,并且关系有很多收件人。现在,我显示了关系,我希望它显示每个关系的收件人数量,但是每个关系显示为 0,而事实上,至少有一个关系有一个收件人(家庭有一个记录)。当我在 Ember 检查器中检查数据时,GiftList.Recipient 有 1 条记录,GiftList.Relationship 有 4 条记录。如何正确显示正确数量的收件人记录?

路线

Router.js.coffee:

GiftList.Router.map ->
  @resource 'relationships', { path: '/' }, ->
    @resource 'relationship', { path: ':relationship_id' }, ->
      @resource 'recipients', { path: 'recipients' }, ->
        @resource 'recipient', { path: ':recipient_id'}

relationships_route.js.coffee:

GiftList.RelationshipsRoute = Ember.Route.extend
  model: ->
    @store.find('relationship')

recipients_route.js.coffee:

GiftList.RecipientsRoute = Ember.Route.extend
  model: ->
    @store.find('recipient')

控制器

relationships_controller.js.coffee:

GiftList.RelationshipsController = Ember.ArrayController.extend({})

relationship_controller.js.coffee:

GiftList.RelationshipController = Ember.ArrayController.extend
  recipients: GiftList.Recipient.find()

模型

relationship.js.coffee:

GiftList.Relationship = DS.Model.extend
  name: DS.attr('string')
  recipients: DS.hasMany('recipient')

recipient.js.coffee:

GiftList.Recipient = DS.Model.extend
  first_name: DS.attr('string')
  last_name: DS.attr('string')
  relationship: DS.belongsTo('relationship')

模板

relationships.handlebars:

<h2>Relationships</h2>
  <ul id="relationships">
    {{#each}}
      <li>{{name}}({{recipients.length}})</li>
    {{/each}}
  </ul>

*更新***

序列化器

class RecipientSerializer < ActiveModel::Serializer
  embed :id
  attributes :id, :first_name, :last_name
  has_one :relationship, key: :relationship

end

class RelationshipSerializer < ActiveModel::Serializer
  embed :ids, include: true
  attributes :id, :name
  has_many :recipients, key: :recipients

end

所有其他代码保持不变

【问题讨论】:

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


    【解决方案1】:

    我假设您使用的是 ember-data 测试版。如果您使用的是旧版本,则此答案将不适用。

    当您要获取的是特定关系的收件人时,您正在获取所有关系和所有收件人。如果您不想将 hasMany 关系作为旁加载记录嵌入到父模型中,则需要将关系声明为异步:

    GiftList.Relationship = DS.Model.extend
      name: DS.attr('string')
      recipients: DS.hasMany('recipient', { async: true })
    

    那么你只需要获取关系的接收者:

    GiftList.RecipientsRoute = Ember.Route.extend
      model: ->
        @modelFor('relationship').get('recipients')
    

    这将对 /relationships/:id/recipients 进行异步调用以获取关系的收件人,因为您要求异步 hasMany 尚未加载到关系模型上的属性。

    【讨论】:

    • 感谢您的回复。是的,我正在使用 embder data beta:1.0.0 beta。我已经尝试过你的建议,但没有奏效。我应该提一下,我的控制台中出现了由收件人引起的未捕获类型错误:我的 relationship_controller.js.coffee 文件中的 GiftList.Recipient.find()。你认为这是导致它不起作用的原因吗?
    • 尝试从控制器中删除此行:recipients: GiftList.Recipient.find()。我从记忆中开始,所以语法可能略有偏差,但谷歌 async 关键字或搜索 ember-data 源以获取文档。
    • 其实语法是对的。我追查出了问题所在,问题是我的模型没有正确序列化。我明天会发布更新。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-02-29
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多