Dynamic segment values are only available to the route that the dynamic segment belongs to, not the child routes. I don’t know the full reason why this is so, but I can say that this is how it should be. In order to avoid duplicating code, only one route should have to resolve the dynamic segment. All child routes should just use this.modelFor('post')
to get the model directly and not have to resolve the segment again. So you’re routes should look something like this:
App.PostRoute = Ember.Route.extend({
model: function (param) {
return App.Post.find(param.post_id);
}
});
App.PostIndexRoute = Ember.Route.extend({
model: function () {
return this.modelFor('post');
}
})
App.CommentsRoute = Ember.Route.extend({
model: function () {
return App.Comment.find(this.modelFor('post').get('id'));
}
});
App.CommentsIndexRoute = Ember.Route.extend({
model: function () {
return App.Comment.find(this.modelFor('post').get('id'));
}
});
Also, I have a few unrelated comments on your code that you might find useful.
-
You’re using a pretty old version of Ember-Data (judging by the
find()
calls). You’ll probably want to upgrade to the newer version if possible. -
I don’t know how your data model is setup, but it seems like you’d want to make posts and comments related with relationships. Something like:
App.Post = DS.Model.extend({
comments: DS.hasMany('comment', { async: true })
});
App.Comment = DS.Model.extend({
post: DS.belongsTo('post', { async: true })
});
Then for your model hook, you can just do something like this:
App.CommentsIndexRoute = Ember.Route.extend({
model: function (param) {
return this.modelFor('post').get('comments');
}
});
- You declared the
model
hook for theposts
resource and thecomments
resource. But, you only have to declare the model for a route if you’re using the model in the template. For example, if you don’t have acomments
template declared, or you’re just using it as an outlet, you don’t have to declareApp.CommentsRoute
. Ember will generate one for you. I don’t know your exact layout, but it’s possible that you have extra routes declared. If you wanted to post some more of your code, I might be able to help you eliminate some duplicated code.