Saturday, May 17, 2008    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Jun 2007 >>
S M T W T F S
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
             

Search

Categories
 • Adobe (61) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (6) [RSS]
 • AIR (96) [RSS]
 • Appearances (105) [RSS]
 • Books (66) [RSS]
 • CFEclipse (14) [RSS]
 • ColdFusion (1081) [RSS]
 • Flash (91) [RSS]
 • Flex (319) [RSS]
 • Jobs (81) [RSS]
 • JRun (12) [RSS]
 • Labs (27) [RSS]
 • LiveCycle (12) [RSS]
 • MAX (141) [RSS]
 • Regular Expressions (12) [RSS]
 • SQL (36) [RSS]
 • Stuff (492) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (131) [RSS]
 • Wireless (96) [RSS]

Other BLOGs
 • Ray Camden
 • Tim Buntel
 • Sean Corfield
 • John Dowdell
 • Steven Erat
 • Brandon Purcell
 • Charlie Arehart
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
June 4, 2007

ColdFusion Ajax Tutorial 4: Partial Page Updates

Ajax type applications allow developers to update parts of a web page without needing to refresh the entire page. Click on a link and another area of the screen is updated, click a toggle arrow to expand and collapse an inline detail box, submit a form and receive confirmation without updating the page, and so on.

This type of interaction uses asynchronous HTTP calls back to the server (to get data, or process form submissions, and so on) and client side JavaScript to update specific parts of a page (usually defined using <DIV> tags).

ColdFusion 8 makes this type of interaction very simple by allowing controls to be bound to other controls, so that when one control changes (or an event occurs) a second control may be updated by making an asynchronous call to ColdFusion on the server.

To demonstrate this, here is a simple example. A list of art items is displayed in a <SELECT> control, and clicking on any item updates a description below it. The list of descriptions are not in the client, rather, when an art item is selected the following CFC method is invoked on the server:

<cfcomponent output="false">

<cfset THIS.dsn="cfartgallery">

   <!--- Get art description --->
   <cffunction name="getArtDescription" access="remote" returnType="string">
      <cfargument name="artid" type="numeric" required="true">

      <!--- Define variables --->
      <cfset var data="">
      <cfset var result="">

      <!--- Get data --->
      <cfquery name="data" datasource="#THIS.dsn#">
      SELECT description
      FROM art
      WHERE artid = #ARGUMENTS.artid#
      </cfquery>
   
      <!--- Got it? --->
      <cfif data.RecordCount IS 1>
         <cfset result=data.description>
      </cfif>

      <!--- And return it --->
      <cfreturn result>
   </cffunction>

</cfcomponent>

This simple method accepts an artid and returns the art description as a string.

Now for the client side code:

<!--- Get artlist --->
<cfquery datasource="cfartgallery" name="art">
SELECT artid, artname
FROM art
ORDER BY artname
</cfquery>

<!--- Display art list --->
<cfform>
<cfselect name="artid"
      query="art"
      display="artname"
      value="artid"
      size="10" />

</cfform>

<!--- DIV for description --->
<cfdiv bind="cfc:art.getArtDescription({artid})"
   style="background-color:grey; color:white; height:100; width:200" />

First the art list is retrieved (and yes, this is a bad example, the query should have been in the CFC too, not in the client, but I wanted to keep this example as simple as possible). <CFSELECT> is used to display the list of art items. And then <CFDIV> is used to define a <DIV> and to provide a binding. This <CFDIV> points to the previous CFC method, and passes {artid} (the name of the <SELECT> control) as an argument. This way, whenever the selection in the <SELECT> changes, the binding is fired, the CFC method is invoked, and the display is updated with the returned string.

TrackBacks
There are no trackbacks for this entry.

No trackback URL. Trackbacks are only allowed via interactive form.

Comments
This is an extremely cool new tag. I blogged about it this morning, with a different type of example, along with a screencast that is up on Youtube.

http://samfarmer.instantspot.com/blog/

