Chapter 15: Interacting with Remote Servers
245
See the CFML Language Reference for more information on JavaScript objects.
Converting CFML Data to a JavaScript Object
The following example demonstrates the transfer of a CFQUERY result set from a
CFML template executing on the server to a JavaScript object that is processed by the
browser.
The application consists of five principal sections:
•
Running a data query
•
Including the WDDX JavaScript utility classes
•
Specifying the conversion type and the input and output variables
•
Calling the conversion function
•
Outputting the object data in HTML
This example uses a registered ColdFusion datasource and can be run from
ColdFusion Server.
<!--- Create a simple query --->
<CFQUERY NAME = ’q’ DATASOURCE =’snippets’>
SELECT Message_Id, Thread_id,
Username, Posted from messages
</CFQUERY>
<!--- Cache the JavaScript so that subsequent requests will --->
<!--- use the cached version rather than making additional --->
<!--- requests to the server --->
<SCRIPT LANGUAGE="JavaScript"
SRC="/CFIDE/scripts/wddx.js"></SCRIPT>
<!--- Bring in WDDX JS support objects
A <SCRIPT SRC=></SCRIPT> can be used instead
wddx.js is part of the ColdFusion distribution --->
<CFINCLUDE TEMPLATE=’/CFIDE/scripts/wddx.js’>
<!--- Use WDDX to move from CFML data to JS --->
<CFWDDX ACTION=’cfml2js’ input=#q# topLevelVariable=’q’>
Comparison of JavaScript object and deserialized XML
JavaScript
CFML
x = new Object();
x.a = "Property a";
x.b = "Property b";
x = structNew();
x.a = "Property a";
x.b = "Property b";
Summary of Contents for COLDFUSION 4.5-DEVELOPING WEB
Page 1: ...Allaire Corporation Developing Web Applications with ColdFusion ColdFusion 4 5...
Page 14: ...xiv Developing Web Applications with ColdFusion...
Page 26: ...xxvi Developing Web Applications with ColdFusion...
Page 34: ...8 Developing Web Applications with ColdFusion...
Page 70: ...44 Developing Web Applications with ColdFusion...
Page 84: ...58 Developing Web Applications with ColdFusion...
Page 114: ...88 Developing Web Applications with ColdFusion...
Page 148: ...122 Developing Web Applications with ColdFusion...
Page 174: ...148 Developing Web Applications with ColdFusion...
Page 208: ...182 Developing Web Applications with ColdFusion...
Page 244: ...218 Developing Web Applications with ColdFusion...
Page 274: ...248 Developing Web Applications with ColdFusion...
Page 288: ...262 Developing Web Applications with ColdFusion...
Page 300: ...274 Developing Web Applications with ColdFusion...
Page 350: ...324 Developing Web Applications with ColdFusion...
Page 362: ...336 Developing Web Applications with ColdFusion...