Tuesday, April 15th, 2008

Dojo XHR Plugins; How do you want your XHR today?

Category: Dojo, XmlHttpRequest

Neil Roberts goes into the XHR Plugins that Dojo uses and how you can extend the system to have your own.

If you look at dojo.xhrGet you will see “Acceptable values are: text (default), json, json-comment-optional, json-comment-filtered, javascript, xml”, but:

What you may not know is that the handleAs parameter is merely a way of specifying what plugin to use. Knowing where these plugins are, how they work, and how they can be adapted to suit your project will allow you to make repetitive tasks easy and less error-prone.

He starts by piggybacking on the json style callback, and adds a hook so when you do a query, if there are updated objects out there, they come back in the JSON so you can deal with them without having an extra remote call:


  1. dojo._contentHandlers.json = (function(old){
  2.   return function(xhr){
  3.     var json = old(xhr);
  4.     if(json.updated){
  5.       processUpdatedObjects(json.updated);
  6.       delete json.updated;
  7.     }
  8.     return json;
  9.   }
  10. })(dojo._contentHandlers.json);

Next, Neil goes on to write a system that auto updates nodes:


  1. dojo.xhrGet({
  2.   url: "node-updates.php",
  3.   handleAs: "node-update-server"
  4. });
  6. dojo.xhrGet({
  7.   url: "node-content.php?node=sidebar",
  8.   node: "sidebar",
  9.   handleAs: "node-update"
  10. });

which is as easy as:


  1. dojo.mixin(dojo._contentHandlers, {
  2.   "node-update-server": function(xhr){
  3.     var json = dojo._contentHandlers.json(xhr);
  4.     dojo.forEach(json.updates, function(update){
  5.       var node = dojo.byId(update.id);
  6.       if(node){
  7.         node.innerHTML = update.html;
  8.       }
  9.     });
  10.   },
  11.   "node-update": function(xhr){
  12.     var node = dojo.byId(xhr.args.node);
  13.     if(node){
  14.       node.innerHTML = dojo._contentHandlers.text(xhr);
  15.     }
  16.   }
  17. });

In conclusion:

Dojo makes it incredibly easy to change the way that your Ajax calls work. You can change the format of JSON your server returns without having to change any of your callbacks, you can change the handleAs type for a single function to change the data given to your callback, you can get rid of callbacks altogether and use the arguments to your xhr call determine what should be done with your results.

Posted by Dion Almaer at 6:59 am

4.4 rating from 23 votes


Comments feed TrackBack URI

I think all these “plugin” things from jQuery and Dojo are overrated. All you’re really doing is extending an object’s prototype with your own methods. Nothing particularly architecturally elite about it as it’s a by-design behavior in the JavaScript language itself.

Also, this example with Dojo is far from “pluggable”. All you’re doing is hijacking an existing method. Anyone can inject their own functionality by hijacking existing methods. Moreover, you’re modifying the private variables of an object (private by convention, at least) which is a big no-no in programming integrity.

Comment by Jordan — April 15, 2008


this *isn’t* extending an object prototype, it’s extending the registry for content handlers. Yes, one example hijacks an existing handler, but the dojo.mixin() based example adds completely new handlers to the registry. Maybe it’s not big magic, but it’s certainly useful and as your app grows and your needs grow outside of the pre-ordained “sweet spot” of a toolkit, it’s great to know that your toolkit of choice has provided public, documented, places for extensions which can help you build up your tools while you build out your own app code.


Comment by slightlyoff — April 18, 2008

Leave a comment

You must be logged in to post a comment.