beauby / jsonapi-datastore

JavaScript client-side JSON API data handling made easy.
MIT License
195 stars 40 forks source link

Add included objects to serialized JSON #22

Open mklocek opened 8 years ago

mklocek commented 8 years ago

In one of my apps using jsonapi-datastore I needed a way to include to result of serialize method an array of complete relationship objects, just like in jsonapi specification. For my purposes, I created this: mklocek/jsonapi-datastore@a955efec9ecaba87a0dde0598232d1822022df3a. Including is optional here and looks like that:

var projectAttributes = {
    attributes: ['title'],
    relationships: ['clients', 'managers'],
    included: {
        clients: {
            attributes: ['name']
        },
        managers: {
            attributes: ['name']
        }
    }
};

project.serialize(projectAttributes);
=> # jsonapi json containing 'included' array

Have you thought about such feature?

beauby commented 8 years ago

Hi @mklocek. jsonapi-datastore was intended as a client-side library. Hence, creating payloads with an included property was not in the requirements, as (AFAIK) only a server would have to generate those. Would you mind elaborating on your use-case?

mklocek commented 8 years ago

Hi @beauby! I needed a way to create nested_form-like api requests. That's not the way things are made in jsonapi specification, just my use-case, so I didn't make PR. Just wondering if it could be a good feature for you.