Skip to content
This repository was archived by the owner on Apr 22, 2021. It is now read-only.

Conversation

@TheBox193
Copy link
Contributor

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 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.
@TheBox193 TheBox193 closed this Aug 13, 2014
@TheBox193 TheBox193 reopened this Aug 13, 2014
flippyhead added a commit that referenced this pull request Jul 24, 2015
toJSON include related models.
@flippyhead flippyhead merged commit c594c46 into pathable:master Jul 24, 2015
@nachocodoner nachocodoner mentioned this pull request Jul 24, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants