Ember.js嵌入式记录不起作用 [英] Ember.js embedded records don't work

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

问题描述

我有一个json像

{
   "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"
            }
         ]
      }
   ]
}

目前没有问题访问用户,但我访问组数组有一些困难。我试过 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.

如果我在我的模型中声明:

If I declare in my models :

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

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

推荐答案

我们使用 DS.attr 告诉Ember这个字段是模型的一个属性,可选地我们可以指定一个这个属性的类型。默认情况下,只允许的类型是 string number boolean 日期。要支持自定义类型,应定义特殊类(变换)。这就是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})
});

并使用EmbeddedRecordMixin,如官方 docs 。我可以向你保证,它的工作原理如下所述。

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

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

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