An important benefit of the new and improved Flash Remoting adapter (installed with the Flex updater for ColdFusion MX 7.01, and used in conjunction with Flex 2) is the automatic conversion of objects between ColdFusion and Flex – CFCs mapped to ActionScript classes, and vice-versa.
For example, suppose you had a User.cfc in ColdFusion and a User.as in Flex. If you returned the User ActionScript class to ColdFusion what you’d get on the ColdFusion end is an instance of User.cfc. And when a Flash Remoting call returns a User.cfc (or perhaps an array of them), what you’d end up with in Flex is a User class (or an array of them).
This conversion is automatic, but there are some very specific requirements that you must meet (miss any of them any the data sent back and forth will be a generic object):
- Both the CFC and the ActionScript class must have the exact same properties (same name, same type, and same order). And yes, this does mean that you’ll need to use the
tag in your CFC. - The full path to the CFC is needed, and this means that you must instantiate it using a fully qualified dot notation path (even if the CFC is in the current directory where just the CFC name would usually suffice).
- The ActionScript object must be associated with the CFC using the [RemoteClass] (which expects the fully qualified path to the CFC).
The following snippets from an example application I posted demonstrate this. This first snippet is from Phone.cfc. Notice that it defines 10 properties (the code also contains over 20 methods which are not included here):
THIS.name="";
THIS.description="";
THIS.price=0;
THIS.image="";
THIS.series="";
THIS.triband="false";
THIS.camera="false";
THIS.video="false";
THIS.highlight1="";
THIS.highlight2="";
...
This next snippet is the code that instantiates Phone.cfc for returning back to Flex. The fully path to the CFC is specified (although not needed for ColdFusion itself, as the files are in the same directory):
This next snippet is the Phone class (file Phone.as). [RemoteClass] maps it to the appropriate CFC, and it contains the exact same 10 properties as Phone.cfc. (It does not have the same methods though, and has other methods that are not in the CFC):
// Phone class (maps to Phone.cfc)
package
{
[Bindable]
[RemoteClass(alias="CFIDE.samples.Phones.CF.Phone")]
public class Phone
{
// Properties
public var name:String;
public var description:String;
public var price:Number;
public var image:String;
public var series:String;
public var triband:Boolean;
public var camera:Boolean;
public var video:Boolean;
public var highlight1:String;
public var highlight2:String;
public function Phone()
{
}
// toString()
public function toString():String
{
return "Phone: " + name;
}
}
}
And with that done, automatic conversion can take place.
As you can see, with a little care and planning, automatic conversion of objects back and forth between ColdFusion and Flex 2 is very doable (not to mention incredibly useful).
Leave a Reply