如何使用JSON API属性覆盖主键 [英] How to override the primary key with a JSON API attribute

查看:66
本文介绍了如何使用JSON API属性覆盖主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"Membership"的模型,该模型具有一个字符串属性"inviteToken",我希望将其用作主键.

I've got a model called "Membership" that has a string attribute "inviteToken" which I would like to use as my primary key.

我创建了以下序列化器,但无法使其从JSON中提取主键.

I've created the following serializer, but cannot get it to pick up the primary key from the JSON.

app/serializers/membership.js:

app/serializers/membership.js:

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
  primaryKey: 'invite-token' // also tried 'inviteToken'
});

我得到的具体错误是:

处理路线时发生错误:邀请信.显示断言失败:您在传递给"push"的对象中必须包含"id"作为成员身份
错误:断言失败:您必须在会员身份中添加"id"作为会员资格对象传递给推送"

Error while processing route: invitations.show Assertion Failed: You must include an 'id' for membership in an object passed to 'push'
Error: Assertion Failed: You must include an 'id' for membership in an object passed to 'push'

当我尝试通过路线中的ID来获取记录时,会发生以下情况:

Which happens when I try to get a record by its ID in the route:

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.store.find('membership', params.token);
  }
});

API响应:

{
  "jsonapi":{
    "version":"1.0"
  },
  "data":{
    "type":"membership",
    "id":"30",
    "attributes":{
      "invite-token":"5bGo7IhZh93E4SB07VWauw"
    }
  }
}

奇怪的是,如果我使用"type"作为主键,我会在ember检查器中看到"membership"作为id.好像余烬数据不知道如何使用属性"中的内容.我正在使用余烬数据2.4.0.

The strange thing is that if I use "type" as the primary key, I see "membership" as the id in the ember inspector. It's as if ember data doesn't know how to use something from the "attributes". I'm using ember data 2.4.0.

更新

我可以这样做,以使其在我的序列化程序中起作用:

I can hack this to work in my serializer by doing this:

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
  normalize: function(type, hash) {
    const json =  this._super(type, hash);
    json.data.id = json.data.attributes.inviteToken;

    return json;
  }
});

推荐答案

序列化程序期望 primaryKey 的值引用json中的顶级元素.这就是为什么"type"和"id"有效的原因.它当前不支持嵌套属性(例如primaryKey:"attributes.invite-token")

The serializer expects the value of primaryKey to refer to a top level element in the json. This is why "type" and "id" works. It currently does not support nested properties (for example primaryKey: "attributes.invite-token")

不过,有两种不错的解决方法:

However there are two good workarounds:

首先是重写extractId方法.默认实现非常简单.在您的情况下,您可以执行以下操作:

The first is overriding the extractId method. The default implementation is quite simple. In your case you could do something like:

extractId(modelClass, resourceHash) {
    var id = resourceHash['attributes']['invite-key';
    return coerceId(id);
  },

第二种方法是您发现的方法,这是一种蛮力的方法,那就是在normalize函数中手动分配id.

The second way is the method you discovered, a more brute force approach, and that is to assign the id manually in the normalize function.

这篇关于如何使用JSON API属性覆盖主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