2014-09-05 143 views
0

我想在Ember中嵌套资源,但要能够使用短URL访问它们。嵌套的资源和路径

例如:mysite.com/admin将打开路径:/路由/型材/设置/管理

是否有可能做这样的事情 - 采用Ember? 我目前使用Ember 1.7和Ember App Kit。

我尝试以下,但它不工作:

var Router = Ember.Router.extend(); 

Router.map(function() { 
this.resource('profile', function() { 
    this.resource('profile.settings', { path: '/settings' }, function() { 
     this.resource('profile.settings.admin', { path: '/admin' }); 
    ); 
}); 

感谢。

+0

如果您要嵌套,则不必重复父路由名称。你的嵌套资源'profile.settings'应该只是'settings',对于'profile.settings.admin'来说,应该只是'admin'。 – MilkyWayJoe 2014-09-05 19:49:47

+1

@MilkyWayJoe如果他想让路线名称加前缀,则不行。他要么嵌套'this.route()'调用,要么他必须自己添加前缀。建议使用前缀,这样名称冲突就不会出现很多路由。 – GJK 2014-09-05 19:54:14

回答

2

您的代码不起作用,因为您最内部的资源继承了最外部资源的/profile路径和中间资源的/settings。如果你希望它是只是普通/admin,你必须做这样的事情:

this.resource('profile', { path: '' }, function() { 
    this.resource('profile.settings', { path: '' }, function() { 
     this.resource('profile.settings.admin', { path: '/admin' }); 
    }); 
}); 

然而,这是怎么回事,当你有更多的路线,每个想顶层路径越晦涩。您可能会发现,只需在顶层声明admin路由,然后使用路由中的redirect挂接即可重定向。

+0

这适用于在根级别只有一个空路径的资源。如果你有多个,它不能正常工作。 – alexmngn 2014-09-08 14:23:07

+0

这就是为什么我提到你最好从顶级路线重定向。这比这更明确。 – GJK 2014-09-08 16:17:57