mercredi 4 novembre 2015

How to serialize relationships into JSON-API without relationship id's?

The API that I am connecting to isn't using JSON-API for its response format, so I am having to serialize the responses for my Ember app.

The problem I am having is that my response from the API looks like this:

{
    @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://ift.tt/1MGZdiA}",
    @item: "http://ift.tt/1krHGhs}",
    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://ift.tt/1MGZamU}",
        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://ift.tt/1krHHlC}",
            @rel: "http://ift.tt/1krHHlC}"
            }
        },
        {
        @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://ift.tt/1MGZdiE}",
        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://ift.tt/1krHHCa}",
            @rel: "http://ift.tt/1krHHCa}"
        }
    }, 

    ...

    ]
}

So in this example, patients could be a collection, but I don't have the id's for the hasMany relationship until I have gone to that endpoint.

I'm wondering if there is a way to serialize this in a way that is JSON-API compliant, but will let me load relationship data asynchronously, once my app has decided to go to the patients endpoint.




Aucun commentaire:

Enregistrer un commentaire