无法获得许多关联 [英] Unable to get hasMany association

查看:91
本文介绍了无法获得许多关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 eaa1123 (ember)和 508479d (ember-data)来构建JS文件。

I used commit eaa1123 (ember) and 508479d (ember-data) to build the JS files.

我有从Rails后端返回的以下JSON,它是通过 active_model_serializers 生成的(0.6。 0):

I have the following JSON returned from my Rails backend, which is generated with active_model_serializers (0.6.0):

{
  "posts": [
    {
      "id": 408,
      "title": "Lorem Ipsum",
      "body": "In at quo tempora provident nemo.",
      "comments": [
        {
          "id": 956,
          "body": "Quo incidunt eum dolorem."
        },
        ...
      ]
    }
  ]
}

和以下Ember模型:

and the following Ember models:

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  body: DS.attr('string'),
  comments: DS.hasMany('App.Comment', {
    embedded: true
  })
});

App.Comment = DS.Model.extend({
  body: DS.attr('string'),
  post: DS.belongsTo('App.Post')
});

看起来完全正常:

post = App.Post.find(408);
post.get('title')
// => "Lorem Ipsum"

然而,我似乎无法得到评论:

However, I can't seem to get to the comments:

comments = post.get('comments')
comments.get('firstObject') instanceof App.Comment
// => true
comments.forEach(function(comment) {
  console.log(comment.get('body'))
})
//=> undefined

当我使用:

comments.content

我得到一个包含对象的数组,所以:

I get an Array that contain objects, so:

comments.content[0]
//=> { body: "Quo incidunt eum dolorem.", id: 956 }

但这不是我预期。

看起来很明显,所以我一定要做错事。
作为副作用:目前我无法以简单的方式在模板中呈现我的评论,所以我希望有人可以帮助我。

It seems so obvious, so I must be doing something wrong. As a side-effect: currently I'm not able to render my comments in a template in a easy way, so I hope someone can help me on this one.

提前感谢

推荐答案

如果您使用该提交意味着您正在进行最新的ember数据修订,这是11.添加 embedded:true 来加载嵌入式关联已经不再适用,在版本5或9之间,再也不能确定。

If you used that commit it means you are on the latest ember-data revision, which is 11. Adding embedded: true to load an embedded association was deprecated a while back between revision 5 or 9, not too sure again.

如果使用默认的restAdapter,现在需要将嵌入式加载定义为如下所示的映射,而不是关联选项:

If you are using the default restAdapter, you now need to define embedded loading as a mapping as shown below and not as an association option:

App.store = DS.Store.create({
  revision: 11,
  adapter: DS.RESTAdapter.create()
});

App.store.adapter.serializer.map('App.Post', {
   comments: {embedded: 'load'}
});

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  body: DS.attr('string'),
  comments: DS.hasMany('App.Comment')
});

App.Comment = DS.Model.extend({
   body: DS.attr('string'),
   post: DS.belongsTo('App.Post')
});

您可以通过以下链接关注以前所有的讨论:

You can follow all the previous discussion on it through the links below:

https://github.com/emberjs/data/issues/504# issuecomment-11256934
https://github.com/emberjs/data/pull/430#issuecomment-10925506

https://github.com/emberjs/data/issues/504#issuecomment-11256934 https://github.com/emberjs/data/pull/430#issuecomment-10925506

加载嵌入式的各种修复记录:
https://github.com/emberjs/data/pull/541

Various fixes for loading embedded records: https://github.com/emberjs/data/pull/541

这不是直接相关但是我上面写的所有失败,然后将这个解决方案添加到组合
使用findAssociation时,BelongsTo关联不会实现并提取了许多异步的钩子HasMany:
https://github.com/emberjs/data/issues/525

任何人想要快速查看相关于App.store的呼叫定义的内容。 adapter.serializer.map'

当我们调用App.store.adapter.serializer.map时,对串行器的调用在下面的第536行定义并且该地图在第二个链接中在线696

When we called 'App.store.adapter.serializer.map', the call to serializer is defined on line 536 below and the map is online 696 in the 2nd link

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/adapter.js#L536
https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/adapter.js#L696

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/adapter.js#L536 https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/adapter.js#L696

在线 DS.RESTAdapter DS.Adapter 中的序列号属性指定为 DS.RESTSerializer 添加了 RestAdapter 特有的附加功能。

On line 67 of the DS.RESTAdapter which extends DS.Adapter, the serializer property is made to point to DS.RESTSerializer where additional functionality specific to the RestAdapter are added.

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/ adapters / rest_adapter.js#L67

这篇关于无法获得许多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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