2015-07-10 45 views
0

除了我的最後一個問題ember.js JSONAPIAdapter with hasMany一個同事問,如果「種-的」在工作 JSON -sideloaded關係:API結構可以嵌入這樣的:EmberJS JSONAPIAdapter用的hasMany +嵌入式關係

{ 
    "data": [ 
     { 
     "type": "altersgruppe", 
     "id": "1", 
     "attributes": { 
      "name": "UNTER_21" 
     }, 
     "relationships": { 
      "tarifbeitraege": { 
      "data": [ 
       { 
       "type": "tarifbeitrag", 
       "id": "3", 
       "attributes": { 
        "name": "ZAHN70", 
        "beitrag": "3-29,70", 
        "proergaenzung": "7,00", 
        "gesamtbeitrag": "25.99" 
       } 
       }, 
       { 
       "type": "tarifbeitrag", 
       "id": "4", 
       "attributes": { 
        "name": "ZAHN90", 
        "beitrag": "4-28,70", 
        "proergaenzung": "7,00", 
        "gesamtbeitrag": "30.99" 
       } 
       } 
      ] 
      } 
     } 
     }, 
     { 
     "type": "altersgruppe", 
     "id": "2", 
     "attributes": { 
      "name": "ALTER_21_24" 
     }, 
     "relationships":{ 
      "tarifbeitraege": { 
      "data": [ 
       { 
       "type": "tarifbeitrag", 
       "id": "1", 
       "attributes": { 
        "name": "ZAHN70", 
        "beitrag": "1-25,70", 
        "proergaenzung": "7,00", 
        "gesamtbeitrag": "25.99" 
       } 
       }, 
       { 
       "type": "tarifbeitrag", 
       "id": "2", 
       "attributes": { 
        "name": "ZAHN90", 
        "beitrag": "2-25,70", 
        "proergaenzung": "7,00", 
        "gesamtbeitrag": "25.99" 
       } 
       }] 
      } 
     } 
     } 
    ] 
} 

背後的想法:我們可以在java後端使用較少問題的關係(sideloaded結構難以實現)。

但是上面的JSON結構不起作用。該商店僅包含第一級數據,即「altersgruppe」,但「tarifbeitraege」爲空。

回答

1

這種類型的文檔在JSON:API規範中被稱爲compound document

複合文檔的「關係」部分應該只有關係 - 個別對象應該是resource identifier objects。把屬性放在那裏是行不通的,因爲那不是它們應該在的地方。

取而代之的是,完整的對象被加載到頂級「包含」部分中。因此,你的反應應該看起來可能是這樣的:

{ 
    "data": [ 
     { 
     "type": "altersgruppe", 
     "id": "1", 
     "attributes": { 
      "name": "UNTER_21" 
     }, 
     "relationships": { 
      "tarifbeitraege": { 
      "data": [ 
       { "type": "tarifbeitrag", "id": "3" }, 
       { "type": "tarifbeitrag", "id": "4" } 
      ] 
      } 
     } 
     }, 
     { 
     "type": "altersgruppe", 
     "id": "2", 
     "attributes": { 
      "name": "ALTER_21_24" 
     }, 
     "relationships":{ 
      "tarifbeitraege": { 
      "data": [ 
       { "type": "tarifbeitrag", "id": "1" }, 
       { "type": "tarifbeitrag", "id": "2" } 
       ] 
      } 
     } 
     } 
    ], 
    "included": [ 
     { 
     "type": "tarifbeitrag", 
     "id": "3", 
     "attributes": { 
      "name": "ZAHN70", 
      "beitrag": "3-29,70", 
      "proergaenzung": "7,00", 
      "gesamtbeitrag": "25.99" 
     } 
     }, 
     { 
     "type": "tarifbeitrag", 
     "id": "4", 
     "attributes": { 
      "name": "ZAHN90", 
      "beitrag": "4-28,70", 
      "proergaenzung": "7,00", 
      "gesamtbeitrag": "30.99" 
     } 
     }, 
     { 
     "type": "tarifbeitrag", 
     "id": "1", 
     "attributes": { 
      "name": "ZAHN70", 
      "beitrag": "1-25,70", 
      "proergaenzung": "7,00", 
      "gesamtbeitrag": "25.99" 
     } 
     }, 
     { 
     "type": "tarifbeitrag", 
     "id": "2", 
     "attributes": { 
      "name": "ZAHN90", 
      "beitrag": "2-25,70", 
      "proergaenzung": "7,00", 
      "gesamtbeitrag": "25.99" 
     } 
     } 
    ] 
} 

還有的http://jsonapi.org主頁上的例子表明,包括側負載,以及一個在描述compound documents說明書的部分例子。