PDA

View Full Version : how fast is data binding?


Think
08-19-2009, 07:51 PM
So i am using data binding by defining a variable like this:

[Bindable] public var myVar:Boolean;

and then binding it to another boolean using BindingUtils.BindProperty();

what I want to do is change myVar, and then on the very next line make a function call into a different class that uses the variable that myVar is bound to.

this seems to be working fine but as I understand it, binding in flex uses events, so is it possible that I can use the variable in the other class before the binding has had time to take effect?

Thanks, Think

wvxvw
08-19-2009, 08:59 PM
AS3 code is executed in a single thread - so, it doesn't matter if the Binding is fast or slow, no matter how long it takes for the binding to take effect, the code after it will not be executed, untill the binding finishes.

flexexplorer
08-20-2009, 06:06 AM
As you know, when you make a class Bindable, it changes the class to implement the IEventDispatcher interface. This allows the class to dispatch events whenever changes are made to variables.

Let's say you bind two variables like this: BindingUtils.bindProperty(A, "foo", B, "bar"). That means that whenever B.bar changes, A.foo will changes as well. But what happens behinds the scene?

bindProperty creates an object called ChangeWatcher. This object registers a listener with B. Whenever B.bar changes, it dispatches an event, and as a result executes ChangeWatcher's listener. It's important to understand that this step is atomic. You are guaranteed that no function, or outside event, will come in between an assignment to B.bar and ChangeWatcher assigning B.bar to A.foo

So to answer your original question. If you make a change to B.bar, you can safely assume that that value will be in A.foo.

I suggest you read up on events in ActionScript. Event dispatching is actually sugar coating for a function call. It doesn't spawn a new thread or anything of that sort.