20.14. Transmitting Custom Datatypes to a Flash Remoting Back End

Problem

You want to create an object of a custom type to send to a Flash Remoting back end.

Solution

On the client-side, create a custom class in the ActionScript code, and then use Object.registerClass( ) to register it.

Discussion

Flash Remoting enables you to send and receive many different kinds of data—even custom datatypes—to and from a Flash Remoting back end. However, Flash has to know about the datatype for it to be properly transmitted. The Object.registerClass( ) method takes care of registering custom classes with the Flash movie so that it knows how to handle objects of those types when sending and receiving them via Flash Remoting. When you use the registerClass( ) method for this purpose, the first parameter should be an arbitrary name by which Flash should know the datatype, and the second parameter should be a reference to the class that you want to register. Conventionally, the name to which you register the class should match the class name.

// Create a custom class.
function MyClass(  ){}

// Register the class so that Flash knows it by the ID MyClass.
Object.registerClass("MyClass", MyClass);

Note that for the properties of a custom class to be correctly interpreted by Flash, when using this technique, you must declare the properties as members of the class’s prototype:

// Create a custom class. The constructor accepts two parameters and sets two // properties. Normally this would be all that is necessary. ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.