Template frameworks like Handlebars use the results of the toJSON for the data that is available to them to use. I've found it convenient to include related models in the toJSON response almost similar to the Eager Loading idea ORMs have. This is helpful when rendering say a post, and want to include the author or react based on the type of author for instance.
In the model we just set withJSON with the models to be included:
var Post = Supermodel.Model.extend({
urlRoot:App.apiURL+'posts',
withJSON: ['user', 'image']
});
The toJSON function will both test if the relationship type exists, and if this model has one related. If one exists- its toJSON is called and the result is assigned to the name of the relationship.
Their might be a better option name than withJSON. Feel free to test, I thought this might be nice.
Template frameworks like Handlebars use the results of the
toJSON
for the data that is available to them to use. I've found it convenient to include related models in thetoJSON
response almost similar to the Eager Loading idea ORMs have. This is helpful when rendering say a post, and want to include the author or react based on the type of author for instance.In the model we just set
withJSON
with the models to be included:The toJSON function will both test if the relationship type exists, and if this model has one related. If one exists- its toJSON is called and the result is assigned to the name of the relationship.
Their might be a better option name than withJSON. Feel free to test, I thought this might be nice.