Execute code after multiple resources have loaded

Showing 1-5 of 5 messages
Execute code after multiple resources have loaded Eric Feng 3/12/13 10:48 AM
In Angular, what is the best practice for triggering code after multiple models have been populated by resource services. Nest $scope.$watch? 

Right now, I'm cheating and checking off values in an array, which doesn't feel very "angular."
$scope.loaded = [];
$scope.modelA = aResource.query({}, function() {$scope.loaded.push('a')});
$scope.modelB = bResource.query({}, function() {$scope.loaded.push('b')});

$scope.$watch(loaded.length, function(newValue) {
if ($scope.loaded.indexOf(modelA) != -1 && $scope.loaded.indexOf(modelB) != -1) {
console.log('done!'); 
})
Re: Execute code after multiple resources have loaded Eric Feng 3/12/13 10:52 AM
Also, posted on StackOverflow, for those who want karma. :)

Re: Execute code after multiple resources have loaded Caleb Olin 3/12/13 4:02 PM
Take a look at the $q service:


You can use it to bundle up promises then execute a callback when they've all loaded.
Re: Execute code after multiple resources have loaded Mark Perry 3/12/13 4:32 PM
Perhaps an answer in this thread:


M
Re: [AngularJS] Re: Execute code after multiple resources have loaded Eric Feng 3/13/13 8:44 AM
Thank you! I think everyone is right: it's time for me to learn promises, something that I've been avoiding for some time. :)


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/TizlifUL7FU/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.