3

私のアプリケーションにはApplicationAdapterajaxErrorメソッドがカスタマイズされた があります。そのメソッド内で、特定の 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 の Gist に、新しいルーターに関する追加情報があります: 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 に答える