如何在没有关系id的情况下将关系序列化为JSON-API?

时间:2015-11-04 09:01:09

标签: ember.js ember-data json-api

我连接的API并没有使用JSON-API作为其响应格式,因此我不得不为我的Ember应用程序序列化响应。

我遇到的问题是我对API的回答如下:

{
    @type: "application/x.app-name.nurse-collection+json",
    @type.item: "application/x.app-name.nurse+json",
    @type.collection: "application/x.app-name.nurse-collection+json",
    @href: "http://192.168.33.10:3000/api/nurses{?includePatients}",
    @item: "http://192.168.33.10:3000/api/nurses/{id}",
    items: [
    {
        @type: "application/x.app-name.nurse+json",
        @type.item: "application/x.app-name.nurse+json",
        @type.collection: "application/x.app-name.nurse-collection+json",
        @href: "http://192.168.33.10:3000/api/nurses/1{?includePatients}",
        id: 1,
        Nurse: "Maria Holmes",
        NurseTelephoneNo: "123 4567",
        NurseMobileNo: "0123345566",
        Email: "someone@example.com",
        Patients: {
            @type: "application/x.app-name.patient-collection+json",
            @href: "http://192.168.33.10:3000/api/nurses/1/patients{?name,gender,includeNurse,includeEvents,includeReferrals,includeReviews,includePanels,includeRetrospectives,includePhbs,includeDisputeStage2s,includeDisputeResolutionPanels,includeEthnicity,includeFirstLanguage,includeMaritalStatus,includeReligion,includeSecurityLevel,includeSexualOrientation,includeWhichReview}",
            @rel: "http://192.168.33.10:3000/api/nurses/1/patients{?name,gender,includeNurse,includeEvents,includeReferrals,includeReviews,includePanels,includeRetrospectives,includePhbs,includeDisputeStage2s,includeDisputeResolutionPanels,includeEthnicity,includeFirstLanguage,includeMaritalStatus,includeReligion,includeSecurityLevel,includeSexualOrientation,includeWhichReview}"
            }
        },
        {
        @type: "application/x.app-name.nurse+json",
        @type.item: "application/x.app-name.nurse+json",
        @type.collection: "application/x.app-name.nurse-collection+json",
        @href: "http://192.168.33.10:3000/api/nurses/2{?includePatients}",
        id: 2,
        Nurse: "Julie Smart",
        NurseTelephoneNo: "543 1234",
        NurseMobileNo: null,
        Email: "someone@example.com",
        Patients: {
            @type: "application/x.app-name.patient-collection+json",
            @href: "http://192.168.33.10:3000/api/nurses/2/patients{?name,gender,includeNurse,includeEvents,includeReferrals,includeReviews,includePanels,includeRetrospectives,includePhbs,includeDisputeStage2s,includeDisputeResolutionPanels,includeEthnicity,includeFirstLanguage,includeMaritalStatus,includeReligion,includeSecurityLevel,includeSexualOrientation,includeWhichReview}",
            @rel: "http://192.168.33.10:3000/api/nurses/2/patients{?name,gender,includeNurse,includeEvents,includeReferrals,includeReviews,includePanels,includeRetrospectives,includePhbs,includeDisputeStage2s,includeDisputeResolutionPanels,includeEthnicity,includeFirstLanguage,includeMaritalStatus,includeReligion,includeSecurityLevel,includeSexualOrientation,includeWhichReview}"
        }
    }, 

    ...

    ]
}

因此,在此示例中,patients可能是一个集合,但在转到该端点之前,我没有hasMany关系的id。

我想知道是否有办法以符合JSON-API的方式对其进行序列化,但是一旦我的应用决定转到patients,我就会异步加载关系数据端点。

1 个答案:

答案 0 :(得分:0)

我会编写一个适合此响应类型的自定义序列化程序,而不是将其转换为JSON-API规范。

这里看起来没有关于患者记录的任何信息(即ID列表),因此请忽略它,除非您要为{设置link属性{1}}。

异步加载关系数据可以很好地处理ember-data。当您获得一系列hasMany条记录并将其添加到商店时,只要加载的记录都有patient,就可以使用nurse.get('patients')