Recipe 20.14 Transmitting Custom Datatypes to a Flash Remoting Back End

20.14.1 Problem

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

20.14.2 Solution

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

20.14.3 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.
function MyClass(a, b) {
  this.a = a;
  this.b = b;

// When you want to register a custom class you should also declare the properties as
// members of the class's prototype.

    Part I: Local Recipes
    Part II: Remote Recipes