簡體   English   中英

灰燼數據JSONAPIAdapter:獲取嵌套資源

[英]Ember data JSONAPIAdapter: fetch nested resources

我正在嘗試讓Ember Data的JSONAPIAdapter與嵌套資源一起使用。 對於服務器部分,使用django-rest-framework-json-api。

我的(簡化的)余燼模型:

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'),
})

服務器對/api/v1/cases/4的響應如下所示:

{
  "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"
          }
        }
      }
    }
  ]
}

現在,如果我正確理解了Ember Data和JSON-API規范,則當我引用/api/v1/cases/4/comments時,ember應該請求/api/v1/cases/4/comments comments。 相反,它會請求/api/v1/comments/5 ,顯然會返回404

我的問題總結如下:

  • 服務器響應是否符合JSON-API規范?
  • 我如何使炭燼尊重嵌套路線?

我正在使用ember v2.8。

獎金的問題:我面臨着創建一個新的評論同樣的問題-如何獲得燼到POST/case/4/comments ,而不是/comments

JSON API規范不強制執行任何特定的URL模式,因此您要執行的操作符合標准。 但是,我發現使用Ember Data可以更容易地使用平面URL結構,盡管有一種解決方法。

您需要查看ember-data-url-templates插件,並將其中的一些邏輯添加到模型的適配器中。

有了該插件,您可以使用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 });
    }
  }
});

除非該插件允許解決該問題,否則我相信這會將您鎖定在該URL結構中,以在整個應用程序中進行評論。 因此,在決定沿這條路線走之前,一定要權衡一下這一權衡。

是的,這可行,應按以下步驟進行設置

模型/ client.js

export default DS.Model.extend({
    name: DS.attr('string'),
    telno: DS.attr('string'),

    campaigns: hasMany()
});

模型/ client.js

export default DS.Model.extend({
    name: DS.attr('string'),
    startdate: DS.attr('date'),
    enddate: DS.attr('date'),

    client: DS.belongsTo('client')
});

/templates/client/edit.bhs

  <table class="table table-bordered table-striped table-condensed"> <thead> <tr> <th></th> <th>Name</th> </tr> </thead> <tbody> {{#each model.campaigns as |campaign|}} <tr> <td>{{campaign.name}}</td> </tr> {{/each}} </tbody> </table> 

http:// localhost:3000 / api / clients / 1

{
{
  "links": {
    "self": "http://localhost:3000/api/clients/1"
  },
  "data": {
    "type": "clients",
    "relationships": {
      "campaigns": {
        "links": {
          "related": "http://localhost:3000/api/clients/1/campaigns"
        }
      }
    },
    "id": "1",
    "attributes": {
      "name": "Test",
      "telno": "123"
    },
    "links": {
      "self": "http://localhost:3000/api/clients/1"
    }
  }
}

http:// localhost:3000 / api / clients / 1 / campaigns

{
  "links": {
    "self": "http://localhost:3000/api/clients/1/campaigns"
  },
  "data": [
    {
      "type": "campaigns",
      "relationships": {
        "client": {
          "links": {
            "related": "http://localhost:3000/api/campaigns/1/client"
          }
        }
      },
      "id": "1",
      "attributes": {
        "enddate": "2019-01-01T00:00:00.000Z",
        "name": "test",
        "startdate": null
      },
      "links": {
        "self": "http://localhost:3000/api/campaigns/1"
      }
    }
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM