2016-03-20 12 views
0

後は角度ルート解像度 - deferred.reject働いていない - アンギュラ1 +活字体

function routes($routeProvider: ng.route.IRouteProvider) { 

     let accessResolver = ['UserFactory', (UserFactory: any) => { 
      return UserFactory.isAuthenticated(); 
     }]; 

     //Configuring routes 
     $routeProvider.when('/', { 
      templateUrl: '/views/home.html', 
      controller: 'HomeController', 
      controllerAs: 'homeCtrl', 
      resolve: accessResolver 
     }).when('/login', { 
      templateUrl: '/views/login.html', 
      controller: 'LoginController', 
      controllerAs: 'loginCtrl' 
     }).otherwise({ 
      redirectTo: '/' 
     }); 
    } 

そして、私のルート変更のエラーハンドラ

function run($rootScope: ng.IRootScopeService, $location: ng.ILocationService) { 
     $rootScope.$on("$routeChangeError",() => { 
      console.log("Route Change Error"); 
      $location.url('/login?redirect=' + $location.url()); 
     }); 
    } 

そしてUserFactory

module TheHub { 
    export interface IUserFactory { 
     isAuthenticated(): ng.IDeferred<String>; 
    } 

    class UserFactory implements IUserFactory { 

     constructor(private $http: ng.IHttpService, private $q: ng.IQService, private $rootScope: any) { 
     } 

     isAuthenticated(): ng.IDeferred<String> { 
      let deferred = this.$q.defer(); 
      if (this.$rootScope.auth && this.$rootScope.auth.isAuthenticationChecked) { 
       if (this.$rootScope.auth.isAuthenticated) { 
        deferred.resolve('OK'); 
       } else { 
        deferred.reject('Unauthorized'); 
       } 
      } else { 
       this.$http.get('secure/user').then(
        (response: ng.IHttpPromiseCallbackArg<{}>) => { 
         if (!this.$rootScope.auth) { 
          this.$rootScope.auth = {}; 
         } 
         this.$rootScope.auth.isAuthenticationChecked = true; 
         this.$rootScope.auth.isAuthenticated = true; 
         deferred.resolve('OK'); 
        }, 
        (error: any) => { 
         if (!this.$rootScope.auth) { 
          this.$rootScope.auth = {}; 
         } 
         this.$rootScope.auth.isAuthenticationChecked = true; 
         deferred.reject('Unauthorized'); 
        }); 
      } 
      return deferred; 
     } 
    } 

    function userFactory($http: ng.IHttpService, $q: ng.IQService, $rootScope: any) { 
     return new UserFactory($http, $q, $rootScope); 
    } 

    userFactory.$inject = ['$http', '$q', '$rootScope']; 

    angular.module('TheHub').factory('UserFactory', userFactory); 
} 
私のルートの設定です

ここでのロジックは、ユーザーが既にログインしてセッションを持っているかどうかを確認するリクエストです。 問題は、ユーザーが既にログインしていない場合、サービスが失敗しており、約束が拒否されていることです。しかし、なぜハンドラ$ routeChangeErrorが起動されていないのかわかりません。 JavaScriptエラーが発生した場合は正常に動作しています。

+2

あなたはdeferred.promise'返す '使用する必要があると思いますが、あなたが本当に完全に[繰延アンチパターンを避ける]すべきである(http://stackoverflow.com/ q/23803743/1048572) – Bergi

+0

ありがとうございました..それは愚かな間違いでした..あなたは答えとしてこれを投稿できるので、解決した印を付けることができます。 – Pavan

答えて

1

.promiseを忘れてしまい、延期されていないものだけが返されるようになりました。これは待たず、期待した解決値でもありませんでした。

しかし、あなたはavoid the deferred antipattern anyway必要があります - ちょうど行う

isAuthenticated(): ng.IPromise<String> { 
    if (this.$rootScope.auth && this.$rootScope.auth.isAuthenticationChecked) { 
     if (this.$rootScope.auth.isAuthenticated) { 
      return this.$q.resolve('OK'); 
//   ^^^^^^^^^^^^^^^^^^^^^^ 
     } else { 
      return this.$q.reject('Unauthorized'); 
//   ^^^^^^^^^^^^^^^^^^^^^^ 
     } 
    } else { 
     return this.$http.get('secure/user').then(
//  ^^^^^^ 
      (response: ng.IHttpPromiseCallbackArg<{}>) => { 
       if (!this.$rootScope.auth) { 
        this.$rootScope.auth = {}; 
       } 
       this.$rootScope.auth.isAuthenticationChecked = true; 
       this.$rootScope.auth.isAuthenticated = true; 
       return 'OK'; 
//    ^^^^^^ 
      }, 
      (error: any) => { 
       if (!this.$rootScope.auth) { 
        this.$rootScope.auth = {}; 
       } 
       this.$rootScope.auth.isAuthenticationChecked = true; 
       return this.$q.reject('Unauthorized'); 
//    ^^^^^^^^^^^^^^^^^^^^^ 
      } 
     ); 
    } 
} 
+1

「ng.IDeferred 」も同様に修正する必要があります。 – estus

+0

@estus:ああ、右、私はそれがtypescriptであることを忘れました。 'ng.IPromise 'が正しいタイプであるかどうか知っていますか? – Bergi

+1

私は頻繁にタイピングをしませんが、はい、私はそれが正しいタイプであることが肯定的です。また、 'throw 'は$ qの' $ q.reject() 'と互換性がありません。前者も例外ハンドラを起動しますが、未処理のアプリエラーを「スロー」することはありません。 – estus

関連する問題