Resolving An AngularJS Deferred Object Twice With DeferredWithUpdate.js

Posted October 8, 2012 at 10:36 AM by Ben Nadel

Tags: Javascript / DHTML

A couple of weeks ago, I blogged about using jQuery's Deferred.notify() to provide a locally-cached response before the server had time to resolve the request. Lately, as I've been playing around with AngularJS, I've wanted to implement the same kind of "update" behavior with the $q class - AngularJS' deferred implementation. Since the $q class doesn't have a notify() method, however, I've created a new AngularJS class that decorates the core $q class with update() and mistake() callback hooks.


 
 
 

 
 
 
 
 

View the DeferredWithUpdate.js project on GitHub.com.

The DeferredWithUpdate.js module has the same API as the core $q class:

  • all()
  • defer()
  • reject()
  • when()

The defer() method instantiates the core $q.defer() method and then augments both the deferred object and its promise. The all(), reject(), and when() methods currently pass the request directly through to the underlying $q class without augmentation.

When the defer() method decorates the underlying promise object, it adds two callback hooks:

  • update( callback )
  • mistake( callback )

These callbacks will only be invoked if the deferred object events are triggered once the deferred object is already in a resolved state. If the deferred object has been resolved and then the resolve() method is called again, the second request will be routed to the update() callbacks. Similarly, if the deferred object has been resolved and then the reject() method is called, it will be routed to the mistake() callbacks.

The intent here is that the deferred object can be resolved with locally-cached data while still providing the calling context with a hook into the server's true response. And, while the calling context does need to be aware of this new class, the callbacks should work completely fine even if no caching is used.




Reader Comments

Oct 8, 2012 at 2:47 PM // reply »
1 Comments

Interesting. These decorators are more powerful than what we expected. :-)

I'm curious if others find these api useful. They kind of break the expectations one should have from a promise, but who knows.. maybe the spec is wrong :)


Oct 8, 2012 at 2:54 PM // reply »
11,238 Comments

@Igor,

I don't think the spec is wrong ;) I've been loving Deferred / Promise objects for a long time now and this project is the first one where I really wanted this kind of augmentation. This is very specifically for combining locally-cached responses with server-side responses.

I tried, originally, with just the $q class. And this worked great well there was no caching. Then I tried using the $resource class; and that worked great when there was no caching. In both cases, I was basically using the success-callback to update the UI.

When, when I added client-side caching / localStorage integration, then I ended up invoking the callbacks for the cached data and never knew when the server data would come back.

In my situation, I am actually using $q AND $resource - the deferred object resolves to the resource instance. This allows the value to update dynamically (ala the resource behavior). But, I was still left in the cold as to how to "filter" the resultant values once they came back from the server.

But, like I said, this is my first AngularJS project - and I know there is an "angular way". So, this may be crazy - may be missing the point.


Oct 8, 2012 at 3:01 PM // reply »
11,238 Comments

@Igor,

One more thing - sorry - since my deferred usually resolve to Resource instances, you only need to use the update() callback *if* you need to *further consume* the data once it comes back from the server. If not, then the simple then() callbacks will already propagate the resource response into the array or object reference.


Oct 12, 2012 at 4:22 PM // reply »
11,238 Comments

@All,

As a follow-up, I've looked at applying cached responses to AngularJS $resource responses:

http://www.bennadel.com/blog/2432-Applying-A-Cached-Response-To-An-AngularJS-Resource.htm

... the trick is to never break the original object reference.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
May 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools