Blog

Blog posts made on 04-Jun-07
4Jun
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:

view plain print about
1<cfcomponent output="false">
2
3<cfset THIS.dsn="cfartgallery">
4
5    <!--- Get art description --->
6    <cffunction name="getArtDescription" access="remote" returnType="string">
7        <cfargument name="artid" type="numeric" required="true">
8
9        <!--- Define variables --->
10        <cfset var data="">
11        <cfset var result="">
12
13        <!--- Get data --->
14        <cfquery name="data" datasource="#THIS.dsn#">
15        SELECT description
16        FROM art
17        WHERE artid = #ARGUMENTS.artid#
18        </cfquery>
19    
20        <!--- Got it? --->
21        <cfif data.RecordCount IS 1>
22            <cfset result=data.description>
23        </cfif>
24
25        <!--- And return it --->
26        <cfreturn result>
27    </cffunction>
28
29</cfcomponent>

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

Now for the client side code:

view plain print about
1<!--- Get artlist --->
2<cfquery datasource="cfartgallery" name="art">
3SELECT artid, artname
4FROM art
5ORDER BY artname
6</cfquery>
7
8<!--- Display art list --->
9<cfform>
10<cfselect name="artid"
11        query="art"
12        display="artname"
13        value="artid"
14        size="10" />

15</cfform>
16
17<!--- DIV for description --->
18<cfdiv bind="cfc:art.getArtDescription({artid})"
19    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.

Read More ›

4Jun
2007
Ben Nadel On CFTHREAD

Ben Nadel has posted several really useful entries on the new <CFTHREAD> tag (Part I - Data Exchange, Part II - Parallel Threads, Part III - Set It And Forget It). Nice job, Ben!

Read More ›