Tags

ColdFusion Struct to Flex Untyped Object Gotcha

by Kyle Hayes on May 14th, 2007

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);

From Uncategorized

13 Comments
  1. 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.

  2. Heh, yes you can. A point I meant to mention but forgot to, thanks, Scott!

  3. morezy@gmail.com permalink

    "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

  4. @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>

  5. morezy@gmail.com permalink

    //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

  6. 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.

  7. 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?t=160828
    any sugestions as to how to make this work or
    I am doing wrong would be great

  8. Hey Dave,

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

    with

    ArrayCollection(e.result as Array);

  9. @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;

  10. MnM permalink

    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.

  11. @MnM – What does your ActionScript result handler look like?

  12. Hi Dave,

    Looks like you have some experience with arraycollections and CF. I’m currently trying to pass an arraycollection to my cfc. I figured I should set the argument type as ’struct’. Unfortunately, I get an error telling me that my my arraycollection is not of type struct. Setting it to array doesn’t work either. Any thoughts?

    Thanks.

    Novian

    Code:

    <cffunction name="saveQuiz" access="remote" returntype="boolean" hint="Saves new quiz to quiz_questions.">
    <cfargument name="quizQuestion" type="string" required="yes">
    <cfargument name="quizAnswer" type="string" required="yes">
    <cfargument name="optionsAC" type="struct" required="yes">
    <cfset var qRead="">
    <cfquery name="savedQuizQuestion" datasource="my_db" result="savedQuizID">
    INSERT into quiz_questions (quizQuestion, quizAnswer)
    VALUES (’#ARGUMENTS.quizQuestion#’, ‘#ARGUMENTS.quizAnswer#’)
    </cfquery>
    <cfloop from="1" to="#ArrayLen(optionsAC.optionsArray)#" index="i">
    <cfquery name="savedQuizOptions" datasource="my_db">
    INSERT into quiz_options (quizID, optionName, correct)
    VALUES (’#savedQuizID.GENERATED_KEY#’, ‘#optionsAC.optionsArray[i].label#’, ‘#optionsAC.optionsArray[i].data#’)
    </cfquery>
    </cfloop>
    <cfreturn true>
    </cffunction>

  13. Did you guys get this figured out? I have the same problem with asp.net.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS