CFCs require little modification to work with a Flash application. The 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 Flash application. The name of the CFC file (*.cfc) translates to the service name in ActionScript.
Note: For CFC methods to communicate with Flash applications, you must 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.
<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 Flash application.
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 varCFCService
: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 file name, including the directory structure from the web root, serves as the service name. Remember to delimit the path directories with periods rather than backslashes.