Tutorials

ColdFusion, WDDX, and Flash (Page 3 of 8)

THE COLDFUSION SIDE

In your code editor of choice, ColdFusion Studio in my case, open up the qry_getEmployees.cfm template.

1. <cfsetting enablecfoutputonly="Yes" showdebugoutput="No">
2. <cfheader name="Expires" value="##NOW()##">
3. <cfsilent>
4. 	<!--- query the database and retrieve the employees --->
5. 	<cfquery name="GetEmployees" datasource="Employees">
6.		SELECT EmployeeID, FirstName, LastName, PhoneExtension
7.		FROM Employees
8.		ORDER BY EmployeeID
9.	</cfquery>
10.	<!--- serialize the query result-set into a WDDX packet --->
11.	<cfwddx action="CFML2WDDX" input="##GetEmployees##" output="myWDDXPacket">
12. </cfsilent>
13. <cfif GetEmployees.RecordCount neq 0>
14. 	<cfoutput>##myWDDXPacket##</cfoutput>
15. </cfif>
16. <cfsetting enablecfoutputonly="No" showdebugoutput="No">
	

As you can see there are very few lines of code to retrieve the information we need and only a few more to get the information into a valid WDDX packet. Let's go through the code. The first line uses a tag called CFSETTING. CFSETTING is used to control various aspects of page processing giving you more control over your application. The ENABLECFOUTPUTONLY attribute, when set to YES, blocks output of all HTML that resides outside of CFOUTPUT tags. Since we are creating a WDDX packet and since Flash is picky over whether this packet has extra whitespace, extra lines and so forth, we want to be sure that we get ONLY the XML output generated from the serialization. On line 2, the CFHEADER tag is used to generate a custom HTTP response header on the client machine. Setting the EXPIRES attribute to the CF server time of NOW() will ensure that this ColdFusion page is never cached in the clients browser. On line 3 you will see the opener tag for CFSILENT. This tag suppresses all output that is produced by the ColdFusion server. It's pretty much overkill, but if we had a CFOUTPUT statement within the CFSILENT block the output would not be displayed in the clients browser. It's basically another way to ensure that nothing gets displayed on the client screen at any time (other than Flash related display).

The next couple of lines are the SQL required to retrieve our employee information from the database. Line 11 is the workhorse in this template. The CFWDDX tag allows you to serialize data into a WDDX packet and it also allows you to deserialize WDDX packets. We're sending the tag our query (GetEmployees), telling ColdFusion to translate this data into an XML WDDX packet and to call the created packet myWDDXPacket. Lines 13-15 have some conditional processing which says: "If at least one employee was found in the database, display the WDDX packet to the screen. If zero employees were returned, do nothing." Since our Flash movie is going to call this template, writing the packet to the screen will allow Flash to read the packet in. The last line of code resets our page processing allowing any HTML to display in the browser.

PREVIOUS | NEXT