2014-03-03 100 views
1

我有一個角色服務,談到一個寧靜的web服務。角度服務在這種情況下如何鏈接承諾?

REST式服務有以下方法

  • getManifest
  • 登錄
  • serviceMethod1
  • serviceMethod2
  • ...

getManifest返回一個清單結構,並且登錄返回一個認證令牌;兩者都需要進行其他serviceMethodX調用,但是manifest和auth令牌只需要被請求一次,這意味着它們可以被請求第一個serviceMethodX調用的一部分,或者可以在任何調用之前被預取,但不需要再次調用以用於後續調用。

我有以下服務

'use strict'; 

myApp.factory('MyService', function($http, $q, ManifestService){ 

    var manifest = null; 
    var authenticationToken = null; 

    return { 
     getAuthenticationToken: function() { 
      var deferred = $q.defer(); 

      $http.post('/login', authData) 
       .success(function(data, status, headers, config){ 
        authenticationToken = data.authToken; 
        deferred.resolve(data.authToken); 
       }); 
      return deferred.promise; 
    },   
     getManifest: function() { 
      var deferred = $q.defer(); 

      $http.post('/manifest', authData) 
       .success(function(data, status, headers, config){ 
        deferred.resolve(data.manifest); 
        manifest = data.manifest; 
       }); 
      return deferred.promise; 
    }, 
     makeServiceCall: function(url, data) { 
      var deferred = $q.defer(); 
      // use manifest and authtoken here 
      $http.post(url, authData) 
       .success(function(data, status, headers, config){ 
        deferred.resolve(data); 
       }); 
      return deferred.promise; 

     } 

    } 
}); 

我怎麼能保證清單和的authToken makeService功能之前被填充被稱爲或我如何鏈不填充,如果他們的承諾,僅僅是第一次?

感謝

回答

1

你可以有像一個reqsDeferred,保持未決,直到它被解決的服務電話。 logingetManifest每個檢查是否其他解決之前,並解決reqsDeferred時如此:

'use strict'; 
myApp.factory('MyService', function($http, $q, ManifestService){ 

    var manifest = null; 
    var authenticationToken = null; 

    // [1] Create the deferred requirements object 
    reqsDeferred = $q.defer(); 

    return { 
     getAuthenticationToken: function() { 
      var deferred = $q.defer(); 

      $http.post('/login', authData) 
       .success(function(data, status, headers, config){ 
        authenticationToken = data.authToken; 
        deferred.resolve(data.authToken); 

        //[2] Check for the other to be resolved 
        if (manifest !== null) { 
         reqsDeferred.resolve(); 
        } 
       }); 
      return deferred.promise; 
    },   
     getManifest: function() { 
      var deferred = $q.defer(); 

      $http.post('/manifest', authData) 
       .success(function(data, status, headers, config){ 
        deferred.resolve(data.manifest); 
        manifest = data.manifest; 

        //[2] Check for the other to be resolved 
        if (authenticationToken !== null) { 
         reqsDeferred.resolve(); 
        } 
       }); 
      return deferred.promise; 
    }, 
     makeServiceCall: function(url, data) { 
      var deferred = $q.defer(); 

      //[3] Keep the calls pending until the promise is satisfied 
      reqsDeferred.promise.then(function() { 

       // use manifest and authtoken here 
       $http.post(url, authData) 
        .success(function(data, status, headers, config){ 
         deferred.resolve(data); 
        }); 

      }); 

      return deferred.promise; 
     } 

    } 
});