User Guide

Data binding classes (Flash Professional only) 217
CustomFormatter class (Flash Professional only)
ActionScript Class Name mx.data.binding.CustomFormatter
The CustomFormatter class defines two methods,
format() and unformat(), that provide the
ability to transform data values from a specific data type to String, and vice versa. By default, these
methods do nothing; you must implement them in a subclass of
mx.data.binding.CustomFormatter.
To create your own custom formatter, you first create a subclass of CustomFormatter that
implements
format() and unformat() methods. You can then assign that class to a binding
between components either by creating a new Binding object with ActionScript (see “Binding
class (Flash Professional only)” on page 214), or by using the Bindings tab in the Component
inspector. For information on assigning a formatter class using the Component inspector, see
“Schema formatters” in Using Flash.
You can also assign a formatter class to a component property on the Component inspectors
Schema tab. However, in that case, the formatter will be used only when the data is needed in the
form of a string. In contrast, formatters assigned with the Bindings panel, or created with
ActionScript, are used whenever when the binding is executed.
For an example of writing and assigning a custom formatter using ActionScript, see “Sample
custom formatter” on page 217.
Note: To make this class available at runtime, you must include the data binding classes in your FLA
document.
For an overview of the classes in the mx.data.binding package, see “Classes in the mx.data.binding
package (Flash Professional only)” on page 213.
Sample custom formatter
The following example demonstrates how to create a custom formatter class and then apply it to a
binding between two components by using ActionScript. In this example, the current value of a
NumericStepper component (its
value property) is bound to the current value of a TextInput
component (its
text property). The custom formatter class formats the current numeric value of
the NumericStepper component (for example, 1, 2, or 3) as its English word equivalent (for
example, “one”, “two”, or “three”) before assigning it to the TextInput component.
To create and use a custom formatter:
1.
In Flash MX Professional 2004, create a new ActionScript file.
2.
Add the following code to the file:
// NumberFormatter.as
class NumberFormatter extends mx.data.binding.CustomFormatter {
// Format a Number, return a String
function format(rawValue) {
var returnValue;
var strArray = new Array('one', 'two', 'three');
var numArray = new Array(1, 2, 3);
returnValue = 0;
for (var i = 0; i<strArray.length; i++) {