Ember.js嵌入式记录无法正常工作 [英] Ember.js embedded records don't work

查看:59
本文介绍了Ember.js嵌入式记录无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

{
   "meta":{
      "per":20,
      "page":1,
      "total":2
   },
   "users":[
      {
         "id":119506,
         "first_name":"erglk",
         "last_name":"wfe",
         "email":"bent@exemple.com",
         "groups":[
            {
               "id":5282,
               "name":"test"
            },
            {
               "id":8880,
               "name":"everybody"
            }
         ]
      },
      {
         "id":119507,
         "first_name":"eriglk",
         "last_name":"wife",
         "email":"benit@exemple.com",
         "groups":[
            {
               "id":5284,
               "name":"testf"
            },
            {
               "id":8880,
               "name":"everybody"
            }
         ]
      }
   ]
}

目前访问用户没有问题,但是访问groups数组有些困难.我尝试了 hasMany belongsTo 都没有成功.我有错误.我已经阅读了几篇有关 EmbededRecordMixin 的文章,但没有成功.

For the moment no problem to access the user but I have some difficulties to access the groups array. I've tried hasMany and belongsTo without success. I had errors. I've read few articles about EmbededRecordMixin but without any success.

如果我在模型中声明:

export default DS.Model.extend({
  first_name: DS.attr('string'),
  last_name: DS.attr('string'),
  email: DS.attr('string'),
  groups: DS.attr('group')
});

我得到:处理路由时出错:用户断言失败:无法为组"找到转换错误:断言失败:无法为组"找到转换

推荐答案

我们使用 DS.attr 告诉Ember该字段是模型的属性,并且可以选择指定类型此属性.默认情况下,仅允许的类型为 string number boolean date .为了支持自定义类型,应定义特殊类(转换).这就是Embers试图通过此错误消息告诉您的内容.如何定义此类,您可以在此处

We use DS.attr to tell Ember that this field is an attribute of a model, and optionally we can specify a type of this attribute. By default, only allowed types are string, number, boolean, and date. To support custom type, special class (transform) should be defined. That's what Embers is trying to tell you with this error message. How to define such class, you may find here

但是,您无需为任务定义自定义转换.您需要定义一个关系:

But, you don't need to define a custom transform for your task. You need to define a relationship:

export default DS.Model.extend({
  first_name: DS.attr('string'),
  last_name: DS.attr('string'),
  email: DS.attr('string'),
  groups: DS.hasMany('group', {async: false})
});

并按照官方 docs 中所述使用EmbeddedRecordMixin.我可以向您保证,它可以按照此处的描述进行操作.

And use an EmbeddedRecordMixin, as described in official docs. I can assure you that it works as described there.

这篇关于Ember.js嵌入式记录无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