Angularjs promises chaining when working with pagination

I am working with a REST api that provides a paged response for GET requests, for example:

{count: 43103
previous: null
next: http://ecoengine.berkeley.edu/api/photos/?page=2
results: [json objects....]
}

I would like to create a service that loads all the data, following the link nextuntil nextit becomes zero. I am stuck in the promises chain in this scenario and would appreciate any help on how to proceed (angular / js newbie here). My plunker with where I got it so far is here http://plnkr.co/edit/ySiQLvu9RNrKkQAoDmKh . From the console messages you can see that the code retrieves data only from the first 2 pages. Thanks.

+4
source share
1 answer

promises .

http://plnkr.co/edit/NPh6uQ2DgVuhVxUgHB6h?p=info

, loadData, .

var loadData = function(url) {
      var deferred = $q.defer();

      function loadAll() {
        $http.get(url)
           .then(function(d) {
                debugger;
                console.log('private http.get().then()');
                console.log(d);
                aggregateData.value.push(d.data.results);
                if(d.data.next) {
                   url=d.data.next;
                   loadAll();
                }
                else {
                   deferred.resolve(aggregateData.value);
                }
           })
      }
      debugger;
      loadAll();
      return deferred.promise;

 };

aggregateData, , loadData.

+3

All Articles