嵌套的资源和路径

Nested resources and path

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

我想在Ember中嵌套资源,但可以使用短URL访问它们。

例如:mysite.com/admin将打开路由:/routes/profiles/settings/admin

有可能用Ember做这样的事情吗?我目前正在使用Ember1.7和EmberAppKit。

我尝试了以下操作,但不起作用:

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' });
    );
});

谢谢。

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

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

然而,当你有更多的路由,每个路由都想要顶级路径时,这将变得非常棘手。您可能会发现,只需在顶层声明admin路由,然后使用路由中的重定向挂钩进行重定向会更容易。