2015-07-10 77 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说明书的部分例子。