Ember数据JSONAPIAdapter:获取嵌套资源 [英] Ember data JSONAPIAdapter: fetch nested resources

查看:219
本文介绍了Ember数据JSONAPIAdapter:获取嵌套资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Ember Data的JSONAPIAdapter与嵌套资源一起使用。对于服务器部分,使用django-rest-framework-json-api。



我的(简化)ember模型:



case.js

 导出默认Model.extend({
firstName: attr('string'),
lastName:attr('string'),
评论:hasMany('comment'),
})

comment.js

  export default Model.extend({
text:attr('string'),
case:belongsTo('case'),
})

服务器对 / api / v1 / cases / 4 的响应如下所示: / p>

  {
data:[
{
type:cases
id:4,
attributes:{
first-name:Hans,
last-name:Peter b $ b},
relationships:{
comments:{
meta:{
count:1
},
data:[
{
type:comments,
id:5
}
],
links:{
:http:// localhost:8000 / api / v1 / cases / 4 / comments
}
}
}
}
]

现在,如果我正确地了解了Ember Data和JSON-API规范,ember应该请求code> / api / v1 / cases / 4 / comments 当我引用评论。相反,它请求 / api / v1 / comments / 5 ,这显然返回一个 404



我的问题摘要:




  • 服务器响应是否符合JSON-API规范? / li>
  • 我如何得到ember尊重嵌套的路线?



我正在使用ember v2 .8。

奖金问题:我面对同样的问题,创建一个新的评论 - 我如何得到ember到 POST to / case / 4 / comments 而不是 / comments

解决方案

JSON API规范不强制执行任何特定的网址格式,所以您要做的是符合条件。但是,我发现,使用Ember Data可以更轻松地使用平面URL结构,但有一个解决方法。



您将要查看 ember-data-url-templates 插件,并将一些逻辑从它添加到您的模型的适配器。 p>

使用该插件,您可以使用 app / adapters / comment.js

 从'./application'导入ApplicationAdapter; 
从ember-data-url-templates导入UrlTemplates;

导出默认ApplicationAdapter.extend(UrlTemplates,{
命名空间:'api / v1',//您可能需要或可能不需要此命名空间设置:
//我是这个区域有点生锈:)

urlTemplate:'{+ host} / case / {caseId} / comments {/ id}',

urlSegments:{
contentId:function(type,id,snapshot / *,query * /){
return snapshot.belongsTo('case',{id:true});
}
}
});

除非有其他附件可以解决这个问题,我相信这会锁定你整个应用程式中的评论网址结构。在决定下线之前,绝对权衡这个权衡。


I'm trying to get Ember Data's JSONAPIAdapter to work with nested resources. For the server part django-rest-framework-json-api is used.

My (simplified) ember models:

case.js

export default Model.extend({
  firstName: attr('string'),
  lastName: attr('string'),
  comments: hasMany('comment'),
})

comment.js

export default Model.extend({
  text: attr('string'),
  case: belongsTo('case'),
})

The server's response for /api/v1/cases/4 looks like this:

{
  "data": [
    {
      "type": "cases",
      "id": "4",
      "attributes": {
         "first-name": "Hans",
         "last-name": "Peter",
      },
      "relationships": {
        "comments": {
          "meta": {
            "count": 1
          },
          "data": [
            {
              "type": "comments",
              "id": "5"
            }
          ],
          "links": {
            "related": "http://localhost:8000/api/v1/cases/4/comments"
          }
        }
      }
    }
  ]
}

Now, if i understand Ember Data and the JSON-API spec correctly, ember should request /api/v1/cases/4/comments when i reference the comments. Instead, it requests /api/v1/comments/5, which obviously returns a 404.

My questions in summary:

  • Does the server response comply to the JSON-API spec?
  • How do i get ember to respect the nested route?

I'm using ember v2.8.

Bonus question: I face the same problem for creating a new comment - how do i get ember to POST to /case/4/comments instead of /comments?

解决方案

The JSON API spec does not enforce any specific URL pattern, so what you're trying to do is compliant. However, I find that working with a flat URL structure is easier with Ember Data, though there is a workaround.

You'll want to look at the ember-data-url-templates addon and add some logic from it to your model's adapter.

With that addon, here is what you can do with app/adapters/comment.js:

import ApplicationAdapter from './application';
import UrlTemplates from 'ember-data-url-templates';

export default ApplicationAdapter.extend(UrlTemplates, {
  namespace: 'api/v1', // You may or may not need this namespace setting:
                       // I'm a little rusty in this area :)

  urlTemplate: '{+host}/case/{caseId}/comments{/id}',

  urlSegments: {
    contentId: function(type, id, snapshot/*, query */) {
      return snapshot.belongsTo('case', { id: true });
    }
  }
});

Unless there is something else that the addon allows to get around this, I believe that this then locks you into that URL structure for comments across your entire app. So definitely weigh that tradeoff before deciding to go down this route.

这篇关于Ember数据JSONAPIAdapter:获取嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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