Passing ActionScript Classes to ColdFusion via Remoting

Something I just found out today is you can pass ActionScript classes to a CFC method via Flash Remoting. I was under the impression that this only worked in OpenAMF, AMFPHP, and Flash Remoting for Java. I have publicly stated in the past that it cannot be done, and I was wrong in doing so.

Typically, the structs I get back from ColdFusion are either objects with properties that have uppercase names, or arrays of structs. I write Factory code in either Flash or Flex, ARP or Cairngorm, to convert these transfer objects to value objects which are basically ActionScript classes that match their CFC counterparts, property for property. This is done so both I and the server-side developer can be on the same page when we discuss what a “Person” really means. If we both have a class that has data for the person defined in properties in our respective languages, it allows us to communicate effectively, both in code and over the phone, email, and IM.

A lot of time is spent writing Factory code, and is one of my frustrations with using Remoting with ColdFusion. OpenAMF isn’t any better if you don’t have a Java guy with Flash Remoting and/or OpenAMF experience. While your VO’s may technically work, you’ll spend weeks configuring it. AMFPHP just works.

However, as of today, I found out you CAN send ActionScript classes instead of just plain Object’s to a CFC method as long as you do 2 things:

  1. Wrap it in an Object with 1 property. That property must be named the same as the cfargument tag
  2. Make a deep clone of the VO you are sending back

If your CFC’s cfargument tag looks like this:

<cfargument name="someVO" type="struct" required="false" default="" />

Then you’re service call in ActionScript looks something like this:

var clonedVO:SomeClass = myVO.clone();
service.method ( {someVO: clonedVO} );

Notice how the someVO matches the cfargument tag’s name. The first part makes sense, but the clone still baffles me. Regardless, it works, and cuts down on 40% of the time I spend writing Factory code to transpose ActionScript value object classes to plain old vanilla objects.

This post is irrelevant with Flex 2 and ColdFusion > 7.0.0 since Remoting works like you’d expect it to (at least, from what I’ve seen in the demo’s, haven’t played with Mystic yet).

4 Replies to “Passing ActionScript Classes to ColdFusion via Remoting”

  1. The trick with flex2 and cf is that your cfc must have cfproperty tags that match the instance vars of your as object (type and order). This allows the cf amf gateway to translate the properties correctly. Well that and you still have to register your object.

    But you are right, cf 7 with flex 2.0 works like a dream compared to how it works with flex 1.5.

  2. > Typically, the structs I get back from ColdFusion are either objects with propertie
    > that have uppercase names, or arrays of structs.

    In CFMX 6.1 or higher, you need to specify your properties using bracket syntax and then case will preserved when the AMF is received on the SWF end.



    – Ian

  3. hmmm … the blog stripped out all my cf tags.

    ok — try this pseudo CF (sans angled brackets)

    cfset result = StructNew()
    cfset result[‘propertyOne’] = ‘foo’
    cfset result[‘propertyTwo’] = ‘bar’
    cfreturn result

Comments are closed.