Using Flash with CFCs

CFCs require little modification to work with a SWF application. The tag cffunction tag names the method and contains the CFML logic, the cfargument tag names the arguments, and the cfreturn tag returns the result to the SWF application. The name of the CFC file (*.cfc) translates to the service name in ActionScript.

Note: For CFC methods to communicate with SWF applications, set the cffunction tag’s access attribute to remote.

The following example replicates the helloWorld function that was previously implemented as a ColdFusion page. For more information, see Using the Flash Remoting service with ColdFusion pages.

Create a CFC that interacts with a SWF application

  1. Create a CFC and save it as flashComponent.cfc in the helloExamples directory.

  2. Modify the code in flashComponent.cfc so that it appears as follows:

    <cfcomponent name="flashComponent"> 
        <cffunction name="helloWorld" access="remote" returnType="Struct"> 
            <cfset tempStruct = StructNew()> 
            <cfset tempStruct.timeVar = DateFormat(Now ())> 
            <cfset tempStruct.helloMessage = "Hello World"> 
            <cfreturn tempStruct> 
        </cffunction> 
    </cfcomponent>

    In this example, the helloWorld function is created. The cfreturn tag returns the result to the SWF application.

  3. Save the file.

The helloWorld service function is now available through the flashComponent service to ActionScript. The following ActionScript example calls this function:

import mx.remoting.*; 
import mx.services.Log; 
import mx.rpc.*;  
 
// Connect to the Flash component service and create service object 
    var CFCService:Service = new Service( 
            "http://localhost/flashservices/gateway", 
            null, 
            "helloExamples.flashComponent", 
            null,  
            null ); 
// Call the service helloWorld() method 
var pc:PendingCall = CFCService.helloWorld(); 
// Tell the service what methods handle result and fault conditions 
pc.responder = new RelayResponder( this, "helloWorld_Result", "helloWorld_Fault" ); 
 
function helloWorld_Result(re:ResultEvent) 
{ 
    // Display successful result 
    messageDisplay.text = re.result.HELLOMESSAGE; 
    timeDisplay.text = re.result.TIMEVAR; 
} 
 
function helloWorld_Fault(fe:FaultEvent) 
{ 
    // Display fault returned from service 
    messageDisplay.text = fe.fault; 
}

In this example, the CFCService object references the flashComponent component in the helloExamples directory. Calling the helloWorld function in this example executes the function that is defined in flashComponent.

For ColdFusion components, the component filename, including the directory structure from the web root, serves as the service name. Remember to delimit the path directories with periods rather than backslashes.