簡體   English   中英

如何通過分頁使用RESTful API來按順序獲取AngularJS中的所有記錄

[英]How to consume a RESTful API with pagination to fetch all records sequentially in AngularJS

我正在使用的API提供了這樣的鏈接頭:

</resource?page=1&limit=10>; rel="next",
</resource?page=1&limit=10>; rel="last",
</resource?page=0&limit=10>; rel="first"

我需要循環使用一次/resource端點,一次10個對象,直到鏈接頭(最后一頁)中沒有next一個對象為止。

我有這樣的資源:

myResources.factory('MyResource', [
    '$resource',
    function($resource) {

        const ENDPOINT = '/api/resource/:id';

        return $resource(ENDPOINT, null, {
            query: {
                method: 'GET',
                isArray: true,
                interceptor: {
                    response: function(response) {
                        response.resource.headers = response.headers;
                        return response.resource;
                    }
                }
            }
        });
    }]);

我有這樣的服務:

myServices.factory('MyResourceService', [
    function(MyResource) {
        return {
            findResources: function(){
                return MyResource.query().$promise;
            },
            findAllResources: function(){
                // I need to return a promise which will fetch all 
                // results from the server synchronously

                var hasNext = true;
                var params = {limit: 10, page: 0};
                var chain = $q.all();

                while(hasNext){
                    chain = chain.then(function(){
                        return MyResource.query(params).then(function(res){
                            var next = linkHeaderParser.parse(res.headers('link').next);
                            if(next) params = {limit: next.limit, page: next.page};
                            else hasNext = false;
                        }, function(){
                            hasNext = false;
                        });
                    });
                }

                return chain;
            },
            ...
        };
    }]);

好吧,您的眼睛可能會受傷,因為我知道這不是實現此目標的正確方法,因為hastNext在promise實際執行之前沒有更新,所以會導致無限循環。 但是我無法解決這個問題。 任何幫助表示贊賞,謝謝。

參考bigless評論 ,這是我解決的方法:

            var getResource = function(params){
                return MyResource
                    .query(params)
                    .$promise;
            };

            // Method that extracts the last page
            var pageCount = function(response){
                var last = 
                linkHeaderParser.parse(response.headers('link')).last;
                return parseInt(last.page);
            };

            params.limit = 0;
            params.page = 0;

            var deferred = $q.defer();
            var resources = [];

            // Method that extracts the last page
            getResource(params)
                .then(function(res){

                    var promises = [];
                    var lastPage = pageCount(res);
                    resources = resources.concat(res);

                    // Loop to get all pages
                    for(var i = params.page + 1; i <= lastPage; i++){
                        params.page = i;
                        promises.push(getResource(params));
                    }

                    return $q.all(promises);
                }, function(err){
                    if(err.headers('x-pagination-count') &&
                        parseInt(err.headers('x-pagination-count')) === 0)
                        return $q.all([]);
                    }
                })
                .then(function(res){

                    // Concat results and resolve the promise
                    for (var i = 0; i < res.length; i++){
                        resources = resources.concat(res[i]);
                    }

                    deferred.resolve(resources);
                });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM