简体   繁体   中英

Ember - prevent transition on route error

I am intercepting the error hook on my ember routes. I am doing this on top level and inheriting all my routes from this class.

What I want to do is, when the server returns a 401 Unauthorized response, I want to display a notice saying the user is not authorized, and stay on the same page.

So far I have this:

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

The notice works, but the application still switches to the new template, which has empty content because the user does not have authorization to view it.

I want my application to stay on the same page and not transition to the requested page.

How?

I'm not sure exactly but you can try willTransition :

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

Maybe it'll help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM