简体   繁体   English

如何重新加载 ember-data 与 ember octane 中的 hasMany 关系?

[英]How to reload hasMany relationship in ember-data with ember octane?

After upgrading to Ember 3.21.2 from 3.9, reloading hasMany relationships is not working properly anymore.从 3.9 升级到 Ember 3.21.2 后,重新加载 hasMany 关系不再正常工作。 For example the following model hook to fetch editable contents for a user does not update the user model anymore.例如,下面的 model 挂钩为用户获取可编辑内容不再更新用户 model。

model(params) {
    const { user } = this.modelFor('application')

    const requestParams = this.mapParams(params)

    return RSVP.hash({
      user,
      results: user.hasMany('editableContents').reload({
        adapterOptions: requestParams
      })
    })
  },

It still triggers requests, but it loads the same contents with every request, even after the request params have changed.它仍然会触发请求,但它会为每个请求加载相同的内容,即使请求参数已更改。 Initially the request is sent to /users/:user_id/editable-contents?filter=..... .最初,请求被发送到/users/:user_id/editable-contents?filter=.....

After changing the adapter options it sends a request for each content to /contents/:content_id更改适配器选项后,它将对每个内容的请求发送到/contents/:content_id

We believe the.reload() function is to blame, because we found out that the.hasMany('editableContents').reload() does not jump to the findHasMany() hook in our application adapter, but instead calls findRecord() for each record.我们认为 .reload() function 是罪魁祸首,因为我们发现 .hasMany('editableContents').reload() 没有跳转到我们的应用程序适配器中的 findHasMany() 钩子,而是调用 findRecord()每条记录。

We are using:我们正在使用:

"ember-cli": "~3.21.2",
"ember-data": "~3.21.0"

Any help is appreciated.任何帮助表示赞赏。 Thanks!谢谢!

With the help of user sly7-7 on Ember's Discord server we got pointed in the right direction.在 Ember 的 Discord 服务器上的用户 sly7-7 的帮助下,我们找到了正确的方向。

The real problem was that our payload was missing the "related" link, because a paginated payload does not contain that link.真正的问题是我们的有效负载缺少“相关”链接,因为分页有效负载不包含该链接。 A missing related link wasn't a problem before a change in ember-data that implemented the following block that will never be called if payload.links.related is undefined:在实施以下块的 ember-data 更改之前,丢失的相关链接不是问题,如果payload.links.related未定义,则永远不会调用该块:

if (payload.links) {
      let originalLinks = this.links;
      this.updateLinks(payload.links);
      if (payload.links.related) {
        let relatedLink = _normalizeLink(payload.links.related);
        let currentLink = originalLinks && originalLinks.related ? _normalizeLink(originalLinks.related) : null;
...
}

see: https://github.com/emberjs/data/blob/ff4f9111fcfa7dd9e39804ed17f5af27a4a01378/packages/record-data/addon/-private/relationships/state/relationship.ts#L633见: https://github.com/emberjs/data/blob/ff4f9111fcfa7dd9e39804ed17f5af27a4a01378/packages/record-data/addon/-private/relationships/state/relationship.ts#L633

As a workaround we overrode the normalizeArrayResponse() hook in our application serializer and set the related link to the base request link if there is no related link:作为一种解决方法,我们覆盖了应用程序序列化程序中的normalizeArrayResponse()钩子,如果没有相关链接,则将相关链接设置为基本请求链接:

normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
  if (payload.links && payload.links.first && !payload.links.related) {
    const baseLink = payload.links.first.split('?')[0]
    if (isRelationshipLink(baseLink)) {
      payload.links.related = baseLink
    }
  }
}

With that workaround, which also uses a rather hacky way to see if the link is a relationship link, the.reload() function works again globally in our application.使用这种解决方法,它也使用一种相当老套的方法来查看链接是否是关系链接,the.reload() function 在我们的应用程序中再次全局工作。

To be safe we will also see if we can send the related links with the backend response, which would be cleaner than above workaround.为了安全起见,我们还将查看是否可以将相关链接与后端响应一起发送,这将比上述解决方法更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM