If you've been working with databinding in the Flash IDE I'm sure you've come across (or at least read about) the problems with cutting and pasting components or moving them inside a movieclip. Doing any of those things simply make all your bindings evaporate into thin air (cool huh!). True believers will tell you: "it's not a bug, it's a feature", all I can add to that is that Flash MX 2004 is certainly a feature rich product!

In any case, for those reasons and a few other practical considerations I've taken to writing bindings using the runtime databinding API. There's fairly little info posted about the subject and because I've been getting an increasing amount of questions about it I've made a few basic examples available for download.

When you first get started with the API it might look a bit scary but it's in fact quite straight-forward. I'll walk you through the necessary steps for a simple binding:

- Open up a new blank FLA

- Drag an instance of the DataBindingClasses into your library
(Window > Other Panels > Common Libraries > Classes)

- Drag two instances of the TextArea component on stage
(instancenames "fromText" and "toText")

- Next you can start writing the actual ActionScript code:

import mx.data.binding.*

var from:Object = {component: fromText, property: "text", event:"keyDown"};
var to:Object = {component: toText, property: "text"};

var myBinding:Binding = new Binding(from,to);

What the code above does is import the binding classes and define the "from" and "to" objects that contain information on what component properties to bind. When you look at the Flash MX 2004 documentation on this you'll notice that they are using the EndPoint class but I've found regular objects work just as well.

In this case we'll be using one-way binding between the text property of the two TextArea component and setting "KeyDown" as the event trigger. You can set an optional third and fourth argument when instantiating the Binding class that allows you to set a class formatter and whether or not it is a two-way binding. Check out "binding-formatter.fla" if you want to learn more about this!

Posted
AuthorPeter
CategoriesFlash