多个方法在angularjs中调用具有cachefactory的相同单个服务

时间:2014-08-24 05:21:02

标签: angularjs

我有类似下面的服务......它总是从服务器获得所有类型的买家。

var buyersService = function ($http, $q,$cacheFactory) {
            var serviceBase = '/api/OMData/';
            var  BuyersFactory = {};
            buyersService.cache = $cacheFactory('cacheId');

            BuyersFactory.GetBuyers = function (type) {
              var buyersDiffer = $q.defer();
              var dataList =  buyersService.cache.get('BuyerData');
              if (dataList != null && dataList.length > 0) {
                  buyersDiffer .resolve(_getBuyerByType(type,dataList));
              }
              else {
                  $http.get(serviceBase + 'GetBuyers').then(
                   function (results) {
                       buyersService.cache.put("BuyerData", results.data);
                       buyersDiffer .resolve(_getBuyerByType(type,results.data));
                   });
              }
              return buyersDiffer .promise;
            }

    app.factory('OMDataService', ['$http', '$q', '$cacheFactory', buyersService]);

});



function _getBuyerByType(Type,dataList) {
        try {

            var typedBuyer= Enumerable.From(dataList).Where(function (x) {
                return x.Type== Type;
            }).ToArray();
            return typedBuyer;
        } catch (e) {
            throw e;
        }
    }


 function getLocalBuyer(){
        return BuyersFactory.GetBuyers(1);
    }

   function getForeignBuyer(){
        return BuyersFactory.GetBuyers(2);
    }

我的公司有两种类型的买家。如localBuyer和foreignBuyer。在我的控制器中,我需要用Q.All一次调用两个服务。当两个服务命中buyerService方法然后系统调用服务器两次。因此,现在的现金是空的。 但我希望系统调用服务器为一次服务调用一次,第二次服务从缓存中获取数据。

编辑:控制器代码

function loadDefaultData() {
    try {
        $q.all([
            OMDataService.getLocalBuyer(),
            OMDataService.getForeignBuyer(),

        ]).then(function (data) {
            $timeout(function () {
                //set dropdown list
                viewData.local= data[0];
                viewData.foreign= data[1];

            }, 0);
        }).catch(function (e) {
            showError(e);
        });

    } catch (e) {
        showError(e);
    }
}

1 个答案:

答案 0 :(得分:0)

我建议定义一个变量来保存$ http返回的承诺。第一次调用服务时,该变量最初是未定义的。它设置为$ http。

返回的承诺

您服务的逻辑因此变为:

  1. if cache,return cache
  2. 如果承诺,返回承诺
  3. 其他呼叫服务器并保存承诺
  4. 我删除了你的延迟变量。它不再需要。我确实在$ q.when()(doc:https://docs.angularjs.org/api/ng/service/ $ q)

    中包装缓存响应
    var buyersService = function ($http, $q, $cacheFactory) {
        var serviceBase = '/api/OMData/';
        var  BuyersFactory = {};
        buyersService.cache = $cacheFactory('cacheId');
    
        var serverPromise; // Will be used to store the promise returned by $http on first server call
    
        BuyersFactory.GetBuyers = function (type) {
            var dataList =  buyersService.cache.get('BuyerData');
    
            function serverCall () { // stores promise receives by $http and return it
                serverPromise = $http.get(serviceBase + 'GetBuyers').then(
                    function (results) {
                        buyersService.cache.put("BuyerData", results.data);
                        return _getBuyerByType(type, results.data);
                    });
                return serverPromise;
            }
    
            if (dataList !== null && dataList.length > 0) {
                return $q.when(_getBuyerByType(type,dataList)); // auto resolving promise
            }
    
            // If a first call to the server was already done, serverPromise will exist and no further call to the server will be made
            return serverPromise || serverCall();
        };
    
        app.factory('OMDataService', ['$http', '$q', '$cacheFactory', buyersService]);
    
    });
    
    
    
    function _getBuyerByType(Type,dataList) {
        try {
    
            var typedBuyer= Enumerable.From(dataList).Where(function (x) {
                return x.Type== Type;
            }).ToArray();
            return typedBuyer;
        } catch (e) {
            throw e;
        }
    }