Recipe 20.12 Passing Complex Parameters to ColdFusion Component Methods

20.12.1 Problem

You want to pass an object parameter to a ColdFusion Component (CFC) method using Flash Remoting.

20.12.2 Solution

Pass the value as a named parameter.

20.12.3 Discussion

ColdFusion automatically attempts to convert object parameters into named parameters (see Recipe 20.11). This is problematic if you want to pass an ActionScript object to a CFC method as a parameter. Consider the following example:

<!--- A sample CFC method definition --->
<cffunction name="insertCatalogItem" access="remote">
  <cfargument name="catalogItem" type="struct">
  <!--- rest of method body --->

// The corresponding ActionScript snippet . . . 

// Create an item object with name, price, and description properties.
item = new Object(  ); = "Widget";
item.price = 9.99;
item.description = "Widgets available in green, blue, and gold";

// Call the insertCatalogItem(  ) service function and pass it the item object.
myService.insertCatalogItem(myResponseObject, item);

In the preceding example, we want to pass the item object from Flash to the CFC method as a parameter that should be converted into a struct (a complex datatype). However, ColdFusion attempts to convert the properties of the item object into named parameters, and it tries to match up the name, price, and description properties to corresponding parameters within the CFC method. This prevents it from successfully passing a complex parameter, such as an object datatype.

You should pass the object parameter as a named parameter instead of as a positional parameter. Here, the item object is passed as the named parameter catalogItem:

MyService.insertCatalogItem(myResponseObject, {catalogItem: item});

Note that using named parameters prevents ColdFusion from trying to convert the subitems into individual parameters within the CFC method, thus allowing complex datatypes to be passed.

20.12.4 See Also

Recipe 20.11

    Part I: Local Recipes
    Part II: Remote Recipes