Ember - 防止路由错误转换

Ember - prevent transition on route error

我正在拦截 ember 路线上的 [​​=12=] 钩子。我在顶层执行此操作并从此 class.

继承我的所有路由

我想做的是,当服务器 returns 一个 401 Unauthorized 响应时,我想显示一个通知说用户没有被授权,并停留在同一页上。

到目前为止我有这个:

Ember.Route = Ember.Route.extend(InfinityRoute, {
  actions: {
    error: function(error){
      if (error.status === 401) {
        this.store.createRecord('notice', {
          message: "You are not authorized to view this content. Sorry man."
        });
        // Some code here ...
      }
    }
  }
});

通知有效,但应用程序仍切换到新模板,该模板内容为空,因为用户无权查看它。

我希望我的应用程序停留在同一页面上,而不是转换到请求的页面。

如何?

我不确定,但你可以试试 willTransition:

willTransition: function(transition) {
  // inspect transition object to see if the error ocurred
}

也许会有帮助:)