Flex RemoteObject and ColdFusion Components

I am not a CF expert. So when I was asked to provide a sample program showing how to use ColdFusion Components (CFCs) from Flex, I decided it was time to learn more about Flex’s big brother.

I’m sure a lot of you will roll your eyes at the simplicity of this, but if are just getting started combining the technologies, this should be a big help.


First, define your CFC. I won’t go into the details about that, there are plenty of books on the subject. Here is a simple CFC to illustrate this example:

<cfcomponent>
 <cffunction name="getdata" access="remote" returnType="any">
    <cfset myArray = ArrayNew(1)>
    <cfset myStruct = StructNew()>
    <cfset myStruct.fruit = "Apples">
    <cfset myStruct.quantity = 1245>
    <cfset myArray[1] = myStruct>

    <cfset myStruct = StructNew()>
    <cfset myStruct.fruit = "Oranges">
    <cfset myStruct.quantity = 5601>
    <cfset MyArray[2] = myStruct>

    <cfreturn myArray>
  </cffunction>
</cfcomponent>

Know where your ColdFusion server is installed. For me, it is at http://localhost:8101/cf6 with /cf6 being the context root.

My CFC is in the file: cfdocs/petersamples/cfc/flextest.cfc

Know where your Flex server is installed. For me, it is at http://localhost:8700/flex with /flex being the context root.

In my Flex application I defined the following tag:

<mx:RemoteObject id="ro"
    endPoint="http://localhost:8101/cf6/flashservices/gateway"
    source="cfdocs/petersamples/cfc/flextest">
      <mx:method name="getdata" />
</mx:RemoteObject>

The endPoint is set to the Flash Remoting gateway that comes built into ColdFusion. The source points to the CFC starting just below the context root (/cf6).

The method names the cffunction(s) in the CFC.

At this point, you treat the CFC as you would any other RemoteObject: invoke the function (eg, ro.getdata(parameters)) and handle the result (eg, {ro.getdata.result}).