[英]How to consume a RESTful API with pagination to fetch all records sequentially in AngularJS
The API I am consuming provides with the link headers as such: 我正在使用的API提供了这样的链接头:
</resource?page=1&limit=10>; rel="next",
</resource?page=1&limit=10>; rel="last",
</resource?page=0&limit=10>; rel="first"
And I need to consume the /resource
endpoint, 10 objects at a time, in a loop, until there's no more next
in link headers (last page). 我需要循环使用一次
/resource
端点,一次10个对象,直到链接头(最后一页)中没有next
一个对象为止。
I have a resource as such: 我有这样的资源:
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;
}
}
}
});
}]);
and I have a service as such: 我有这样的服务:
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;
},
...
};
}]);
Well, your eyes might hurt, because I know this is not the proper way to achieve this, since hastNext
is not updated before the promise is actually executed, this leads to an infinite loop. 好吧,您的眼睛可能会受伤,因为我知道这不是实现此目标的正确方法,因为
hastNext
在promise实际执行之前没有更新,所以会导致无限循环。 But I couldn't get my head around it. 但是我无法解决这个问题。 Any help appreciated, thanks.
任何帮助表示赞赏,谢谢。
With reference to the comment by bigless , this is how I solved it: 参考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.