The i-Technology Media!
Register | Log in
   
 
.NET  ·  AJAX  ·  CLOUD  ·  ECLIPSE  ·  FLEX  ·  OPEN WEB  ·  iPHONE  ·  JAVA  ·  LINUX  ·  OPEN SOURCE  ·  ORACLE  ·  PBDJ  ·  SEARCH  ·  SILVERLIGHT  ·  SOA  ·  VIRTUALIZATION  ·  WEB 2.0  ·  WIRELESS  ·  XML
Comments
Plone and Drupal: Different Approaches, Different Results
paul.nowak wrote: Matt, thanks for the comments. I made an error on the version of Plone. It's 2.5 Plone running on Zope 2.9x. In regards to the additional products, we have a skin installed and we have a product that we had custom developed for us that connects to a PostgreSQL database. We've looked at slow PostgreSQL queries causing problems and have not been able to find an issue. We've also tested for the case where the PostgreSQL server is down and have not been able to create an issue. We therefor...
Nov. 4, 2009 04:19 PM EST
Cloud Expo on Google News
Did you read today's front page stories & breaking news?


2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts

2009 East
GOLD SPONSORS:
CA
Get Your Transactions Under Control: SOA Performance Management
Software AG
Performance Driven Adoption: The Secret to Advancing SOA
Intel
The Evolving SOA Appliance: 3 Game-Changing Innovations
SILVER SPONSOR:
Denodo
Data Mashups: Deliver Your Project Faster with Virtualized Data Services Across Internal & External Sources
POWER PANELS:
The Business Value of Service Orientation
Driving Profitability Through User Experience
Click For 2008 West
Event Webcasts
Live Google News by SYS-CON!
Top Three Links You Must Click On


AJAXWorld News Desk
Functional AJAX with Dojo
Does Dojo have an AJAX method that updates a div?

By: Neil Roberts
Mar. 17, 2008 12:45 PM

SitePen "Watercooler of the Open Web" Blog

I frequently hear the question: "Does Dojo have an AJAX method that updates a div?" - The answer is that there's no Dojo-approved way of doing something like this. Updating a div is such an easy process, that implementing a de facto solution is not only extra code, but you also hit a wall as soon as you want to do "something more". The short and sweet solution looks something like this...

I frequently hear the question: "Does Dojo have an Ajax method that updates a div?". The answer is that there's no Dojo-approved way of doing something like this. Updating a div is such an easy process, that implementing a de-facto solution is not only extra code, but you also hit a wall as soon as you want to do "something more".

The short and sweet solution looks something like this:

 
dojo.xhrGet({
url: "title.php",
load: function(data){
dojo.byId("title").innerHTML = data;
// or
dojo.query("h1 .title").addContent(data);
}
});
 

This is a fine solution, but let's start discussing the DRY philosophy. If you work with more than one node or URL (which you probably will), and you want to process the HTML before you add it to your document or update the DOM after you've made your innerHTML assignment, you can end up with a lot of duplicated code. Armed with some knowledge, and the power of functional programming, we can fix this.

The load function in dojo.xhrGet is actually passed 2 arguments. The second argument passed to the load function is the XMLHttpRequest object that was used to make the call. Attached to this object is a field named args, which contains the arguments originally passed to dojo.xhrGet. This creates a handy way of passing information to your load function.

So now, we can write a cool new function that we can share between our dojo.xhrGet calls. Let's call it loadIntoNode:

 
function loadIntoNode(data, xhr){
if(xhr.args.node){
xhr.args.node.innerHTML = data;
}
}
 

And then we can reduce our dojo.xhrGet call by replacing the load argument:

 
dojo.xhrGet({
url: "title.php",
node: dojo.byId("title"),
load: loadIntoNode
});
 

Notice that we've reduced the information here to the lowest common denominators. Added is the the node field, which loadIntoNode is able to access through xhr.args.node. With just a few more lines of code, loadIntoNode could accept a dojo.query result as well.

We can even take this a step further and create a function that generates our dojo.xhrGet argument:

 
function intoNode(node, url){
return {
url: url,
node: dojo.byId(node),
load: loadIntoNode
};
}
 

Which leaves us with the super-compact, super DRY dojo.xhrGet call:

 
dojo.xhrGet(intoNode("title", "title.php"));
 

Changing Things Up

So now you're saying: "But library X lets me do it differently". And it probably looks a little like this:

 
$("#quote p").load("content.php");
 

Dojo's $ equivalent is dojo.query. And if that's not good enough for you, just set the variable $ to dojo.query.

Here, the important functional method comes from dojo.NodeList, the type of object dojo.query returns. It has a forEach function that accepts a function that is called for each found node. In practice, it looks like this:

 
dojo.query("#title p").forEach(function(node){ /* content */ });
 

If you've been following along, you should already know what to do here: pass it a reusable function! Our only problem is that this function is only called with a node, but it also needs a URL. How do we fix it? Well first of all, we need to have a function that accepts a URL. We also need this function, when called, to return a function that we can pass to forEach.

