I'm currently trying to find a kind of best practice to make multiple calls to $resource in function of the return of a first one.
Consider the following schema : we have a database of authors, that have books, that have multiples tags (sf,thriller,horror,...).
angular.module('myApp',[ngResource])
.controller('myCtrl',['$scope', function($scope){
$scope.results = [];
apiService.Authors.getAll().$promise //return array of authors
.then(function(authors){
$scope.results.push(authors);
angular.forEach(authors,function(akey,author){
apiService.BooksBy.getAll({authorID:author.id}).$promise //return array of books
.then(function(books){
$scope.results[akey].books = [];
$scope.results[akey].books.push(books);
angular.forEach(books,function(bkey,book){
apiService.Tags.getAll({bookID:book.id}).$promise //return array of tags
.then(function(tags){
$scope.results[akey].book[bkey].tags = [];
$scope.results[akey].book[bkey].tags.push(tags);
});
});
});
});
});
}]);
Is this the only way to make multiple nested calls ?
I was thinking of something more flattened and more readable, something like a workaround with chained promises.
Any ideas would be welcomed, as I'm unable to find how to do this in a better way.
Regards !