Ember.JS中嵌套资源的路由命名

Naming of Routes for nested resources in Ember.JS

本文关键字:路由 资源 JS 嵌套 Ember      更新时间:2023-09-26

下面的路由设置来自Ember.JS文档(http://emberjs.com/guides/routing/defining-your-routes/),我必须处理一个等效的问题:

App.Router.map(function() {
    this.resource('post', { path: '/post/:post_id' }, function() {
        this.route('edit');
        this.resource('comments', function() {
            this.route('new');
        });
    });
});

根据文档和我自己的经验,结果如下:

/post/:post_id/comments -> App.CommentsIndexRoute

但是,因为我想要一个特定于post的注释路由,所以我希望

/post/:post_id/comments -> App.PostsCommentsRoute

我的错误到底是什么,我需要改变什么来实现我的目标。

只有route与父resource同名。如果你想让它像PostsCommentsRoute一样出现,它会更像这样(注意,我把它复数化以匹配你的例子,尽管url没有被复数化)

App.Router.map(function() {
    this.resource('posts', { path: '/post/:post_id' }, function() {
        this.route('comments');
    });
});