我正在使用REST API来返回像foll这样的分页响应:
{count: 43103
previous: null
next: http://ecoengine.berkeley.edu/api/photos/?format=json&page=2
results: [json objects....]
}
目前,我使用$ http来获取数据并使用递归链接承诺。我如何使用Restangular做同样的事情?这是我使用$ http:
的代码.factory('holosData', ['$q', '$http', function($q, $http ) {
var aggregateData = { isLoaded: false, value: [] };
return {
loadData: function(url) {
var deferred = $q.defer();
//empyt array of any previous results
aggregateData.value.length = 0;
function loadAll() {
$http.get(url)
.then(function(d) {
aggregateData.value = aggregateData.value.concat(d.data.results);
if(d.data.next) {
url=d.data.next;
loadAll();
}
else {
deferred.resolve(aggregateData.value);
}
});
}
loadAll();
return deferred.promise;
},
getData: function() {
return aggregateData.value;
}
}])
这就是我迄今为止尝试过的事情。我可以获取第一页,但我无法弄清楚如何在Restangular中链接promise。
loadData: function() {
var deferred = $q.defer();
//empty array of any previous results
allPhotos.length = 0;
function loadAll() {
Restangular.all('photos').getList(queryParams).then(function(list) {
allPhotos = list;
deferred.resolve(allPhotos);
});
}
loadAll();
return deferred.promise;
}
谢谢!