http://www.youtube.com/watch?v=whjPKSqPl6c
# Posted By Sam Farmer | 6/4/07 6:12 PM
Does anyone have an example of an expandable directory tree done in Ajax?
# Posted By Gary Funk | 6/4/07 9:17 PM
Gary, I am working on that for one of the next posts, server-side directory tree browsing using an Ajax <CFTREE>.

--- Ben
# Posted By Ben Forta | 6/4/07 9:21 PM
How did I guess that. While your at it, can you show us how to rewrite Major BBS, PCBoard, TBBS and Wildcat so those of us that remember those days, can go back to them.
# Posted By Gary Funk | 6/4/07 10:39 PM
How does this work when Javascript is disabled or not available? The same question applies to all the new cfajax functionality. Thinking here about the accessibility of pages; is the information still available to people using text readers etc, or do we need to roll our own accessible pages using javascript detection etc?
# Posted By duncan | 6/5/07 5:56 AM
Gary, that tree example was just posted.

Duncan, not very well, and that's a hole with any and all Ajax stuff, JavaScript is an absolute requirement. Fortunately that's not as big an issue nowadays, but if it is a concern for your user base, then you'd be best off avoiding anything Ajax.

--- Ben
# Posted By Ben Forta | 6/5/07 10:04 AM
In the documentation that ships with the beta - the cfdiv tag has an example that uses <cfdiv source="page.cfm?urlvar={something}" /> but when I try to use run it I get an error stating 'required attribute 'bind' not provided' (and I tried putting the page source as the bind attribute but that didn't work). So how can I do this - is the documentation wrong (pg 887 of the downloadable pdf file).
# Posted By Cory Miller | 6/13/07 4:17 PM
After a couple of frustrating hours of debugging, I found a problem with binding to a CFC.

This problem happens when you are using the Application.cfc method onRequestEnd() to display content.

Suppose my Application.cfc onRequestEnd() method looks like this:

<cffunction name="onRequestEnd" output="true" returnType="void" description="Runs at the end of a request, after all other CFML code">
   <cfargument name="targetPage" type="string" required="yes" />

   THIS IS HTML CONTENT in Application.onRequestEnd<br>
</cffunction>

The return from CFC invocation in the bind argument will ALSO return content from the onRequestEnd() method.

I'm working on some code that will display a list of state abbreviations from a list of selected states (checkboxes).

*** IMPORTANT ***
CF8 suggests that you append "cfdebug" to the URL for debugging ajax calls. I wish that I had noticed this before it would have saved me at least 90 minutes.
*** IMPORTANT ***

After appending "cfdebug=1" to the URL, I noticed that it can display the calls to the CFC in real time. This is an awesome debugging tool. The following is an example of what was returned when the onRequestEnd() method used the snippet above:

info:http: CFC invocation response: "KY, ND" THIS IS HTML CONTENT in Application.onRequestEnd

Now see what it looks like when Application.onRequestEnd() does nothing:

info:bind: Assigned bind value: 'KY, ND' to StateProvinceList.innerHTML
info:http: CFC invocation response: "KY, ND"

Notice that when there is content in onRequestEnd() that the bind event never occurs. It just gets as far as the CFC invocation response.

So, if you are using onRequestEnd() to display debugging info or other content, I would recommend moving this code to the end of onRequest() or find some other place for it.
# Posted By Scott Jibben | 9/29/07 7:52 AM
I'm trying to run this example and I'm getting th efollowing error:
The ARTID argument passed to the getArtDescription function is not of type numeric.
# Posted By Victor | 10/14/07 9:27 PM
OK, I have fixed the erorr by adding the selected atribute to the cfselect.
Now there is no error but no description is displayed either.

What I'm doing wrong?

Thx
# Posted By Victor | 10/15/07 3:59 PM
FIX IT. The color of the text that was specified in the <cfdiv was white so it was actually displayed but I could not see it,

Thx
# Posted By Victor | 10/15/07 8:42 PM
Can you bind a CFDIV to a cfc with returntype'="query"?

With my setup, using returntype=string, I get just one record, and I should get many.

When I try to use returntype="query" in my cfc I just get "[object Object]" in my div. ??
# Posted By Preserved | 11/8/07 1:32 PM

  © Copyright 1997-2008 Ben Forta, All Rights Reserved