Reputation: 4067
I have an Rails API which responds with the following JSON:
/users/1
{
"id": 1,
"email": "[email protected]"
}
/users/1?embed=role,store
{
"id": 1,
"email": "[email protected]"
"store": {
"id": 1,
"name": "Apple Store"
},
"role": {
"id": 1,
"name": "Owner"
}
}
Is it possible to decide which relationships to fetch with Ember.js - App.User.find(1)? I've tried App.User.find({id: 1, embed: 'role'}) but it sends the GET request as /users?id=1&embed=role which routes to the index action in the Rails controller.
Is there a better way to handle this? I have full access to modify the API. Sideloading on every API request seems overkill, I would be great if I could decide relationships based on query params.
Upvotes: 0
Views: 152
Reputation: 23322
Is there a better way to handle this?
I guess the best option assuming you are using the RESTAdapter should be to define your side-loading as embedded, for example:
DS.RESTAdapter.map('App.User', {
role: { embedded: 'always' },
store: { embedded: 'always' }
};
This way role
and store
are saved embedded in the same record and thus not resulting in separate GET requests.
The two possible values of embedded are:
always: The child records are embedded when loading, and are saved embedded in the same record.
load: The child records are embedded when loading, but should be saved as standalone records. In order for this to work, the child records must have an ID.
Hope it helps.
Upvotes: 2