从 API 加载分页数据

Loading of paginated data from API

我正在开发一个系统,该系统当前需要我从 API.

加载所有项目

API 内置了分页功能。我一直在多次呼叫 API 并且 $http.get 诅咒系统不响应。例如,一旦我加载需要多次调用 API 的页面(根据页面数量可能会调用 50 到 80 次),在几分钟内我所做的任何事情都不会响应,直到调用API 快完成了。已经试了很多方法都不行。

$scope.loadAllPagedItems = function (category_uuid, max_pageitem, item_perpage) {
    for (var a = 0 ; a < max_pageitem; a++) {
        itemResource.findItems(category_uuid, item_perpage, a).then(function (response) {
            if (response.data.data.length > 0) {
                for (var a = 2 ; a < $scope.categories.length; a++) {
                    if ($scope.categories[a][0].category_uuid == response.data.data[0].category_uuid) {
                        for (var j = 0; j < response.data.data.length; j++) {
                            $scope.categories[a][0].data.push(response.data.data[j]);
                        }
                    }
                }
            }
        }, function (error) {
            console.log(error);
        })
    }
}

有什么办法可以做得更好吗?

从异步中顺序检索分页数据 API

$scope.loadAllPagedItems = function(category_uuid, max_pageitem, item_perpage) {
    var promise = $q.when([]);
    for (let a = 0 ; a < max_pageitem; a++) {
        promise = promise
          .then(function(dataArray) {
            var p = itemResource.findItems(category_uuid, item_perpage, a);
            p = p.then(function (response) {
                  return dataArray.concat(response.data.data);
            });
            return p;
        });
    };
    return promise;
};

上面的例子,执行XHRs sequentially and uses the array concat方法将解析的数组合并成一个数组。

用法:

$scope.loadAllPagedItems(category, pages, itemsPerPages)
  .then(function(finalArray) {
    console.log(finalArray);
}).catch(function(response) {
    console.log("ERROR ",response.status);
    throw response;
});