Flash CS3 Documentation |
|||
| ActionScript 2.0 Components Language Reference > Data binding classes > CustomFormatter class > 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.
// 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++) {
if (rawValue == numArray[i]) {
returnValue = strArray[i];
break;
}
}
return returnValue;
} // convert a formatted value, return a raw value
function unformat(formattedValue) {
var returnValue;
var strArray = new Array('one', 'two', 'three');
var numArray = new Array(1, 2, 3);
returnValue = "invalid";
for (var i = 0; i<strArray.length; i++) {
if (formattedValue == strArray[i]) {
returnValue = numArray[i];
break;
}
}
return returnValue;
}
}
import mx.data.binding.*;
var x:NumberFormatter;
var customBinding = new Binding({component:stepper, property:"value", event:"change"}, {component:textInput, property:"text", event:"enter,change"}, {cls:mx.data.formatters.Custom, settings:{classname:"NumberFormatter"}});
The second line of code (var x:NumberFormatter) ensures that the byte code for your custom formatter class is included in the compiled SWF file.
This makes the data binding runtime classes available for your document. For more information, see Making data binding classes available at runtime.
Click the buttons on the NumericStepper component and watch the contents of the TextInput component update.
The following table lists the methods of the CustomFormatter class.
|
Method |
Description |
|---|---|
|
Converts from a raw data type to a new object. |
|
|
Converts from a string, or other data type, to a raw data type. |
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00002672.html