Execute code after multiple resources have loaded

67 views
Skip to first unread message

Eric Feng

unread,
Mar 12, 2013, 1:48:47 PM3/12/13
to ang...@googlegroups.com
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!'); 
})

Eric Feng

unread,
Mar 12, 2013, 1:52:06 PM3/12/13
to ang...@googlegroups.com

Caleb Olin

unread,
Mar 12, 2013, 7:02:33 PM3/12/13
to ang...@googlegroups.com
Take a look at the $q service:


You can use it to bundle up promises then execute a callback when they've all loaded.

Mark Perry

unread,
Mar 12, 2013, 7:32:31 PM3/12/13
to ang...@googlegroups.com

Eric Feng

unread,
Mar 13, 2013, 11:44:56 AM3/13/13
to ang...@googlegroups.com
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.
 
 

Reply all
Reply to author
Forward
0 new messages