Creating runtime data bindings using ActionScript 581
6. Insert a new layer and name it actions.
7. Add the following ActionScript to Frame 1 of the actions layer:
var src:mx.data.binding.EndPoint = new mx.data.binding.EndPoint();
src.component = in_ti;
src.property = "text";
src.event = "focusOut";
var dest:mx.data.binding.EndPoint = new mx.data.binding.EndPoint();
dest.component = out_ti;
dest.property = "text";
new mx.data.binding.Binding(src, dest);
If you prefer the somewhat shortened version, you could import the binding classes and
use the following code instead:
import mx.data.binding.*;
var src:EndPoint = new EndPoint();
src.component = in_ti;
src.property = "text";
src.event = "focusOut";
var dest:EndPoint = new EndPoint();
dest.component = out_ti;
dest.property = "text";
new Binding(src, dest);
This ActionScript creates two data binding end points, one for each component that
you’re binding. The first endpoint you create defines which component it is binding from
(
in_ti), which property to watch for (text), and which event will trigger the binding
(
focusOut). The second endpoint you create lists only the component and property
(
out_ti and text, respectively). Finally, you create the binding between the two
endpoints when you call the constructor for the Binding class (
new Binding(src,
dest)
).
You don’t need to use fully qualified class names (such as
mx.data.binding.EndPoint)
in your ActionScript, as you saw in the first code snippet. If you use the
import statement
at the beginning of your code, you can avoid using fully qualified names. When you
import all the classes in the
mx.data.binding package using the wildcard (*) (the
package includes both the EndPoint and Binding classes), you can shorten your code and
directly reference the EndPoint and Binding classes. For more information on import
statements, see the
import entry in the ActionScript 2.0 Language Reference.
8. Select Control > Test Movie to test the code in the test environment. Enter some text into
the
in_ti text input field.
After the
in_ti instance loses focus (click the Stage, press Tab, or click the second field),
Flash copies any text that you input into
in_ti to the out_ti text field.
9. Select File > Save to save your changes.