EmberJS JSONAPIAdapter with hasMany +嵌入式关系



除了我上一个问题ember.js JSONAPIAdapter with has许多同事问是否"kind-of -side - loaded"的关系在工作中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"为空。

这种类型的文档在JSON:API规范中被称为复合文档。

复合文档的"关系"部分应该只包含关系——其中的单个对象应该是资源标识符对象。把属性放在那里不起作用,因为那不是它们应该在的地方。

相反,完整的对象是在顶层的"included"部分中侧加载的。因此,您的响应应该看起来更像这样:

{
    "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的主页上有一个示例,其中显示了一个包含侧加载的示例,以及描述复合文档的规范部分中的一个示例。

最新更新