灰烬,hasMany和belongsTo无法正常工作

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

我尝试了一个简单的emberapp来显示一些用户数据.

I tried a simple emberapp to display some userdata.

我的API向我返回了这个json:

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":[ ] } ] }

我的用户/model.js:

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'), });

和我的role/model.js

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.

推荐答案

您将需要侧载数据并使API返回数据,如下所示:

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": [] }] }

如果您必须像以前一样嵌入数据,则可能需要研究EmbeddedRecordsMixin [ emberjs/api/data/classes/DS.EmbeddedRecordsMixin.html]

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)遵循json-api( jsonapi/),因为ember数据紧随其后并支持它是开箱即用的(我认为从1.13.0版开始)

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)接受它还是抛出它的想法-我不知道您的要求,但理想情况下,用户模型与角色之间应具有许多关系.所以我会做这样的事情:

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.

更多推荐

灰烬,hasMany和belongsTo无法正常工作

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

发布评论

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

>www.elefans.com

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