4

我定义了以下路线:

SettingsApp.Router.map(function () {
    ....
    this.resource('profile', function () {
        this.route('user'),
        this.route('company')
    });

});

SettingsApp.ProfileRoute = Ember.Route.extend({
    redirect: function () {
        this.transitionTo('profile.user');
    },
    model: function () {
        return Ember.A([
            Ember.Object.create({title:"User", link:"#/profile/user"}),
            Ember.Object.create({title:"Company", link:"#/profile/company"})
        ]);
    }
})

#/profile正如预期的那样重定向到#/profile/user问题是#/profile/company重定向到#/profile/user. 每当我访问该资源下方的任何网址时,似乎都会遵循资源重定向。

为什么是这样?我怎样才能重定向顶层#/profile

4

2 回答 2

3

您可以将您的路线移动redirect()profile资源的index路线,ProfileIndexRoute这将导致它仅在您浏览#/profile并允许您访问#/profile/user并且#/profile/company没有问题时触发:

SettingsApp.Router.map(function () {
    this.resource('profile', function () {
        this.route('user');
        this.route('company');
    });

});

SettingsApp.ProfileRoute = Ember.Route.extend({
    //redirect: function () {
    //    this.transitionTo('profile.user');
    //},
    model: function () {
        return Ember.A([
            Ember.Object.create({title:"User", link:"#/profile/user"}),
            Ember.Object.create({title:"Company", link:"#/profile/company"})
        ]);
    }
});


SettingsApp.ProfileIndexRoute = Ember.Route.extend({
  redirect: function () {
        this.transitionTo('profile.user');
    },
});

示例 JSBin

提示:设置LOG_TRANSITIONS: true您的Application,以显示您正在通过路由器访问哪些路由。这对于调试此类问题非常有帮助。

SettingsApp = Ember.Application.create({
  LOG_TRANSITIONS: true
});
于 2013-06-20T17:33:33.770 回答
1

redirectin your是硬重定向,这ProfileRoute意味着无论您拥有什么嵌套路由/资源,它都将始终重定向到profile.user. 要具有不同的行为,您应该删除redirect钩子并提供父模板中的链接或导航到您的子资源,例如

{{#linkTo profile.user}}User{{/linkTo}}
{{#linkTo profile.company}}Company{{/linkTo}}

这将生成以下 HTML 标记:

<a href="/user">User</a>
<a href="/company">Company</a>

如果您想在路由转换中传递模型,您可以在模板设置中执行以下操作:

{{#linkTo profile.user user}}User{{/linkTo}}
{{#linkTo profile.company company}}Company{{/linkTo}}

如果您确实传递了模型,则需要相应地更改路由器映射:

SettingsApp.Router.map(function () {
  ....
  this.resource('profile', function () {
    this.route('user', { path: "/user/:user_id" }),
    this.route('company', { path: "/company/:company_id" })
  });
});

这将生成以下 HTML 标记:

<a href="/profile/user/1">User</a>
<a href="/profile/company/1">Company</a>

最后,如果您重定向到profile.user或者profile.company您还需要此路线的模板。

希望能帮助到你。

于 2013-06-20T09:48:41.003 回答