Clever (ab)use of AngularJS promise resolution for lazy-loading async data

1,072 views
Skip to first unread message

Geoff Goodman

unread,
Apr 30, 2013, 6:14:37 PM4/30/13
to ang...@googlegroups.com
By assigning objects and arrays a then() method while that object/array represents a client-side representation of the resource, we can get some pretty neat behaviour based on AngularJS deep promise integration.

Please read the readme and check out the code: http://embed.plnkr.co/DkRdpOXzRZg2FZf677pN/readme

I'm thinking of taking this approach for Plunker itself and would love to hear about reasons that this is a BAD IDEA.

One that I've already seen is that I can't seem to get a template to react to when an object / array is loading. This is because even if I do ng-show="plunks.then", AngularJS will wait for plunks to be loaded before attempting to check the then property.

What do you think?

Andy Joslin

unread,
May 2, 2013, 12:44:51 PM5/2/13
to ang...@googlegroups.com
Cool :-)

Ryan Zec

unread,
May 2, 2013, 1:12:47 PM5/2/13
to ang...@googlegroups.com
Yea, I think this pattern has it use cases, I use it in my REST API library : https://github.com/nucleus-angular/rest


On Thu, May 2, 2013 at 12:44 PM, Andy Joslin <andyt...@gmail.com> wrote:
Cool :-)

--
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 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.
 
 

Geoff Goodman

unread,
May 2, 2013, 2:36:22 PM5/2/13
to ang...@googlegroups.com
Ryan, interesting. Here I was hoping to have been a pioneer... haha

Have you noticed any major/minor caveats in taking this approach?

Ryan Zec

unread,
May 2, 2013, 4:03:20 PM5/2/13
to ang...@googlegroups.com
Not so far.  Also this it somewhat similar to what the $resource component does (that is where I got the idea) so if it is good enough for them, it is good enough for me.  Not sure of any other way to have a return value that can be a promise or the result of a promise (whether or not a return value should be that is another question).

Geoff Goodman

unread,
May 2, 2013, 4:10:17 PM5/2/13
to ang...@googlegroups.com
Ryan, what I see the major difference being between what ngResource does and what you and I have done is to assign a then property to the underlying value of the resource/model.

I think an additional difference in the hack I'm presenting is that I *remove* the .then property from the array/object when the pending operation completes. This has the effect of transforming the array/object from a duck-typed promise (something with a then() method) into a simple json object.

When the next asynchronous operation needs to be performed, a 'then' property is re-added that corresponds to the promise of the new asynchronous operation.

One cool side-effect is that this means that you can easily make sure that you are not performing two asynchronous operations on the same data at the same time by wrapping any methods that issue asynchronous operations in a $q.when().then() callback so that these operations get chained in the order in which they were requested.


--
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/qPm-KpgUs2o/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages