Activate your free membership today | Log-in

Tuesday, April 15th, 2008

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

Category: Dojo, XmlHttpRequest

<p>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:

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

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

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

which is as easy as:

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

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.

Related Content:

  • Common Web application security exploits and how to stop them
    When examined at the granular level, Web applications nearly always have a number of security holes. At the 2009 Ajax Experience, Joe Walker, creator...
  • Ajax mature enough for the enterprise
    Edwin Aoki, technology fellow at AOL LLC., delivered a keynote address titled "Ajax in the Real World" at The Ajax Experience conference in San...
  • Distinguishing a faked XMLHTTP request from a real one
    When verifying XMLHTTP requests, don't depend on your Web application to determine the difference between real and fake. Web services security expert...
  • XHR
    XMLHttpRequest, also called XHR, is an application program interface (API) that was first used by Microsoft in version 5.0 of its Internet Explorer...
  • XMLHttpRequest
    XMLHttpRequest, also called XHR, is an application program interface (API) that was first used by Microsoft in version 5.0 of its Internet Explorer...

Posted by Dion Almaer at 6:59 am
2 Comments

++++-
4.8 rating from 18 votes

2 Comments »

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

Jordan:

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.

Regards

Comment by slightlyoff — April 18, 2008

Leave a comment

You must be logged in to post a comment.