But what does this function look like?

 
function loadFromQuery(url){
var html = "";
dojo.xhrGet({
url: url,
sync: true,
load: function(data){
html = data;
}
});
return function(node){
node.innerHTML = html;
}
}
 

We've made this a synchronous dojo.xhrGet using the sync flag. What this means is that code execution will pause until the html variable is properly set and available to our returned function. It also means that any chained functions that occur after this function is used will be able to see the modified HTML.

So now you're saying: "I don't care if I can see the HTML or not, I don't want I/O slowing down my code!" and that's cool, because there's a solution. Let's rewrite our loadFromQuery function.

 
function loadFromQuery(url){
var get = dojo.xhrGet({ url: url });
return function(node){
get.addCallback(function(data){
node.innerHTML = data;
return data;
});
}
}
 

What's going on there?! Well, dojo.xhrGet returns something called a Deferred. The short and sweet of it is that it's an object you can attach callbacks to. If you attach a callback before the file finishes loading, the callback waits until it's done. But, if it's finished loading, the callback is made immediately. This allows our code to execute without having to pause and wait for the file to finish loading.

Also notice that the callback function returns the data it was originally given. This is because the next callback that gets added uses the return from the previous callback. So you'll want to make sure that's available.

So that's great, we have a function, but how do we use it? Well, like this:

 
dojo.query("#title p").forEach(loadFromQuery("script.php"));
 

And what I can't help pointing out at this point is where this approach wins out over the solution mentioned at the beginning of this section. The result of our loadFromQuery call is a function that we can save to a variable and pass around as much as we want. It will never have to make the Ajax call again or consult a cache. But I'll stop talking about it and show it in action.

 
var result = loadFromQuery("script.php");
dojo.query("#title p").forEach(result);
dojo.query("#section .subsection").forEach(result);
 

So there you have it. Because of Dojo's functional approach to code, you can mix and match things to suit your project, without any unnecessary code repetition.


Published Mar. 17, 2008— Reads 3,419 — Feedback 1
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About Neil Roberts
Neil Roberts

Add Your Feedback

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

#1
queZZtion commented on 15 Mar 2008

Functional Ajax with Dojo wrote: Does Dojo have an Ajax method that updates a div?


Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers

ADS BY GOOGLE

Breaking Java News
Fujitsu Announces Free Access to Its Enhanced Cloud BPM Platform for Solution Providers and Enterprise Teams
Fujitsu Interstage BPM Version 11 Lets Businesses Proactively "Sense and Respond" to Change
Money Worries put 75 per cent of Brits at Risk of Avoidable Sight Loss

ADVERTISE   |   MAGAZINE SUBSCRIPTIONS   |   FREE BREAKING-NEWSLETTERS!   |   SYS-CON.TV   |   BLOG-N-PLAY!   |   WEBCAST   |   EDUCATION   |   RESEARCH

.NET Developer's Journal - .NETDJ   |   ColdFusion Developer's Journal - CFDJ   |   Eclipse Developer's Journal - EDJ   |   Enterprise Open Source Magazine - EOS
Open Web Developer's Journal - OPENWEB   |   iPhone Developer's Journal - iPHONE   |   Virtualization - Virtualization   |   Java Developer's Journal - JDJ   |   Linux.SYS-CON.com
PowerBuilder Developer's Journal - PBDJ   |   SEO / SEM Journal - SJ   |   SOAWorld Magazine - SOAWM   |   IT Solutions Guide - ITSG   |   Symbian Developer's Journal - SDJ
WebLogic Developer's Journal - WLDJ   |   WebSphere Journal - WJ   |   Wireless Business & Technology - WBT   |   XML-Journal - XMLJ   |   Internet Video - iTV
Flex Developer's Journal - Flex   |   AJAXWorld Magazine - AWM   |   Silverlight Developer's Journal - SLDJ   |   PHP.SYS-CON.com   |   Web 2.0 Journal - WEB2
Apache   |   CMS   |   CRM   |   HP   |   Oracle Journal   |   Perl   |   Python   |   Red Hat   |   Ruby on Rails   |   SAP   |   SaaS

SYS-CON MEDIA:   ABOUT US   |   CONTACT US   |   COMPANY NEWS   |   CAREERS   |   SITE MAP
SYS-CON EVENTS:   |  AJAXWorld Conference & Expo  |  iPhone Developer Summit  |  Cloud Computing Conference & Expo  |  SOA World Conference & Expo  |  Virtualization Conference & Expo
INTERNATIONAL SITES:   India  |  U.K.  |  Canada  |  Germany  |  France  |  Australia  |  Italy  |  Spain  |  Netherlands  |  Brazil  |  Belgium
 Terms of Use & Our Privacy Statement     About Newsfeeds / Video Feeds
Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.
Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.
 
close this window