Emberjs在父模型模板中显示关联数据

时间:2013-12-10 22:19:35

标签: ember.js associations ember-data

我在我的Ember应用程序中遇到了障碍,我一直试图摆脱它。我有两个模型如下:

App.UserModel = DS.Model.extend({
  ../
  actions: DS.hasMany('action')
});

App.ActionModel = DS.Model.extend({
  ../
  user: DS.belongsTo('user')
});

我查看用户个人资料的路线如下:

App.UsersProfileRoute = Em.Route.extend({
  model: function(params) {
    return this.get('store').find('user', params.user_id);
  }
});

从服务器获取以下结构:

{
  user: {
    action_ids: [1, 2, 3]
  },
  actions: [{
    id: 1,
    type: "Problem"
  },{
    id: 2,
    type: "Problem"
  },{
    id: 3,
    type: "Problem"
  }]
}

根据Ember的Chrome扩展程序正确加载商店。但是当我尝试在模板中访问关联数据时如下:

<h2>Actions</h2>
<div class="container">
  {{#each actions}}
    you have action
  {{else}}
    You don't have actions
  {{/each}}
</div>

似乎action关联为空或null,因为即使Ember Store显示正确的存储信息,它也不会通过#each检查。

我尝试了一些模板,例如{{#each action in model.actions}}等,但无济于事。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

user: {
  action_ids: [1, 2, 3]
},

应该是

user: {
  actions: [1, 2, 3]
},
相关问题