Ember,hasMany和belongsTo不起作用 [英] Ember, hasMany and belongsTo doesn't work
问题描述
我的API返回我这个json:
{
roles:[
{
id:5,
name:admin,
alias:Administrator,
users:[
{
id:1,
username:Evolutio,
email :mail@evolutio.tld,
display_role:管理员
}
]
},
{
id 2,
name:user,
alias:Benutzer,
users:[
]
}
{
id:1,
name:banned,
alias:Gesperrt,
users:[
]
},
{
id:3,
名称:mod,
别名:主持人,
users:[
]
},
{
id:4,
name:support,
alias:支持者,
users:[
]
}
]
}
我的用户/ model.js:
从ember-data导入DS;
导出默认DS.Model.extend({
用户名:DS.attr('string'),
电子邮件:DS.attr('string'),
display_role:DS.attr('string'),
roles:DS.belongsTo('role'),
});
和我的角色/ model.js
从ember-data导入DS;
导出默认DS.Model.extend({
名称:DS.attr('string'),
别名:DS.attr('string'),
用户:DS.hasMany('user'),
});
使用此设置,我的开发者控制台中出现此错误:
处理路由时出错:team.index Assertion Failed:传递类到存储方法已被删除。请传递一个dasherized字符串而不是未定义的EmberError @
我没有得到错误。也许任何人都可以帮助我。
您需要加载数据并使您的API返回数据,如下所示:
{
users:[{
id:1,
username:Evolutio,
email:mail@evolutio.tld,
display_role:管理员,
角色:[5]
}],
:[{
id:5,
name:admin,
别名:管理员,
用户:[1]
},{
id:2,
name:user,
alias:Benutzer,
users
},{
id:1,
name:banned,
alias:Gesperrt,
users ]
},{
id:3,
name:mod,
alias:主持人,
users []
},{
id:4,
name:support,
alias:支持者,
users:[]
}]
}
如果你必须嵌入像您这样做的数据,您可能需要查看EmbeddedRecordsMixin [ http:// emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html]
但我想提出几点:
1)遵循json-api( http://jsonapi.org/ ),因为垃圾数据遵循它,并支持它开箱即用(从1.13.0起我认为)
2)拿它或扔它的想法 - 我不知道您的要求,但最理想的用户模式将与角色有很多关系。所以我会做一些这样的事情:
// user / model.js:
import'烬数据';
导出默认DS.Model.extend({
用户名:DS.attr('string'),
电子邮件:DS.attr('string'),
// display_role:DS.attr('string'),//你不需要这个,因为用户可以有多个角色,你也可以通过使用model.roles.forEach((role )=> {role.alias或role.name})
roles:DS.belongsTo('role')
});
//role/model.js
从ember-data导入DS;
导出默认DS.Model.extend({
名称:DS.attr('string'),
别名:DS.attr('string')
});
因为一般来说,角色比用户要少,你也不想加载所有的数据(角色和用户)。
I tried a simple emberapp to display some userdata.
My API returns me this 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":[
]
}
]
}
my 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'),
roles: DS.belongsTo('role'),
});
and my 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'),
});
With this setup I got this error in my developer console:
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@
I didn't get the mistake. Maybe anyone can help me for this.
You would need to sideload the data and make your API return data like this:
{
"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": []
}]
}
If you must embed the data like you did, you might want to look into EmbeddedRecordsMixin[http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html]
But I would like to suggest few things:
1) follow json-api(http://jsonapi.org/) since ember data follows it and supports it out of the box( starting from ver 1.13.0 I think)
2) Take it or throw it idea - I don't know your requirement but ideally user model would have hasMany relationship to roles. So I would do things bit differently like this:
//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')
});
because generally there will be fewer roles than users and also you wouldn't want to load all that data(roles and users) in one request call.
这篇关于Ember,hasMany和belongsTo不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!