On Wednesday 09 July 2014 06:19:39 Chris Scribner wrote:
> As an example, this code seems to work for things returned by
> $resource:
> 
> $scope.things = Model.getThings({ property: '123' });
> 
> However, this seems to be the way to do it with promises now:
> 
> Model.getThings({ property: '123' }).$promise.then((things) => {
>     $scope.things = things;
> });
> 
> Accessing .$promise directly feels really weird though. I also don't
> understand why $resource returns an array containing $promise and
> $resolved instead of just returning the promise (maybe it deals with
> getting the automatic unwrapping behavior?). I tried returning a
> similar structure from one of my own methods and it did not get
> automatically unwrapped...
> 
> Can anyone help explain the thinking around this and how we should use
> it?

I'm plainly lacking the experience to answer this with any certainty, 
but as there are no other takers... I've rummaged around a bit in the 
angular sources what seems to be happening is that your Model.getThings 
immediately returns an empty object (or collection). When the http 
request completes and the associated promise is resolved, the original 
object is updated. Then the $digest loop is triggered, which detects the 
change and updates the view accordingly.

https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L565-L596
https://github.com/angular/angular.js/blob/g3_v1_2/src/ng/http.js#L1010

Michael

-- 
Michael Schuerig
mailto:[email protected]
http://www.schuerig.de/michael/

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to