I recently discovered the solution to a problem I was having relating an ActionScript class to a ColdFusion CFC. To troubleshoot the problem, I created a very simple test application. In the test application, I have a Person CFC and a PersonServices CFC. The PersonServices CFC has methods that return a single Person object and an array of Person objects. On the Flex side, I have a Person ActionScript class that matches up with the Person CFC.
Initially, during my testing, the object returned by the CFC function was not being automatically cast to the correct ActionScript class type in Flex even though I had created the matching ActionScript class. Rather the type returned by the CFC was only recognized as a generic Object in my Flex application (instead of the Person ActionScript type it should have been).
After some troubleshooting, I learned that you must reference the ActionScript class (declare a variable of that ActionScript type) in your MXML to ensure the Flex compiler includes the ActionScript class in the SWF. If you don't include a specific reference to the ActionScript class then apparently that ActionScript class will not be part of the compiled SWF. If the ActionScript class is not part of the compiled SWF, then when you call your CFC function and get a specific type back, the SWF cannot automatically cast that return value to the correct type.
To force my Person.as to be part of the compiled SWF I included this line in my test Flex application:
/*ensure our Person.as class is
compiled into the SWF
otherwise the array of Person objects
returned by the
CFC will be treated as generic objects
private var aPerson:Person ;
Note, I don't use the aPerson object anywhere in my test. However, that one line forces the Flex compiler to include the Person ActionScript class in the SWF. So when I get a Person object back from calling my CFC function, my Flex application can cast it to the ActionScript Person class.
You can view an example application that demonstrates this test (right click on the left panel to view all the source code). The panel on the left of the example includes the above code. The panel on the right of the example does not. That is the only difference between the code for the two SWFs. If you click on the button in the left panel, you'll see the results returned are properly cast to the Person ActionScript type. If you click on the button in the right panel, you'll see the results returned are recognized only as a Generic object.
My mistaken assumption was that if the ActionScript class I created was in the same folder as my MXML application, then the Flex compiler would automatically include it in the SWF. I thought I best post a blog entry about this issue in case anyone else runs into this problem when troubleshooting a Flex to CFC problem and to remind myself about the correct way to test a Flex to CFC application.
For background on relating an ActionScript class to a CFC and other problems I've encountered see:
- Exchanging User Defined Objects Between ColdFusion and Flex
- Exchanging an Array of Objects Between CF and Flex and Updating Objects in a DataGrid
- Solving A Problem In Relating An ActionScript Class To A ColdFusion CFC
- Don't Use Delete for a CFC function Name if You Want to Call The Function From Flex