User Guide

234 Chapter 6: Components Dictionary
7.
Add the following code to the Actions panel:
// Add ComponentMixin methods to TextInput component.
// Note that this step is only necessary if the component
// isn’t already involved in a data binding,
// either as the source or destination.
mx.data.binding.ComponentMixins.initComponent(zipCode_txt);
// Define event listener function for component:
validateResults = function (eventObj) {
var errors:Array = eventObj.target.validateProperty("text");
if (errors != null) {
trace(errors);
}
};
// Register listener function with component:
zipCode_txt.addEventListener("enter", validateResults);
8.
Select Window > Other Panels > Common Libraries > Classes to open the Classes library.
9.
Open your document’s library by choosing Window > Library.
10.
Drag DataBindingClasses from the Classes library to your document’s library.
This step makes the data binding runtime classes available to the SWF file at runtime.
11.
Test the SWF file by selecting Control > Test Movie.
In the TextInput component on the Stage, enter an invalid United States zip code—for
example, one that contains all letters, or one that contains fewer than five numbers. Notice the
error messages displayed in the Output panel.
DataType class (Flash Professional only)
ActionScript Class Name mx.data.binding.DataType
The DataType class provides read and write access to data fields of a component property. To get
a DataType object, you call the
ComponentMixins.getField() method on a component. You
can then call methods of the DataType object to get and set the value of the field.
If you get and set field values directly on the component instance instead of using DataType class
methods, the data is provided in its “raw” form. In contrast, when you get or set field values using
DataType methods, the values are processed according to the fields schema settings.
For example, the following code gets the value of a components property directly and assigns it to
a variable. The variable,
propVar, contains whatever “raw” value is the current value of the
property
propName.
var propVar = myComponent.propName;
The next example gets the value of the same property by using the DataType.getAsString()
method. In this case, the value assigned to
stringVar is the value of propName after being
processed according to its schema settings, and then returned as a string.
var dataTypeObj:mx.data.binding.DataType = myComponent.getField("propName");
var stringVar: String = dataTypeObj.getAsString();
For more information about how to specify a field’s schema settings, see “Working with schemas
in the Schema tab (Flash Professional only)” in Using Flash.