3

ApplicationAdapter在我的应用程序中,我有一个ajaxError自定义方法。在该方法中,我希望能够过渡到给定的 route。我怎样才能做到这一点?

App.ApplicationAdapter = DS.RESTAdapter.extend({
    ajaxError: function(jqXHR) {
        var error = this._super(jqXHR);
        if (jqXHR) {
            switch(jqXHR.status) {
            // [...]
            case 401:
                // How can I transitionTo('login') here?
            }
            // [...]
        }
    }
});
4

2 回答 2

9

恕我直言,您可以返回一个实例并在当前路由Error的操作中处理它,而不是在适配器中进行转换,这不是一个好的做法:error

App.UnauthorizedError // create a custom Error class

App.ApplicationAdapter = DS.RESTAdapter.extend({
    ajaxError: function(jqXHR) {        
        var defaultAjaxError = this._super(jqXHR);
        if (jqXHR) {
            switch(jqXHR.status) {            
                case 401:
                return new App.UnauthorizedError()
            }
        }
        return defaultAjaxError;
    }
});

App.IndexRoute = Ember.Route.extend({
  model: function() {
      return this.store.find('person');
  },
  actions: {
      error: function(reason) {
          // all errors will be propagated to here, we check the instance to handle the UnauthorizedError          
          if (reason instanceof App.UnauthorizedError) {
              this.transitionTo('login')
          }
      }
  }  
});

如果您想将其用于所有路由,您可以将未经授权的转换放在ApplicationRoute. 因为 ApplicationRoute 是所有路由的父级,未处理的操作或返回 true 的操作将冒泡到父级路由。

App.ApplicationRoute = Ember.Route.extend({
    actions: {
      error: function(reason) {
          if (reason instanceof App.UnauthorizedError) {
              this.transitionTo('login')
          }
      }
  }
});

App.BarRoute = Ember.Route.extend({
    actions: {
        error: function(reason) {
            // handle errors of bar route

            // bubble to application route
            return true;
        }
    }
});

这是这个示例的小提琴http://jsfiddle.net/SkCH5/

于 2013-11-09T01:31:48.880 回答
0

抛出错误并允许路径上的错误钩子捕获它并从那里转换。此外,您可以使用此逻辑创建一个 mixin,并将该 mixin 添加到您的所有路由中。

Machty 在他的要点中有关于新路由器的其他信息:https ://gist.github.com/machty/5647589

App.AuthenticatedRoute = Ember.Route.extend({
 beforeModel: function(transition) {
  if (!authTokenPresent) { 
   return RSVP.reject();
   // Could also just throw an error here too...
   // it'll do the same thing as returning a rejecting promise.

   // Note that we could put the redirecting `transitionTo`
   // in here, but it's a better pattern to put this logic
   // into `error` so that errors with resolving the model
   // (say, the server tells us the auth token expired)
   // can also get handled by the same redirect-to-login logic.
  }
 },

 error: function(reason, transition) {
  // This hook will be called for any errors / rejected promises
  // from any of the other hooks or provided transitionTo promises.

  // Redirect to `login` but save the attempted Transition
  var loginController = this.controllerFor('login')
  loginController.set('afterLoginTransition', transition);
  this.transitionTo('login');
 }
});
于 2013-11-08T22:24:32.633 回答