Ember,hasMany和belongsTo不起作用

编程入门 行业动态 更新时间:2024-10-25 10:26:05
本文介绍了Ember,hasMany和belongsTo不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的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/api/data/classes/DS.EmbeddedRecordsMixin.html]

但我想提出几点:

1)遵循json-api( jsonapi/ ),因为垃圾数据遵循它,并支持它开箱即用(从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[emberjs/api/data/classes/DS.EmbeddedRecordsMixin.html]

But I would like to suggest few things:

1) follow json-api(jsonapi/) 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不起作用

本文发布于:2023-10-21 17:20:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1514856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   Ember   hasMany   belongsTo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!