【问题标题】:How to get included values of jsonapi on a router with ember-data?如何在带有 ember-data 的路由器上获取包含的 jsonapi 值?
【发布时间】:2016-09-06 15:51:05
【问题描述】:

我正在使用 jsonapi 指令在 ember.js 应用程序和 API 之间建立连接。我有一个 track,它可能有 50 个或更多 comments,我需要将它们加载到路由上以执行一些逻辑。

这是 API 的示例响应:

{
   "data": {
       "id": 1,
       "type": "track",
       "attributes": {
           "name": "XPTO"
       },
       "relationships": {
           "comment": {
               "data": [
                   {"id": 1, "type": "comment"}
               ]
           }
       }
   },
   "include": [
       {
          "id": 1,
          "type": "comment",
          "attributes": {
             "text": "Lorem ipsum..."
          }
       }
   ]
}

现在想象一下它有 50 个 cmets,每次调用都发出请求会非常消耗。如果我在带有each 循环的视图中执行此操作,它不会发出所有请求,但我尝试在路由中访问它,它将发出所有请求。如何使用以下代码实现?

this.store.findRecord('track', 1).then((t) => {
   // logic here
   // I tried this but it would make all the requests too
   t.get('comments');
})

【问题讨论】:

    标签: ember.js ember-data json-api


    【解决方案1】:

    你可以在你的适配器中使用属性“coalesceFindRequests”

    coalesceFindRequests: true
    

    如果将 coalesceFindRequests 设置为 true,则会触发以下请求:

    GET /comments?ids[]=1&ids[]=2
    

    所以这只调用了所有 cmets。

    注意:请求合并依赖于 URL 构建策略。所以如果你 在您的应用程序组中覆盖 buildURLRecordsForFindMany 更有可能 也应该被覆盖以使合并起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2013-03-12
      • 2015-03-14
      • 2017-05-08
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多