Learning Flex – Lesson 5, Part II

Building Custom ActionScript Classes

Building an application in MXML results in an ActionScript class. The MXML is converted to ActionScript, combined with any code within a Script tag and the resulting class is compiled into a swf. When you compile test.mxml, a file named test-generated.as is created with the line

public class Test extends mx.core.Application

If you want to see the generated as files, add -keep-generated-actionscript to the flex compiler arguments. In FlashDevelop, you can do this by selecting project,properties… and clicking the compiler options tab. Click on the + next to the Additional Compiler Options and enter the value in the column to the right of [0].

Value or Data Transfer Objects

This kind of object is implemented as an ActionScript class and can be considered a container for data. They are often used for transfer of information to the back end of an application (potentially a database).

Each class is defined within a package similarly to Java. In fact, ActionScript classes look very similar to Java classes. One of the differences is how variables are declared ( var variableName:type; rather than Type variableName; ).

The [Bindable] metadata tag specified before a class name means that all public properties in the class can be bound to controls or other data structures. This can be placed before individual properties to limit it’s scope.

Externalizing Model Data

You can read the data for a model from an XML file by specifying the source property as the location of the file. The data will then be automatically parsed into an ActionScript compatible data structure. Data retrieved from a model tag is untyped so non strings must be cast to their type before use.

Debugging with Trace Statements

Flex has a global method called trace() that allows you to output data to the console when running the debugger version of the Flash player. It takes a string as the value and outputs the current value to the console. Defining a toString():String function within a custom class allows trace statements to automatically call this function if an object of that class is provided in a trace statement.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: