具有hasMany +嵌入式关系的EmberJS JSONAPIAdapter

时间:2015-07-10 07:24:19

标签: json ember.js json-api

除了我的上一个问题ember.js JSONAPIAdapter with hasMany,一位同事询问工作中是否存在“善意”的问题 JSON:API结构可以这样嵌入:

{
    "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后端使用较少问题的关系(侧载结构更难实现)。

但是上面的JSON结构不起作用。商店只包含第一级数据,即“altersgruppe”,但“tarifbeitraege”为空。

1 个答案:

答案 0 :(得分: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的规范部分中的一个示例。

相关问题