Calling only once / caching the data from a $http get in an AngularJS service

后端 未结 4 988
春和景丽
春和景丽 2021-02-10 16:50

This may sound like a really simply/stupid question but I need to ask it as I haven\'t came across this scenario before... okay I have a service in my angularJS app. this servic

相关标签:
4条回答
  • 2021-02-10 17:29

    It is just as you say; this code can (and should) be refactored in many ways. One example:

    Let us factor the HTTP stuff into a separate service, that will also take care of caching. (Another idea for this would be to have a service for the HTTP/remote calls and another - maybe a general use decorator - to handle caching. LEt us not go into so much detail for now.) And let us put the formatting code in another method:

    The remote call service:

    .service('townHttpService', function($http, $q) {
        var cache;
    
        function getCities() {
            var d = $q.defer();
            if( cache ) {
                d.resolve(cache);
            }
            else {
                $http({method: 'GET', url: '/api/country/cities'}).then(
                    function success(response) {
                        cache = response.data;
                        d.resolve(cache);
                    },
                    function failure(reason) {
                        d.reject(reason);
                    }
                });
            }
            return d.promise;
        }
    
        function clearCache() {
            cache = null;
        }
    
        return {
            getCities: getCities,
            clearCache: clearCache
        };
    })
    

    The formatter:

    .service('townFormatter', function() {
        return function townFormatter(jsonData) {
            // HERE WE FORMAT THE response as desired... that creates a returnArray
            var returnArray = [], key;
            // loop through the countries
            for (key in jsonData['countries']) {
                 // formatting code...
            }
            // end of repeated CODE
            return returnArray; // this is array, we don't do any formatting here
        };
    })
    

    Your townDataService, written in terms of the above:

    .factory('townDataService', function (townHttpService, townFormatter) {
    
        var townList = {};
    
        townList.getTownList = function () {
            return townHttpService.getCities().then(townFormatter);
        };
    
        townList.getCurrentTown = function (place) {
            return townHttpService.getCities().then(townFormatter).then(function(cityList) {
                var currentTown;
                for (var i = 0; i < cityList.length; i++) {
                    // do stuff
                }
                return currentTown; // this is a string
            });
        };
    
        townList.getCurrentCountry = function (place) {
            return townHttpService.getCities().then(townFormatter).then(function(cityList) {
                var currentCountry;
                for (var i = 0; i < cityList.length; i++) {
                    // do stuff
                }
                return currentCountry; // this is a string
            });
    
        return townList;
    })
    
    0 讨论(0)
  • 2021-02-10 17:49

    There is nothing special that you can do and receive some considerable benefit. You would definitely need to cache your GET response and refactor a bit to avoid code duplication and improve readability:

    .factory('townDataService', function ($http) {
        var getCitiesAsync = function(){
            return $http({method: 'GET', url: '/api/country/cities', cache:true});
        };
    
        var townList = {};
    
        townList.getTownList = function () {
            return getCitiesAsync().then(prepareTownList);
        };
    
        var prepareTownList = function(response){
            //extract towns and do whatever you need
            return result;
        };
    
        ...
    

    As for using $cacheFactory - seems like an overhead for such a simple scenario, just use built-in cache option.

    0 讨论(0)
  • 2021-02-10 17:55

    I guess you got two questions there removing repeated logic and best way to cache results.

    First - Removing duplicate code: Looks like townList.getTownList is the common method, the other two methods is an extension of this method. So,

    townList.getCurrentTown = function(place) {
      var towns = townList.getTownList();
      for (var i = 0; i < returnArray.length; i++) { //additional stuff
      }
      return currentTown;
    };
    
    
    townList.getCurrentCountry = function(place) {
      var towns = townList.getTownList();
      for (var i = 0; i < returnArray.length; i++) { //additional stuff
      }
      return currentCountry;
    };
    

    Second - caching values Now the call is being http made only in townList.getTownList, the logic to cache can be easily implemented here. But this depends on whether the data will be onetime fetch or can be refreshed. One time fetch: just enable the cache in the http call $http({method: 'GET', url: '/api/country/cities', cache:true});

    Refresh based on request: I would pass an refresh variable to inform whether data has to be refreshed or not. So if the refresh is true or the townList is empty the data will be fetched.

    var srvc = this;
    var townList;
    townList.getTownList = function(refresh ) {
      if (refresh || !townList) {
        srvc.townList = $http({
          method: 'GET',
          url: '/api/country/cities'
        })
          .then(function(response) {
            var returnArray = [];
            var JsonData = response.data;
            for (var key in JsonData.countries) {}
            return returnArray; // this is array, we don't do any formatting here
          });
      }
    
      return townList;
    };
    
    0 讨论(0)
  • 2021-02-10 17:56

    To avoid timing issues, it is perhaps good to extend the solution a little bit:

    function getCities() {
            var d = $q.defer();
            if( cache ) {
                d.resolve(cache);
            }
            else {
                $http({method: 'GET', url: '/api/country/cities'}).then(
                    function success(response) {
                        if (!cache) {
                            cache = response.data;
                        }
                        d.resolve(cache);
                    },
                    function failure(reason) {
                        d.reject(reason);
                    }
                });
            }
            return d.promise;
        }
    

    After a (perhaps second or third) call to the webservice succeeds, one checks if the cache variable was set while waiting for server response. If so, we can return the already assigned value. That way, there will be no new assignment to the variable cache if multiple calls were issued:

    function success(response) {
        if (!cache) {
            cache = response.data;
    

    It does not have to make problems but if you rely on having identical objects (for example when working with data binding) it is great to be sure to only fetch data once!

    0 讨论(0)
提交回复
热议问题