I have a REST API that I want to call from an AngularJS service like this:
angular.module('myModule').service('MyApi', ['$http', function($http) {
return ({
resources: resources,
details: details
});
function resources() {
return $http.jsonp('/api/resources');
}
function details(key) {
return $http.jsonp('/api/details/' + id);
}
}]);
There are other implementation details removed there such as authentication that aren't important. The API is provided by a third party so I can't change it.
GET /api/resources
returns something like:
[{ "key": "one" }, { "key": "two" }]
GET /api/details/one
returns something like:
{ "count": 5 }
I then have a controller where I want to call MyApi.resources()
, wait for the results and then for each result, call MyApi.details(resource)
. When the final call to MyApi.details(resource)
completes, I want to run a function to aggregate some results from the set of details, but I can't work out how to trigger this at the end.
My controller currently looks like this:
angular.module('myModule').controller('MyCtrl', ['$scope', 'MyApi', function($scope, MyApi) {
$scope.results = new Array();
MyApi.resources().then(function(response) {
var resources = response.data;
var length = resources.length;
for (var i = 0; i < length; i++) {
MyApi.details(resources[i].key).then(function(response) {
$scope.results.push(response.data.count);
});
}
});
// how do I get this line to run only after all the results are returned?
$scope.total = $scope.results.reduce(function(a, b) { return a + b; }, 0);
}]);
What is the best way to achieve the aggregation at the end?
You can use deferred's function $q.all.
angular.module('myModule').controller('MyCtrl', ['$scope', 'MyApi', '$q', function($scope, MyApi, $q) {
$scope.results = new Array();
MyApi.resources().then(function(response) {
var resources = response.data;
var length = resources.length;
var defer = $q.defer();
var promises = [];
angular.forEach(resources, function(value) {
promises.push(MyApi.details(resources[i].key));
});
$q.all(promises).then(function() {
$scope.total = $scope.results.reduce(function(a, b) { return a + b; }, 0);
});
}
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments