2016-09-14 60 views

回答

2

我很困難找到一個直接,可讀的答案,我實際返回的響應。

對於以JSON API的例子,他們有以下JSON:

{ 
    "links": { 
    "self": "http://example.com/articles", 
    "next": "http://example.com/articles?page[offset]=2", 
    "last": "http://example.com/articles?page[offset]=10" 
    }, 
    "data": [{ 
    "type": "articles", 
    "id": "1", 
    "attributes": { 
     "title": "JSON API paints my bikeshed!" 
    }, 
    "relationships": { 
     "author": { 
     "links": { 
      "self": "http://example.com/articles/1/relationships/author", 
      "related": "http://example.com/articles/1/author" 
     }, 
     "data": { "type": "people", "id": "9" } 
     }, 
     "comments": { 
     "links": { 
      "self": "http://example.com/articles/1/relationships/comments", 
      "related": "http://example.com/articles/1/comments" 
     }, 
     "data": [ 
      { "type": "comments", "id": "5" }, 
      { "type": "comments", "id": "12" } 
     ] 
     } 
    }, 
    "links": { 
     "self": "http://example.com/articles/1" 
    } 
    }], 
    "included": [{ 
    "type": "people", 
    "id": "9", 
    "attributes": { 
     "first-name": "Dan", 
     "last-name": "Gebhardt", 
     "twitter": "dgeb" 
    }, 
    "links": { 
     "self": "http://example.com/people/9" 
    } 
    }, { 
    "type": "comments", 
    "id": "5", 
    "attributes": { 
     "body": "First!" 
    }, 
    "relationships": { 
     "author": { 
     "data": { "type": "people", "id": "2" } 
     } 
    }, 
    "links": { 
     "self": "http://example.com/comments/5" 
    } 
    }, { 
    "type": "comments", 
    "id": "12", 
    "attributes": { 
     "body": "I like XML better" 
    }, 
    "relationships": { 
     "author": { 
     "data": { "type": "people", "id": "9" } 
     } 
    }, 
    "links": { 
     "self": "http://example.com/comments/12" 
    } 
    }] 
} 

這條線的位置:

"self": "http://example.com/articles/1/relationships/author"

是一個 「關係鏈接」

這條線的位置:

"self": "http://example.com/articles/1/relationships/comments"

又是一個「關係鏈接」

是的,我知道這是混亂的,因爲另外一個叫related。這些鏈接的目的是什麼?目的是只有管理關係。所以說你做GET /articles/1/relationships/comments你做不是返回評論的信息。你只需要返回一個資源類型/ ID和一些其他的東西,如元數據和鏈接的數組。例如:

{ 
    "data": [{ 
    "type": "comments", 
    "id": "13" 
    }, { 
    "type": "comments", 
    "id": "29" 
    }], 
    "links": { 
    "self": "http://example.com/articles/1/relationships/comments", 
    "next": "http://example.com/articles/1/relationships/comments?page[offset]=2", 
    "last": "http://example.com/articles/1/relationships/comments?page[offset]=4" 
    }, 
    "meta": { 
    "copyright": "Copyright 2015 Example Corp.", 
    "authors": [ 
     "Zach Aysan" 
    ]} 
} 

爲什麼這很有用?因爲有時候我們想只是刪除關係,而不是資源(評論,作者)本身。因此,例如,如果我們執行的DELETE /articles/1/relationships/author不會從用戶表中刪除該用戶,則只會將該用戶作爲作者刪除。要刪除只有一些評論,我們做一個PATCH /articles/1/relationships/comments,只包括我們想保留的評論。但請記住,如果後端認爲這是正確的操作,則可能會刪除實際的評論。 (既然沒有關聯的文章有什麼好評論?)

另一個鏈接呢?爲什麼它是/articles/1/author而不是/people/9?因爲文章的作者可能會在請求之間更改,並且GET /articles/1/author將始終返回當前作者。這就是爲什麼我們通常不需要支持諸如PATCH /articles/1/author之類的東西,因爲它通常會更有效/更安全地引導資源本身的變化。 PATCH /people/9,例如,如果某人在編輯文章頁面上更改了他們的頭像。即使管理員更改了文章的作者身份,PATCH仍然轉到正確的資源。

我知道。這一切都有點乏味,但是一旦理解了Ember Data這樣的東西,就應該將它們組合在一起。