Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion, Adobe Flex, AJAX & REA

ColdFusion: Article

An Introduction to AJAX and Taconite

Yes, it works

Incorporating Taconite and AJAX into your ColdFusion development is seamless. Typically, you do the following:

  1. Determine the point in your app where your user will kick off an AJAX request. Typically, this is a link or a button that, when clicked, calls a JavaScript function that starts the AJAX request process. This step may pass data to the JavaScript function that will be sent to the server for processing.
  2. Determine what area(s) on the page will be modified/updated on the client. An AJAX response can change the content in several locations on the client.
  3. Determine what server-side file should be called to process the AJAX request.
  4. Determine what business logic needs to happen inside the server-side file using the parameters passed from the AJAX request.
  5. Construct the response(s) you want to send back to the client page.
The beauty of AJAX, particularly Taconite, is in the ability to return multiple content blobs to your client. All that is needed is for the server-side file to know the IDs (contextNodeIDs) of the containers that will be updated. You can incorporate the IDs that will be updated in the AJAX request for a truly dynamic experience, or have the contextNodeIDs hard coded in the server file.

Below are examples of how I incorporated the steps above inside cfcPowerTools (www.cfcpowertools.com). cfcPowerTools is a Web-based application used to batch generate ColdFusion Components (CFCs) from database tables.

1.  Determine the point in your app where your user will kick off an AJAX request. The end user creates a project used to organize CFCs. The UI has a drop down for selecting a project. AJAX is used to load all of the Packages within a project and to clear out the main content area. The Project dropdown passes the projectID to the JavaScript function that initiates a Taconite call.

//GetPackages
01 function GetPackages(projectID)
02{
03 var url = "getPackages.cfm";
04 var packageRequest = new AjaxRequest(url);
05 packageRequest.setQueryString('projectID=' + projectID);
06 //packageRequest.setEchoDebugInfo();
07 packageRequest.sendRequest();
08}

The GetPackages JavaScript function accepts the projectID that will be appended to the URL. The URL is defined in line 3. The AJAX request is created in line 4. The projectID is appended to the query string in line 5. You can turn debug on/off using line 6. Line 7 sends the request.

2.  Determine which area(s) on the page will be modified/updated on the client. By selecting a Project, the Packages tab is updated and the main content area (white part) is cleared out. Below are the containers on the client awaiting content to be AJAX'd. Notice the ID values of each <div>:

<div id="zonePackages" class="leftNavDivs"></div>
<div id="zoneContent"></div>

3.  Determine which server-side file should be called to process an AJAX request. The GetPackages JavaScript function calls getPackages.cfm server file (line 3). This file is responsible for processing any business logic and assembling an AJAX response.

4.  Determine which business logic needs to happen inside the server-side file using the parameters passed from the AJAX request. Refer to Figure 3. Line 5 is the business logic. It simply sets a session variable using the passed projectID.

5.  Construct the response(s) you want to send back to the client page. Lines 8-12 construct the AJAX response. Line 8 replaces zonePackages with the contents of packageList.cfm. Line 12 clears out zoneContent.

Notice that the response is created within <cfsavecontent> tag (line 6). This allows you to better control the content being returned. The content is stored in variables.Content variable. Also, notice everything happens inside <cftry> block (line 4). This allows you to create an error message to be AJAX'd back to the client. Notice that inside <cfcatch> (line 14) another <cfsavecontent> (line 15) produces an error response (also named variables.Content). It returns content to an error div (<div id="zoneError"/>).

Regardless of whether an error happens, or the page successfully renders, content is returned to the client. Line 31 injects variables.Content into the Taconite response.

Notice the use of <cfcontent type="text/xml"> (line 26).

AJAX Tips
Here are some tips you may want to consider in your development:

1.  Custom debug functionality. I found debugging Taconite to be a bit cumbersome. I've added a Taconite response to each page:

<!--- SHOW DEBUG --->
<cfif request.ynDebug>
    <taconite-replace-children contextNodeID="zoneDebug" parseInBrowser="true">
    <cfmodule template="debug.cfm">
</taconite-replace-children>
</cfif>

I have a <div id="zoneDebug"/> on the client page. It sits there waiting to receive debug information. If debugging is turned on, then I include a custom debug page whose contents are returned to the debug div.

2.  AJAX requires the use of JavaScript. So, you will need to be sure your users allow JavaScript functionality in their browsers.

Conclusion
There are many layers to AJAX, but using just the basic functionality can greatly improve the richness of your application. Incorporating AJAX using the Taconite library is very easy. Following the steps and examples above you can inject AJAX functionality into your applications.

References
http://taconite.sourceforge.net/

More Stories By Tom Schreck

Tom Schreck is a Macromedia certified ColdFusion MX 6.1 developer. He has been working with ColdFusion since 1997. Check out www.cfcPowerTools.com for more information on cfcPowerTools.

More Stories By Jason Doyle

Jason Doyle is a Web systems engineer at Thomson. He is currently focusing on Rich Internet Applications with the use of mash-ups in a service-oriented environment.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.