Recipe 20.10 Calling ColdFusion Functions from Flash

20.10.1 Problem

You want to invoke ColdFusion service functions using Flash Remoting.

20.10.2 Solution

Preferably use a ColdFusion Component (CFC). Alternatively, you can invoke a ColdFusion page, with some modifications.

20.10.3 Discussion

When you are using ColdFusion, there are two types of Flash Remoting service functions: ColdFusion pages and CFC methods.

Preferably, you should use CFC methods instead of ColdFusion pages for all of your Flash Remoting needs. Calling a CFC method from Flash Remoting does not require any special code changes to the CFC. The only consideration is that you must make sure that the CFC method can be accessed remotely by setting the <cffunction> tag's access attribute to "remote", as follows:

<cffunction name="myCFCMethod" access="remote">
  <!--- method body --->

When you want to call a CFC method from a Flash movie, you should create a service object that maps to the CFC by specifying the fully qualified CFC name in the getService( ) method. The fully qualified CFC name includes any packages in which the CFC has been placed. For example:

// Create a service object that maps to a CFC named MyCFC in the root of the
// ColdFusion application.
myService = myConnection.getService("MyCFC");

// Or, if the CFC is in a package, create a service object including the package
// name. This example maps to a CFC named MyCFC in the OReilly.ASCB package.
myService = myConnection.getService("OReilly.ASCB.MyCFC");

If you must use a ColdFusion page with Flash Remoting, you must make changes to the code within the page. Any values that the page receives or returns are contained within the ColdFusion FLASH scope. All parameters that you send to the ColdFusion page via the service function invocation are stored in the params array within the FLASH scope. If you want to return a value to the Flash movie from the ColdFusion page, you should assign that value to the result variable within the FLASH scope. You can return only one value per ColdFusion page. The last value assigned to the FLASH.result variable when the page has been processed is the value that is returned.

<!--- Set two variables within the ColdFusion page to the values of the first 
      two parameters passed to the page from the Flash movie that called it --->
<CFSET myFirstParam = FLASH.params[1]>
<CFSET mySecondParam = FLASH.params[2]>

<!--- Return a value to the Flash movie --->
<CFSET FLASH.result = "a return value">

20.10.4 See Also

Recipe 20.11

    Part I: Local Recipes
    Part II: Remote Recipes