-1

My scenario is that I want to use ngResource to retrieve lists of files from Google Drive. Drive paginates its results via the inclusion of a nextPageToken within the JSON response, null signifying no more results.

I need a promise or callback that refers to the completion of all pages, rather than after each page. Is there an elegant way for ngResource to handle this?

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Possible duplicates: http://stackoverflow.com/questions/16465768/how-to-handle-pagination-and-count-with-angularjs-ressources http://stackoverflow.com/questions/17460003/how-to-make-a-server-side-pagination-with-angularjs-resource http://stackoverflow.com/questions/14176936/limit-and-offset-data-results-in-angularjs-for-pagination/14180451#14180451 – JBCP Feb 21 '14 at 19:34

1 Answers1

1

I don't think ngResource will inherently do anything like this for you by default, but you can do it by using a recursive closure and calling the ngResource inside it until you get all the data you are after.

var dataSet = [];
function getAllData(){
  var files = $resource('your url');
  files.get(function(data){
    if (data){
      dataSet.push(data); //or whatever   
      getAllData();
    }
  })();
}

Here is the tested version with actual Google Drive semantics

getAllFiles(nextLink:string){
    var self = this;
    if (nextLink) {
        var files = self.refs.resource(nextLink);
    } else {
        var files = self.refs.resource(this.DRIVE_URL);
    }
    files.get(function(data){
        console.log("fetched "+data.items.length);
        self.allFiles.push(data['items']);
        if (data['nextLink']){
            self.getAllFiles(data['nextLink']);
        }
    });
}
cezar
  • 11,616
  • 6
  • 48
  • 84
BoxerBucks
  • 3,124
  • 2
  • 21
  • 26
  • 1
    That worked. For those who may follow, I've edited your answer to include the actual Drive-specific version that I ended up with. – pinoyyid Jan 29 '14 at 17:24