ColdFusion Struct to Flex Untyped Object Gotcha

Flex and ColdFusion work great together...most of the time. Between the two application frameworks data transformation works really well. For instance, when you pass a ColdFusion query object to Flex, Flex sees it as an ArrayCollection. If you pass an Flex ArrayCollection, ColdFusion automatically interprets it as a ColdFusion query object.

This works great in almost all cases. Something to watch out for when sending ColdFusion structs to Flex is that if you use the "dot-notation" of defining the keys, then in Flex when referencing the keys, the key name must in all caps.

// ColdFusion
myStruct.firstName = "Kyle";
// Flex trace(myCFStruct.FIRSTNAME);

On the other hand, if you use bracket notation the case follows that of what is inside the quotations.

// ColdFusion
myStruct["FirstName"] = "Kyle";
// Flex trace(myCFStruct.FirstName);

TrackBacks
There are no trackbacks for this entry.

Trackback URL for this entry:
http://www.kylehayes.info/blog/trackback.cfm?8D644998-1143-DBC8-4BAC469EEB743D89

Comments
You can use

<force-struct-lowercase>true</force-struct-lowercase>

block of services-config.xml file to force CF to pass struct keys in lowercasce.
# Posted By Scott Stroz | 5/14/07 9:28 PM
Heh, yes you can. A point I meant to mention but forgot to, thanks, Scott!
# Posted By Kyle Hayes | 5/14/07 9:31 PM
"If you pass an Flex ArrayCollection, ColdFusion automatically interprets it as a ColdFusion query object."

How do you use this Coldfusion query object on the coldfusion side?

Thanks
# Posted By morezy@gmail.com | 9/13/07 3:10 PM
@morezy: Simply like any other ColdFusion Query object that you would get from a database call. For instance:

<table>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<cfoutput query="myQueryFromFlex">
<tr>
<td>#myQueryFromFlex.name#</td>
<td>#myQueryFromFlex.email#</td>
</tr>
</cfoutput>
</table>
# Posted By Kyle Hayes | 9/13/07 3:32 PM
//My Flex code:

var variables:URLVariables = new URLVariables();
variables.QueryFromFlex = ACprosResult; // ACprosResult = Flex ArrayCollection
      
//Setup a new request and sending the data through a post
var u:URLRequest = new URLRequest("http://mywebsite.com/displayFlexQuery.cfm");
u.data = variables;
u.method = URLRequestMethod.POST;
//Navigate to the displayFlexQuery.cfm
navigateToURL(u,"_blank");

When I display the content of the Flex query, I have

//Alert.show(ObjectUtil.toString(ACprosResult));
(mx.collections::ArrayCollection)#0
filterFunction = (null)
length = 1
list = (mx.collections::ArrayList)#1
length = 1
source = (Array)#2
[0] (Object)#3
FIRST_NAME = "John"
mx_internal_uid = "776493BC-342A-5F09-1ABA-042655F5D74"
LAST_NAME = "Smith"
uid = "BC170492-EAA0-57S1-1731-0426624F18B7"
sort = (null)
source = (Array)#2

On displayFlexQuery.cfm file, I have:

<table>
   <tr>
      <th>First Name</th>
      <th>Last Name</th>
   </tr>
   <cfoutput query="QueryFromFlex">
      <tr>
         <td>#QueryFromFlex.FIRST_NAME#</td>
         <td>#QueryFromFlex.LAST_NAME#</td>
      </tr>
   </cfoutput>
</table>

Coldfusion throw this error:
The value of the attribute query, which is currently "[object Object]", is invalid.

Any ideas?
Thanks
# Posted By morezy@gmail.com | 9/14/07 6:28 AM
Sorry for being unclear. I left out an important factor. This ArrayCollection -> Query Object transformation is only possible through a RemoteObject call in Flex (thus using the binary transmission of data versus a web service call or something similar.
# Posted By Kyle Hayes | 9/14/07 6:35 AM
I am in a bit of a jam trying to figure out how
to get an array created in my cfc to populate a
lineChart. The problem I am having is flex
does not recognize the data type. I have tried
array and struct. The data is an array with
structure. I have posted the code to actionscript.orf
http://www.actionscript.org/forums/showthread.php3...
any sugestions as to how to make this work or
I am doing wrong would be great
# Posted By dave | 2/13/08 11:23 PM
Hey Dave,

Try replacing in your result text:
ArrayCollection(e.result);

with

ArrayCollection(e.result as Array);
# Posted By Kyle Hayes | 2/14/08 9:24 AM
@Dave - Something else I just noticed is that you are declaring your ArrayCollection twice. Which I am surprised Flex is even allowing you to do:

[Bindable]
public var dp:ArrayCollection = expenses;

[Bindable]
public var dp:ArrayCollection;
# Posted By Kyle Hayes | 2/14/08 9:35 AM
I'm in a struggle with my arrayCollection and "[object Object]" also. Any other insights to this? Is this a problem with services-config.xml? I can see datagrids fine.
# Posted By MnM | 2/22/08 8:49 PM
@MnM - What does your ActionScript result handler look like?
# Posted By Kyle Hayes | 2/23/08 8:36 AM