YOUR FEEDBACK
Gregor Rosenauer wrote: well, not what's your take on this? Did I miss a second page of this article or...
AJAXWorld RIA Conference
Early Bird Savings Expire Friday Register Today and SAVE !..

SYS-CON.TV

2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
TOP THREE LINKS YOU MUST CLICK ON


AJAX: Making the HTML User Experience Almost As Pleasant as Flash
The main advantage of Flash, in spite of its vector animations, is that you never reload the page

AJAX can make the HTML user experience almost as pleasant as Flash. The main advantage of Flash, in spite of its vector animations, is that you never reload the page. Flash Remoting allows you to interface with the server in the background and AJAX does exactly the same for HTML pages.

In my previous article, "What's AJAX?" (CFDJ, Vol. 7, issue 9), I covered the basics of AJAX - everything from setting it up, all the way to having it running in an MVC design with basic functionality. Thus far, we have only sent and received simple objects, which is good way to understand the principle, but far from reality.

CFAJAX allows you to send complex objects, but for some reason it's extremely difficult to receive and interpret them on the ColdFusion side. For this reason I came up with a much simpler and straightforward way: WDDX serialization.

This concept may be familiar to some of you. WDDX stands for Web Development Data Exchange and Allaire created it in 2001 to solve problems in exchanging data between Web applications. In a nutshell, WDDX is a technology that facilitates exchanging complex objects over XML. WDXX supports Booleans, numbers, strings, date-time, arrays, structures, and record sets. Modules for WDDX support exist in various languages, including but not limited to ColdFusion, Perl, Java, JavaScript, ASP, .NET, and PHP.

A JavaScript WDDX component is included in the cfide/scripts folder of every ColdFusion installation. You can locate it at /cfide/scripts/wddx.js.

Complex Object Example
Now I'll show you how to use it. Listing 1 shows a simple example in which I created a complex object in JavaScript - a structure that contains an array of structures - and serialized it before sending it to ColdFusion. All you have to do is initialize a new WDDX serializer and serialize the complex object. You can see in Listing 1 how simple it is; you don't need to know the structure of the packet.

Notice the line "DWREngine._execute(_cfscriptLocation, null, ' wddxTest', oWddx.serialize(_o), testResult);". Here the first argument is the location of the ColdFusion model; the wddxTest is the function to be called; the argument is being serialized by our JavaScript component; and the testResult is the callback function.

Listing 2 shows how simple it is to receive the call. The function takes only one argument: the WDDX packet. This packet is not an object; it's actually an XML string. Listing 2 returns it intact just so you see exactly what's happening behind the scenes, and the callback function alerts the return packet. If you run my example, you'll see that the XML string is being alerted to the screen by the testResult function.

Trick 1
Here is a very important cross-browser note for you. Pay special attention to the URLDecode function in the ColdFusion listener (see Listing 2). I found that if you don't use it, the code will work just fine in IE, Firefox, and Netscape, but it will break in Safari for Mac users. I learned this the hard way, so here I am sharing the trick. For some reason, Safari URL-Encodes the XML packet before sending it and ColdFusion cannot decode it, making the argument impossible to de-serialize. Ideally, you'll test your code in as many environments as possible before you go live, so remember this and it may save you some debugging time.

Login Example
I'll now demonstrate a login example and some useful debugging techniques. Sometimes you'll find that the user is navigating your site and he is allowed to log in by using a form that is always available, either in the top or side navigation. Sometimes the user just posts a form, for example, a search products form, and then decides to log in. This may or may not be a problem, but if you allow him to post just the login form, you will lose all other existing form variables. One solution, you may be thinking now, is to loop through the form collection in the login form and create hidden fields; it may work in some cases, however, in other cases such as add-to-cart or checkout, resubmitting the form may cause duplicate process pages. Using cflocate after each process page is a good practice, but I am digressing and won't get into that.

How about adding some of the concepts we learned in this article? How nice would it be to let the user know if her username or password does not match any existing one without refreshing the page? Or even more! How about logging the user in, hiding the login form, and adding her login status without refreshing the page, and thus, without resubmitting, redirecting, or even losing focus of the current item she may be seeing.

