The best way I have found to access nested resources (if you can modify your API) is for your API to return a "links" attribute in the json for comments. For example the json returned will be like:
{
"post": {
"id": 1,
"links": {
"comments": "http://localhost:3000/posts/1/comments"
}
}
}
Now you define the relationship in your ember app and fetch the model in the route:
// models/post.js
import DS from 'ember-data';
export default DS.Model.extend({
comments: DS.hasMany('comments', {async: true});
});
// models/comment.js
import DS from 'ember-data';
export default DS.Model.extend({
post: DS.belongsTo('post', {async: true});
});
// post.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.find('post', params.post_id);
}
});
Now, when in your template when you call
{{#each model.comments as |comment|}}
{{comment.comment_attribute}}
{{/each}}
Ember will make a async request at /posts/:post_id/comments and fetch the comments. If you wish to show a loading icon or something while the request is taking place you can wrap it in
{{#if model.comments}}
{{#each model.comments as |comment|}}
{{comment.comment_attribute}}
{{/each}}
{{else}}
<!-- Something to do while loading -->
{{/if}}
Note :You can also see this documented in
Ember Doc
This findHasMany method is called when ember sees in your template {{model.comments}}
The second solution for your problem would be to customize the adapter while in your route (I dont recommend this and havent really tested it):
//one post route
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.findRecord('post', params.post_id);
}
});
//post/comments route
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
var post_id = this.modelFor('post.show').get('id');
this.store.adapterFor('comment').set('namespace', 'posts/' + post_id );
var comments = this.store.findAll('comment');
// set the namespace again to null
this.store.adapterFor('comment').set('namespace', '');
return comments;
}
});