Flylib.com

Books Software

 
 
 

Recipe 20.10 Calling ColdFusion Functions from Flash

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 --->
</cffunction>

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

Recipe 20.11 Passing Named Parameters to ColdFusion Component Methods

20.11.1 Problem

You want to pass named parameters to a ColdFusion Component (CFC) method using Flash Remoting.

20.11.2 Solution

Create an ActionScript object with properties corresponding to the expected CFC method parameters, and pass that object as a single parameter to the CFC method.

20.11.3 Discussion

You can pass named parameters (as opposed to positional parameters) to a CFC method from a Flash movie. To accomplish this, you should pass a single object parameter to the CFC method in which the property names correspond to the names you have given to the parameters in the CFC method. ColdFusion automatically matches up the object's property values to the CFC method's parameters with the same names . For example:

<!--- A sample CFC method definition --->
<cffunction name="getCarInfo" access="remote">
  <cfargument name="make" type="string">
  <cfargument name="model" type="string">
  <!--- rest of method body --->
<cffunction>

// The corresponding ActionScript snippet . . . 

// Create an object with properties named


make


and


model


.
params = new Object(  );
params.make = "Honda";
params.model = "Accord";

// Call the


getCarInfo(  )


service function and pass it the


params


object.
myService.getCarInfo(myResponseObject, params);

20.11.4 See Also

Recipe 20.12