【问题标题】:EmberJS JSONAPIAdapter with hasMany + embedded relationshipsEmberJS JSONAPIAdapter 与 hasMany + 嵌入式关系
【发布时间】:2015-08-09 01:17:39
【问题描述】:

除了我的最后一个问题ember.js JSONAPIAdapter with hasMany 之外,一位同事还询问了工作中的“种类”-sideloaded 关系是否 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 结构不起作用。 store 只包含第一级数据,即“altersgruppe”,但“tarifbeitraege”为空。

【问题讨论】:

    标签: json ember.js json-api


    【解决方案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 的规范部分中的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 2013-09-29
      相关资源
      最近更新 更多