This time we'll send the user and password to the ColdFusion model and expect an array for the response. The array will have a "success" status: in case of error, a message; and in case of success, the name of the user. We could also pass a structure, but you'll find that if you pass a structure back, JavaScript will interpret it as an array or keys and values. Then you'll have to loop through the array and assign the values to the keys. An alternative could be using WDDX, but this time to encode the packet before sending it back to JavaScript and use the WDDX component in JavaScript to deserialize it.

Sometimes it's not that easy to know exactly how the structure that ColdFusion sent is being interpreted by JavaScript. In these cases, I always refer to the same debugging method: I loop over the entire scope or collection to find what is being sent back. Listing 3 shows a simple one-line loop that will do the trick. Try to return a structure and see for yourself the way it's being passed back.

Let us continue with our login example. The ColdFusion model will return an array of two elements: array[1] will be the success variable (true or false), and array[2] will be the error message in case of an error, or the name of the user in case of success. For the sake of this article, the model just checks for a static user and password; however, this is exactly where your login logic should be. The trick is to update the session in the model in the background and just send back to JavaScript what it needs for the presentation layer. For instance, you could add the User-ID for the session, and send back the name and last name so it can be displayed in the left navigation.

After the user successfully logged in, we will make the login form disappear and add a simple message that reads "logged in as %name%". We will do this by using the JavaScript innerHTML function. The innerHTML function lets you modify the source code on the fly. Our form was placed inside a DIV layer that is used as a holder and, after the login, we will change the code of the holder to display the new status. When using this method, make sure that the navigation does not have to change upon login, or, if it does, you may modify the navigation too by using innerHTML as well. If you code in an object-oriented fashion, your navigation would be generated by an object you may call in the login function and send the generated HTML back to the JavaScript controller, which will replace the existing one by using innerHTML functions.

Content Example
Another great advantage of AJAX is that it improves load time. For example, imagine a typical site: it has a design frame and content in the center area. Every click has to reload the entire frame to update the content. Ideally, you wouldn't have to transfer the code for the frame and download any of its graphic components again. Caching certainly helps, but AJAX can make it even faster. Listings 4 and 5 show a simple example in which the content is being generated in the ColdFusion model and passed back to the JavaScript controller. For simplicity, in this example I actually added the content directly in the Model.cfm page. In real-life examples, the ideal way would be to actually include a view template with a cfinclude inside the cfsavecontent tag; by doing that your views will not depend on the AJAX component. Perhaps you won't see a huge speed difference just by running this example; this is due to the fact it doesn't have any design elements. As an experiment, try to add a full design frame to my example with an average page weight of 100k or 200k in graphics and compare load times.

About Rob Gonda
Rob Gonda is the CTO for iChameleon Group and Contributing Editor to AJAXWorld Magazine. He is an Advanced Certified Coldfusion Developer, member of the Adobe Community Experts, frequent contributor to the CFDJ and ADJ, frequent speaker at IT and developer conferences nationwide, co-author of Real-World Ajax Book, author of ajaxCFC, holds a BS in computer science and engineering, an MBA with a specialization in entrepreneurship, and he specializes in Rich Internet Applications and object-oriented architecture. You can reach him at rob[at]robgonda[dot]com and read his blog is at http://www.robgonda.com

