I need some help to understand how you can handle a JSON document with pagination
Let's say I use ORDS that has a 500 result limit pagination size:
When I'm doing my JQuery.getJSON call, if I know I'll get under 500 results, I can happily do something like this:
self.myThings= ko.observableArray();
$.getJSON("http://xyz.com/apex/xyz/logs/get/").
then(function (result) {
var tmpData = self.myThings();
$.each(result.items, function () {
tmpData.push({
thing: this.thing
});
self.myThings(tmpData);
});
});
Now if I get a 500+ results document, I'll have something like this in my JSON response:
{"next":{"$ref":"http://xyz.com/apex/xyz/logs/get/ ?page=1"},"items":[{"thing":"thing1"}, ... ]}
How do I fetch with JQuery to retrieve all pages, in an ordered way so I can have an array (self.myThings here) that have all the items ordered correctly?
The only way I thought, and it's not very clean would be to make a do/while loop, that iterate until I get a page with no item..
self = this;
self.myThings= ko.observableArray();
// More pages on the resultset
var stillMore = true;
// Processing of a single page is over
var Queryfinished = true;
var url = "http://xyz.com/apex/xyz/logs/get/";
do
{
if (Queryfinished) {
Queryfinished = false;
$.getJSON(url).
then(function (result) {
if (result.next !== null) {
stillMore = true;
url = result.next[0];
} else
{
stillMore = false;
}
var tmpData = self.myThings();
$.each(result.items, function () {
tmpData.push({
thing: this.thing
});
self.myThings(tmpData);
Queryfinished = true;
});
});
}
} while (stillMore || ( !stillMore && !Queryfinished))
I'm pretty sure there's something better than that but I just don't see it..
Any advices?
Thanks!