normalizeResponse 无法识别嵌套关系之间的 link

normalizeResponse not recognizing link between nested relationships

我正在使用的 API 端点正在返回其中具有多个嵌套关系的数据,我在 DS.JSONAPISerializer 中使用 normalizeResponse() 将其按摩成某种东西完全 JSON-API 合规。

ember 检查器显示所有数据都正确放置在其各自的容器中。顶级模型及其 hasMany 子模型之间的 link 确实有效,但嵌套模型之间的 link 无效。我通过在检查器中导航到嵌套模型的子模型、单击它并观察其 'content' 属性 为空来验证这一点。

首先,看看我的模型是如何设置的:

// models/search.js
// i am able to browse from the search model to children with success
export default DS.Model.extend({
  articles: DS.hasMany('article'),
});

// models/article.js
// i CANNOT browse from an article down to its digest in ember inspector
export default DS.Model.extend({
  search: DS.belongsTo('search'),
  type: DS.attr(),
  created: DS.attr(),
  updated: DS.attr(),
  digest: DS.belongsTo('digest'),
});

// models/digest.js
export default DS.Model.extend({
  title: DS.attr(),
  desc: DS.attr(),
  date: DS.attr(),
  article: DS.belongsTo('article'),
});

现在,这是我在 normalizeResponse 中的函数完成后修改的 JSON。从 normalizeResponse 返回此数据后,父 "relationships" 对象下的 "digest" 对象消失。我的 JSON 有问题吗?我已经尝试了很多这种排列但没有成功,我很确定这符合 JSON-API 规范 Compound Documents.

{"data":{
  "type":"searches",
  "id":"17482738723",
  "attributes":{

  },
  "relationships":{
    "articles":{
      "data":[
        {
          "type":"articles",
          "id":"19988"
        },
        {
          "type":"articles",
          "id":"19989"
        },
      ]
    },
    "digest":{
      "data":[
        {
          "type":"digest",
          "id":"19988_digest"
        },
        {
          "type":"digest",
          "id":"19989_digest"
        },
      ]
    }
  }
},
"included":[
  {
    "id":"19988",
    "type":"articles",
    "attributes":{
      "type": "internal",
      "created":"2016-09-27T00:13:11.000Z",
      "updated":"2016-09-27T00:13:11.000Z",
    }
  },
  {
    "id":"19988_digest",
    "type":"digest",
    "attributes":{
      "title":null,
      "desc":"four five six",
    }
  },
  {
    "id":"19989",
    "type":"articles",
    "attributes":{
      "type": "internal",
      "created":"2016-09-27T00:13:11.000Z",
      "updated":"2016-09-27T00:13:11.000Z",
    }
  },
  {
    "id":"19989_digest",
    "type":"digest",
    "attributes":{
      "title":"one two three",
      "desc":null,
    }
  },
]
}

您的回复表明以下关系模型:

// models/search.js
export default DS.Model.extend({
  articles: DS.hasMany('article'),
  dignists: DS.hasMany('digest'),
});

// models/article.js
export default DS.Model.extend({
  search: DS.belongsTo('search'),
});

// models/digest.js
export default DS.Model.extend({
  search: DS.belongsTo('search'),
});

所以你必须修改你的回复:

  • 删除 search
  • 上的 digest 关系
  • 为每个 article
  • 添加一个 digest 关系

所以你会以这样的方式结束:

{
  "data":{
    "type":"searches",
    "id":"17482738723",
    "attributes":{
    },
    "relationships":{
      "articles":{
        "data":[
          {
            "type":"articles",
            "id":"19988"
          },
          {
            "type":"articles",
            "id":"19989"
          },
        ]
      }
    }
  },
  "included":[
    {
      "id":"19988",
      "type":"articles",
      "attributes":{
        "type": "internal",
        "created":"2016-09-27T00:13:11.000Z",
        "updated":"2016-09-27T00:13:11.000Z",
      },
      "relationships":{
        "digest": {
          "data": {
            "type":"digest",
            "id":"19988_digest"
          }
        }
      }
    },
    {
      "id":"19988_digest",
      "type":"digest",
      "attributes":{
        "title":null,
        "desc":"four five six",
      },
      "relationships":{
        "digest":{
          "data": {
            "type":"digest",
            "id":"19989_digest"
          }
        }
      }
    },
    {
      "id":"19989",
      "type":"articles",
      "attributes":{
        "type": "internal",
        "created":"2016-09-27T00:13:11.000Z",
        "updated":"2016-09-27T00:13:11.000Z",
      }
    },
    {
      "id":"19989_digest",
      "type":"digest",
      "attributes":{
        "title":"one two three",
        "desc":null,
      }
    },
  ]
}

知道你也可以反过来做,在 digest 上指定 articleember-data 将自动保持一切同步。为了清楚起见,我个人更喜欢指定关系的双方。