本文介绍了AngularJS处理多次调用promise,但有一些例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过这个问题( AngularJS处理多次调用promise的承诺)现在我有不同的障碍.现在,我必须获取城市列表,但是有一个例外.

I asked this question before (AngularJS handle calling promise multiple times) and now I have different obstacle. Now I have to get cities list but there is an exception.

城市可以像国家一样被多次调用(在我的老问题中),我必须缓存数据以防止多次调用同一数据(城市).旧问题的解决方案可以阻止多个呼叫,但是现在我不得不打个电话(针对新国家的城市).

Cities can be called multiple times like countries (in my old question) and I have to cache data to prevent multiple calls for same data(cities). Old question's solution can block multiple calls but now I have to let some calls (for new country's cities).

所以我的问题是:如何缓存城市数据以防止调用相同数据?(我的函数必须捕获呼叫是否针对新国家/地区的城市列表.如果是,则呼叫服务并获取城市,如果不是,则从缓存中返回城市)

So my question is:How can I cache cities data to prevent call for same data? (My function have to catch if call is for new country's cities list or not. if yes: call service and get cities, if not: return cities from cache)

这是我的服务:

var cityCache = {};
vm.getCities = function (countryCode) {

    if (countryCode!=undefined && !cityCache[countryCode]) {

        vm.cityPromise = $http({
            method: 'POST',
            cache: true,
            url: API + '/api/Global/CountryCities',
            data: {
                "CountryCode": countryCode
            }
        }).then(function successCallback(response,countryCode) {
            if (errorHandler(response.data)) {
                console.log("cities come from ajax")
                cityCache[response.config.data.CountryCode] = response.data;
                console.log(cityCache)
                return response.data
            }
        });
    } else {
        vm.cityPromise = $timeout(function () {//I use this to get promise object
            return cityCache[countryCode]
        }, 0)
        console.log("cities comes from cache");
    }

    return vm.cityPromise;
}

示例:假设我同时调用了3次getCities函数.我正在通过chrome查看我的网络流量.我看到了3个ajax调用.这是正常的.但是有时候,我呼吁同一个城市.我需要编辑我的函数,该函数可以了解之前是否已经调用过城市数据(某种缓存).
例如:如果我用以下参数询问函数3次:

1给我德国的城市,
2给我爱尔兰的城市,
3-再一次给我德国的城市,

这是3次通话.但我想给德国打电话1次,给爱尔兰打电话1次.只需打出2个电话.

Example:Let's say I am calling getCities function 3 times in the same time. I am watching my network traffic via chrome. I see 3 ajax calls. It's normal. But sometimes, I call for same city. I need to edit my function that can understand if city data is already called before (kind of cache).
For example: If i ask function 3 times with this arguments:

1-Give me the cities in Germany,
2-Give me the cities in Ireland,
3-Give me the cities in Germany (again),

It's calling 3 times. But I want 1 call for Germany, 1 call for Ireland. Just 2 calls.

推荐答案

答案与您的其他问题相同,只需将国家/地区代码映射到承诺即可.与以前一样,请考虑错误情况.

Same answer as your other question, just map to country code to the promise.Also same as before, consider the error case.

var vm = this;

vm.cityPromises = {};

function getCities(countryCode) {
    if (!vm.cityPromises[countryCode]) {
        vm.cityPromises[countryCode] = $http({
            method: 'POST',
            cache: true,
            url: API + '/api/Global/Countries',
        }).then(function successCallback(response) {
            if (errorHandler(response.data)) {
                console.log("ajax")
                return response.data;
            }
        });
    } else {
        console.log("cache")
    }
    return vm.cityPromises[countryCode];
}

这篇关于AngularJS处理多次调用promise,但有一些例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 21:36