Recipe 20.18 Returning Typed Objects from ColdFusion

20.18.1 Problem

You want to return a typed object from ColdFusion to a Flash movie.

20.18.2 Solution

Create an ASObject and set the type to match the name of a registered ActionScript class.

20.18.3 Discussion

When an ASObject is returned to a Flash movie, Flash attempts to find a registered class in which the registered name matches the name of the type that has been set for the ASObject via the setType( ) method. You can create an ASObject within ColdFusion using the ColdFusion techniques for creating Java objects. If you are not familiar with how to do this, you can read more about it in the ColdFusion documentation available here:

Here is an example of a ColdFusion Component method that creates and returns an ASObject:

<cffunction name="getTypedObject" access="remote">

  <!--- Create a Java object from the class. 
        Add the name attribute with the arbitrary name ASObjectClass so 
        that you can initialize the object properly. --->
  <cfobject type="Java" class="" 
            action="create" >

  <!---  The init(  ) method returns a proper ASObject. --->
  <cfset aso = ASObjectClass.init(  )>

  <!--- Set the type of the ASObject to match the name 
        of a registered ActionScript class --->
  <cfset aso.setType("MyClass")>
  <cfset aso.put("a", "eh")>
  <cfset aso.put("b", "bee")>
  <cfreturn aso>

Here is an example snippet of ActionScript code that corresponds to the preceding ColdFusion method:

function MyClass(a, b) {
  this.a = a;
  this.b = b;

// Add a method that just writes all the properties to the Output window. This
// example simply illustrates that the ASObject is correctly converted into a MyClass 
// object when returned to Flash.
MyClass.prototype.traceProperties = function (  ) {
  for (var prop in this) {
    trace(prop + " = " + this[prop];

Object.registerClass("MyClass", MyClass);

// Create a response object.
myResponse = new Object(  );
myResponse.onResult = function (result) {
  // The result parameter is correctly converted to a MyClass object. You can prove
  // this because the traceProperties(  ) method gets called.
  result.traceProperties(  );

// Call the service function.

20.18.4 See Also

Recipe 20.15

    Part I: Local Recipes
    Part II: Remote Recipes