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

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

问题描述

我使用了提交 ea1123 (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-data 修订版,即 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-11256934https://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 和 extractHasMany 钩子进行异步 HasMany 时,BelongsTo 关联不会具体化:https://github.com/emberjs/data/issues/525

This not directly related but incase all i wrote above fails, then add this solution to the mix BelongsTo association are not materialized when using findAssociation and extractHasMany hooks for async HasMany: https://github.com/emberjs/data/issues/525

任何想要快速查看有关App.store.adapter.serializer.map"调用的定义位置的人的内部结构

当我们调用App.store.adapter.serializer.map"时,对序列化程序的调用定义在下面的第 536 行,地图在第 2 个链接中的第 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#L536https://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.AdapterDS.RESTAdapter 的第 67 行,serializer 属性 指向 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

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

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