The Phone Selector application demonstrates some important concepts and techniques, which are outlined in the following sections.
The ColdFusion code consists of the following components and function:
<cfproperty> tag to list each property in this CFC. The properties, their names and types, and their exact order are extremely important. If these do not match the properties in the equivalent ActionScript file, mapping does not occur (and when the array phones arrives in Flex it is an array of generic objects instead of an array of Phone objects).<cfinvoke> tag to create and populate a Phone.cfc (which is returned and appended to the array). The <cfinvoke> tag uses a fully qualified path to Phone.cfc, even though it is in the same folder. (In fact, component="Phone" would work). This, too, is to facilitate CFC to ActionScript class mapping. The fully qualified path is embedded in the array that is sent to the Flex application; it must match the path specified in the equivalent ActionScript class.getPhones(): The getPhones() function is the only function invoked from Flex. As a result, it must specify access="remote". Only CFC methods with this access level can be invoked through Flash Remoting.The Flex code includes the following files:
Main.mxml:
<mx:RemoteObject> tag specifies to Flex how to get to the ColdFusion Catalog.cfc file. The source is fully qualified, and the destination is ColdFusion. This generic destination makes it unnecessary to map specific endpoints in Flex XML configuration files.<mx:Panel> tag with a <mx:ControlBar> tag at the bottom.<mx:Application> tag contains creationComplete="svc.getPhones()", which specifies that the application automatically invokes the ColdFusion getPhones() function as soon as the application is loaded and ready to use.<mx:RemoteObject> tag. It creates an array that contains the data and uses that array to populate the phones variable.Phone.as:
[RemoteClass(alias="CFIDE.samples.Phones.CF.Phone")]<cfinvoke> syntax in Catalog.cfc, which is how the object types are automatically matched to each other.toString() method. (It is a good idea for every class you create to have a toString() method). In this simple example there are no other methods; however, there could be, just as the equivalent Phone.cfc has extra functions.FeatureFormatter.as:
<cfif> tags to determine whether to display values, and how to display them. This is a little more complicated in a Flex application; it is a custom formatter.format() method builds the string, starting with the two highlight points and conditionally appending additional text.ProductDetails.mxml:
phone of type Phone is defined. This is passed in Main.mxml.Flash Remoting is the most efficient way to pass data between ColdFusion and client-side Flex applications. An important part of the new Flash Remoting adapter is support for automatic CFC to ActionScript object mapping. This requires careful calling conventions, explicit property definitions, and a RemoteClass definition within the ActionScript class. Once these are in place, passing complex data back and forth becomes very easy, and object types are retained (making any CFC functions or ActionScript class methods usable).
For more information, visit the Flex and ColdFusion topic page in the Flex Developer Center.
Using ColdFusion with Flex – Part 4: Creating and running a session tracker