YOUR FEEDBACK
Dougal Mathers wrote: I cannot seem to get the table example to work and am wondering if you could help explain a bit more around it as with the other examples? Great article to get people started. Many thanks,Dougal.
Thierry Nivelet wrote: You may be interested in our catalog multi-criteria search engine http://www.intuicat.com/IntuiCatAjaxDemo.asp Interesting enough is that all presentation code lies on server, client merely executes change orders sent by server.
SYS-CON Belgium News Desk wrote: AJAX can make the HTML user experience almost as pleasant as Flash. The main advantage of Flash, in spite of its vector animations, is that you never reload the page. Flash Remoting allows you to interface with the server in the background and AJAX does exactly the same for HTML pages.
SYS-CON India News Desk wrote: AJAX can make the HTML user experience almost as pleasant as Flash. The main advantage of Flash, in spite of its vector animations, is that you never reload the page. Flash Remoting allows you to interface with the server in the background and AJAX does exactly the same for HTML pages.
Rob Gonda wrote: by the way, check out my latest alternative to cfajax. I developed an ajax CFC framework. check it out at http://www.robgonda.com/blog/projects/ajaxcfc/
Jim @ The ZK Project wrote: >> First of all, I really disagree on rising cost of developing AJAX applications. Adding an AJAX feature means an extra task, no matter how simple it is. It gets worse when we add more codes to the client. It means you might have to replicate business logic to clients. It also means you have to maintain two copies of codes. >> It definitely helps with dhtml and look and feel, but it does not automate AJAX at all, it makes it actually >> more difficult. It seems like that in order to make it work you need to add your Java server-side code >> within your view. That will make maintenance really tedious. Whether to embed codes in the view is up to developers, not the framework itself. It is designed to speed up prototyping and customization. If MVC or other design patterns are required, developers need only to provide a map between components to the real class they want. On...
Rob wrote: BTW, for more info please visit my blog at http://www.robgonda.com/blog
Rob wrote: Jim, First of all, I really disagree on rising cost of developing AJAX applications. Au contraire, they are going down as more and more developers get into it. Although I usually do not check seeding or spam, I decided to check the ZK project. It definitely helps with dhtml and look and feel, but it does not automate AJAX at all, it makes it actually more difficult. It seems like that in order to make it work you need to add your Java server-side code within your view. That will make maintenance really tedious.
Jim @ The ZK Project wrote: As AJAX gives Web applications a fresh look, the rising cost of developing AJAX applications is challenging its way to success. That is the reason we founded the ZK project ( http://zk1.sourceforge.net ): make AJAX transparent to app developers. ZK is an Open Source and Live Demo at http://www.potix.com/zkdemo/userguide. ZK has the following characteristics. * XUL-based Components. * Event Driven Model. * Server-Centric Processing. * Script in Java and EL Expressions.
Rob wrote: Kevin, good call. I usually do, but with the rush of getting the article out it slipped... Thanks for pointing it out.
Rob wrote: By the way, can someone please fix this feedback tool?! I won't take 9 out of 10 posts I submit.
Rob wrote: Kevin, good call. I usually do, but with the rush of getting the article out it slipped. Thanks for pointing it out.
Kevin Penny wrote: Excellent - you should scope your cfc vars however - makes it alittle easier to understand what vars come from where (arguments)
MissedOut? wrote: I've still yet to build my first AJAX request. Does that mean I'm too late to the party?
newbie wrote: So is AJAX a download or is it a technique? Pafrdon my ignorance...only now I am seeing AJAX, AJAX, AJAX everywhere but I don't really know what it *is* yet
newbie wrote: So is AJAX a download or is it a technique? Pafrdon my ignorance...only now I am seeing AJAX, AJAX, AJAX everywhere but I don't really know what it *is* yet
ByeBye HTML wrote: >> AJAX is extremely fast, secure, and allows >> for advanced functionality that >> synchronous HTML does not. No wonder this is the hottest technology on earth right now. HTML is Dead!
LATEST AJAXWORLD RIA STORIES
Cloud Computing isn’t just another buzzword: this session will look at what the industry is up to, Amazon is up to, and especially how people are innovating in the cloud. Buzzwords aside, virtualized (cloud) computing is a disruptive game changer at both technical and business ...
The Dojo Toolkit is an open-source JavaScript toolkit that has a large community following in and out of the Enterprise. One of the many useful aspects of Dojo is the ability to extend the toolkit to incorporate new functionality. dojo.E is a set of extensions for dojo 1.1 that m...
Using AJAX and Comet, this presentation walks through the process of creating a simple tic-tac-toe game in which two people play while other people can watch the game via their browser. The session involves creating a simple game playable in one session and then stepping through ...
Web applications are accessible on smart phone, TV, desktop, your home office or in your conference room. They have become common decision aids for our personal and business meetings. Situational Applications provide rich information and data visualization aids for decision-orien...
The iPhone has brought the unification of the Desktop and Mobile Web, but there are still a number of challenges in bringing AJAX applications to mobile devices, including reduced bandwidth, increased latency, reduced screen size, and limited battery life. This session provides a...
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