support for relationship links when no data exists
It appears that I may have been mis-understanding/underestimating the value of links provided by the JSON API-spec. I'm using Ember on the front-end can use the related link of a relationship to asynchronously load relationship data when needed. For example, if I have a customer model with a pets relationship and that pets relationship contains ONLY a links attribute, Ember Data will use the related attribute of links to automatically load that data. This allows for much more efficient loading of data, on-demand.
However, with the mapper, we currently require relationship data in order to build said relationships.
My proposal is a method for passing a list of relationships to the mapper. These relationships can than be cross-referenced against the relationships passed along with the Bookshelf model. If there is no relationship in the model, the mapper would simply build the appropriate link. Otherwise, if there is data, the mapper would build out the relationship as normal.
Thoughts on this?
As a follow-up, it seems that this is something the jsonapi-serializer can handle - the ignoreRelationshipData option needs to be passed as an option so that the data element is not included.
@chamini2 any thoughts on this one? Something I need to get implemented at some point.
I've been thinking about it and I'm not sure how we would go about implementing it, since we build the links with information from the relations. The proposal of passing a list of relationships would work like an option to say build the links for these relations if you don't find it?
The proposal of passing a list of relationships would work like an option to say build the links for these relations if you don't find it?
Exactly. This becomes a true advantage when lazy-loading data on the frontend. Ember Data will do this by default, which prevents having to include everything or hit the database just to return a list of relationship ids.
I had to implement a workaround for this. I'm running the equivalent of this code:
options.relations.fields.forEach(function (relation) {
if (model.relations && !model.relations[relation]) {
// Simulating an empty collection in json-mapper
model.relations[relation] = {toJSON: function toJSON() { return []}};
}
}
mapper.map(model, type, options)
Hope this makes sense - I'm processing the relations.fields array passed as an option to the mapper, and for each relation if it's not already in the model.relations object, I create a fake empty collection. The toJSON function is all that's needed for the map function to work.
However, this results in an empty data: [] which I'd like to also get rid of. As far as I can tell, getting rid of this definitely requires changes to jsonapi-mapper.