角度服务如何在这种情况下链接承诺?

时间:2014-03-03 22:24:52

标签: angularjs angularjs-service

我有一个与一个宁静的网络服务对话的角度服务。

restful服务有以下方法

  • getManifest
  • 登录
  • serviceMethod1
  • serviceMethod2
  • ...

getManifest返回清单结构,登录返回一个身份验证令牌;两者都需要进行其他serviceMethodX调用,但是manifest和auth令牌只需要请求一次,这意味着它们可以被请求作为第一个serviceMethodX调用的一部分,或者可以在任何调用之前被预取,但不需要再次调用以用于后续调用。 / p>

我有以下服务

'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;

        }

    }
});

我怎样才能保证在调用makeService函数之前填充manifest和authToken,或者如果第一次没有填充promises,我如何链接promises?

由于

1 个答案:

答案 0 :(得分: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;
        }

    }
});