View comments | RSS feed

Using Flash with CFCs

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.

To create a CFC that interacts with a Flash 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 Flash 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 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.


ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


Curtis24 said on Jul 5, 2006 at 8:27 AM :
The StructNew() function works well in a return with a few nested string variables. When nesting an array inside a structure it may not always work as desired.

<cfset myStructure = StructNew()>
<cfloop index="1" from="1" to="10" step="1">
<cfset myStructure[x][1] = name string>
<cfset myStructure[x][2] = street string>
</cfloop>

For each structure created there is an array nested inside. When using a structure in this manner watch the CPU Usage of your computer. When this return is decompiled by flash remoting for your flash application, it will take more CPU cycles than returning a simple string.

If the CPU Usage of your computer remains at 100% for 15 seconds you will see a pop up message that says.

A script in this movie is causing Flash Player to run slowly. If it continues to run, your computer may become unresponsive. Do you want to abort the script?
strobe3 said on Sep 20, 2007 at 4:07 PM :
It appears that if you work with cfc's that manipulate session variables, flash manages a separate session variable from the coldfusion session. This happens even when you enable JSESSIONID in the adminsitrator

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00001481.htm