Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to execute some code once I load resources from my back-end. I was able to do this by using a callback on ONE resource request like this:

$scope.resrc = Resrc.query({idResource: 1}, function(){
    //CODE AFTER RESOURCE IS LOADED
});

But trying to use $q to wait for MULTIPLE resources to load an then execute code is NOT working for me! (As they suggest here http://stackoverflow.com/a/14545803/215945)

$q.all([
        $scope.services = Services.query({idResource: 1}),
        $scope.brands = Brands.query({idResource: 1})
    ]).then(function() { 
        //CODE AFTER RESOURCES ARE LOADED 
});

What am I doing wrong?

share|improve this question
    
How do you know it's not working, where is your result array? –  Shomz Nov 13 '13 at 3:53

2 Answers 2

up vote 1 down vote accepted

From Angular documentation

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means that in most case one never has to write a callback function for the action methods.

The key point here is that, the object you are passing is NOT a promise.

A promise is a way to say you are waiting for something to finish first. It is the backbone of ajax, and I would recommend reading up on it if you are unfamiliar.

To make $q work you will need to give the promise instead object or reference instead

From the same documentation

The Resource instances and collection have these additional properties:

$promise: the promise of the original server interaction that created this instance or collection.

On success, the promise is resolved with the same resource instance or collection object, updated with data from server. This makes it easy to use in resolve section of $routeProvider.when() to defer view rendering until the resource(s) are loaded.

On failure, the promise is resolved with the http response object, without the resource property.

$resolved: true after first server interaction is completed (either with success or rejection), false before that. Knowing if the Resource has been resolved is useful in data-binding.

So you will need to do something like

$scope.services = Services.query({idResource: 1});
$scope.brands = Brands.query({idResource: 1});

$q.all([
    $scope.services.$promise,
    $scope.brands.$promise
]).then(function() { 
    //CODE AFTER RESOURCES ARE LOADED 
});
share|improve this answer
    
Although both answers were the same, I find @kelaban 's answer much more explanatory, thanks!! –  user1575299 Nov 13 '13 at 4:24

I think this is because the objects that are returned from the calls to $resource are not promises. I have never used this feature but the documentation suggests

$scope.services = Services.query({idResource: 1});
$scope.brands = Brands.query({idResource: 1});

$q.all([
    $scope.services.$promise,
    $scope.brands.$promise
]).then(function() { 
    //CODE AFTER RESOURCES ARE LOADED 
});
share|improve this answer
    
Worked like a charm! Thanks @akonsu –  user1575299 Nov 13 '13 at 4:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.