views:

1976

answers:

1

As far as I understand, binding to a var in ActionScript is carried out via events which are automatically fired when a var is changed. I have a few questions about this:

  1. What happens if the class doesn't explicitly extend EventDispatcher? Is there is some sort of magic that goes on in the compiler which essentially makes the class an EventDispatcher?
  2. How does binding to static vars work? Is some sort of implicit static addEventListener() function added to the class?
  3. I've noticed that you can put [Bindable] on static vars but not static functions, why is this?

Edit:

Thanks to Christophe Herreman for his illuminating answer. However, I have a few follow-ons from my questions above. I wonder if anyone could fill in the gaps for me (I tried to number these questions 4, 5, 6 but they show up 1, 2, 3 for some reason!)

  1. I've added the -keep option to the compiler and looked at the generated binding code. Could anyone explain how this code is applied? The compiler seems to generate a class called BindableProperty for each class, which contains the getter and setter. How does this get folded into the class I wrote? The feature looks kind of like partial classes in C# - is this exclusively behind-the-scenes compiler functionality, or is it something I could use myself?

  2. The BindableProperty class still does not explicitly implement EventDispatcher, how does the generated class implement event dispatching?

  3. In question 3, I asked about binding to static functions, but I actually meant static getter and setter functions. This doesn't seem to be allowed.

+7  A: 

Hi,

bindings do indeed work via event dispatching. You can learn a lot from the generated Actionscript code by adding the -keep flag to the compiler settings.

I'll try to answer your questions.

  1. the compiler will pick up the [Bindable] metadata tag and will create a getter and setter for each property you marked as bindable. The getter will just return the value of the property. The setter will check if the new value differs from the original value and will dispatch a PropertyChange event if it does.

  2. the same as for instance properties basically, only now a static getter and setter are created. In addition, a mx.binding.StaticPropertyWatcher is created to watch changes in the static properties.

  3. when binding to a static method, you get the following warning: "[Bindable] requires an event name parameter when applied to a function that is not a getter or setter." The description pretty much says it all. Since you don't have a "propertyChange" event that is triggered specifically for this method, you have to dispatch a custom one yourself. That is [Bindable(event="customEvent")] and then dispatch that event from somewhere in your class.

Christophe Herreman
Thanks Chris, that's a really helpful explanation.
aaaidan