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:

http://livedocs.macromedia.com/cfmxdocs/Developing_ColdFusion_MX_Applications_with_CFML/Java5.jsp#1134356

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 flashgateway.io.ASObject class.          Add the name attribute with the arbitrary name ASObjectClass so          that you can initialize the object properly. --->   <cfobject type="Java"               name="ASObjectClass"              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> </cffunction>

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; } MyClass.prototype.a; MyClass.prototype.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. myCFCService.getTypedObject(myResponse);

20.18.4 See Also

Recipe 20.15



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net