Ember,hasMany和belongsTo不起作用

时间:2016-04-02 11:24:16

标签: javascript ruby-on-rails ember.js ember-data

我尝试了一个简单的emberapp来显示一些userdata。

我的API给我发了这个json:

{
   "roles":[
      {
         "id":5,
         "name":"admin",
         "alias":"Administrator",
         "users":[
            {
               "id":1,
               "username":"Evolutio",
               "email":"mail@evolutio.tld",
               "display_role":"Administrator"
            }
         ]
      },
      {
         "id":2,
         "name":"user",
         "alias":"Benutzer",
         "users":[

         ]
      },
      {
         "id":1,
         "name":"banned",
         "alias":"Gesperrt",
         "users":[

         ]
      },
      {
         "id":3,
         "name":"mod",
         "alias":"Moderator",
         "users":[

         ]
      },
      {
         "id":4,
         "name":"support",
         "alias":"Supporter",
         "users":[

         ]
      }
   ]
}

我的用户/ model.js:

import DS from 'ember-data';

export default DS.Model.extend({
  username: DS.attr('string'),
  email: DS.attr('string'),
  display_role: DS.attr('string'),

  roles: DS.belongsTo('role'),
});

和我的角色/ model.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  alias: DS.attr('string'),

  users: DS.hasMany('user'),
});

通过此设置,我在开发人员控制台中收到此错误:

Error while processing route: team.index Assertion Failed: Passing classes to store methods has been removed. Please pass a dasherized string instead of undefined EmberError@

我没有弄错。也许任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

您需要侧载数据并使API返回如下数据:

  {
     "users": [{
        "id": 1,
        "username": "Evolutio",
        "email": "mail@evolutio.tld",
        "display_role": "Administrator",
        "roles": [5]
     }],
     "roles": [{
        "id": 5,
        "name": "admin",
        "alias": "Administrator",
        "users": [1]
     }, {
        "id": 2,
        "name": "user",
        "alias": "Benutzer",
        "users": []
     }, {
        "id": 1,
        "name": "banned",
        "alias": "Gesperrt",
        "users": []
     }, {
        "id": 3,
        "name": "mod",
        "alias": "Moderator",
        "users": []
     }, {
        "id": 4,
        "name": "support",
        "alias": "Supporter",
        "users": []
     }]
  }

如果必须像过滤那样嵌入数据,可能需要查看EmbeddedRecordsMixin [http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html]

但我想提出一些建议:

1)遵循json-api(http://jsonapi.org/),因为ember数据跟随它并开箱即用(从我认为的ver 1.13.0开始)

2)接受或抛出它的想法 - 我不知道你的要求,但理想情况下,用户模型与角色有很多关系。所以我会做的事情有点不同:

//user/model.js:
import DS from 'ember-data';

export default DS.Model.extend({
  username: DS.attr('string'),
  email: DS.attr('string'),
  //display_role: DS.attr('string'),//you don't need this, since user can have multiple roles and also you can access it from the user model itself by doing model.roles.forEach((role)=>{ role.alias or role.name})
  roles: DS.belongsTo('role')
});


//role/model.js
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  alias: DS.attr('string')
});

因为通常角色会比用户少,而且您也不希望在一次请求调用中加载所有数据(角色和用户)。