admin管理员组文章数量:1406048
From the API I'm working on I need to take 2 different lists and I need to take in chunks of 20 items to avoid server timeouts.
What I built actually is this:
Items1.query().$promise
.then(function (data) {
$scope.items1 = data.list;
return Items2.query().$promise;
})
.then(function (data) {
$scope.items2 = data.list;
});
With this code I'm downloading the entire list of objects.
Both the query return:
{
list: [...],
next: true,
limit: 20,
last: 20
}
Basically it is a pagination system.
Both services are like this:
App.factory('Items1', ['$resource',
function($resource) {
return $resource('items1/:item1Id', { storeId: '@id'
}, {
query: {
method: 'GET',
isArray: false
},
update: {
method: 'PUT'
}
});
}
]);
I don't really know how to make recursive function with $resource in order to push those items in chunks of 20.
From the API I'm working on I need to take 2 different lists and I need to take in chunks of 20 items to avoid server timeouts.
What I built actually is this:
Items1.query().$promise
.then(function (data) {
$scope.items1 = data.list;
return Items2.query().$promise;
})
.then(function (data) {
$scope.items2 = data.list;
});
With this code I'm downloading the entire list of objects.
Both the query return:
{
list: [...],
next: true,
limit: 20,
last: 20
}
Basically it is a pagination system.
Both services are like this:
App.factory('Items1', ['$resource',
function($resource) {
return $resource('items1/:item1Id', { storeId: '@id'
}, {
query: {
method: 'GET',
isArray: false
},
update: {
method: 'PUT'
}
});
}
]);
I don't really know how to make recursive function with $resource in order to push those items in chunks of 20.
Share Improve this question edited Oct 4, 2015 at 17:02 Viktor Bahtev 4,9082 gold badges35 silver badges41 bronze badges asked Oct 4, 2015 at 16:41 Ayeye BrazoAyeye Brazo 3,4869 gold badges40 silver badges75 bronze badges 8- seems like you need another resource method and api endpoint that includes a page param in url – charlietfl Commented Oct 4, 2015 at 17:31
-
I have everything to do the pagination and it works fine, I just need to cycle the query for each item unless the
next
value isfalse
– Ayeye Brazo Commented Oct 4, 2015 at 17:39 -
well if
query()
only returns 20 you need another method that sets the start point – charlietfl Commented Oct 4, 2015 at 17:43 - I don't understand what do you mean... sorry. – Ayeye Brazo Commented Oct 4, 2015 at 17:56
- how does api know what to deliver if you don't set start point? – charlietfl Commented Oct 4, 2015 at 18:09
1 Answer
Reset to default 12 +50I wrote an example jsfiddle to show recursive promises. Converting it for your example, it would looks something like:
function getList(resource, list, last) {
return resource.query({last: last}).$promise.then(function(data){
list = list.concat(data.list);
if (data.next) {
return getList(resource, list, data.last);
}
return list;
});
}
getList(Items1, [], 0).$promise.then(function(list) {
$scope.items1 = list;
});
getList(Items2, [], 0).$promise.then(function(list) {
$scope.items2 = list;
});
You would need to modify your angular resources to allow you to pass the last
parameter in the API call. I'm assuming that the API, if provided with that parameter, will return the next section starting from it.
本文标签: javascriptAngular resource recursive queryStack Overflow
版权声明:本文标题:javascript - Angular $resource recursive query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744965861a2634941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论