pathable / supermodel

Supermodel - Minimal Model Tracking for Backbonejs
http://pathable.github.io/supermodel
MIT License
229 stars 36 forks source link

toJSON include related models. #61

Closed TheBox193 closed 9 years ago

TheBox193 commented 10 years ago

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.