2

I'm encountering something a bit bizarre, but maybe someone else came across this before.

I've got a base class, that doesn't extend anything. Let's call it...

public class FooBar {
    //...
}

But I want to bind EVERY single one of its exposed properties:

[Bindable] public class FooBar {
    public var propertyOne:String;
    public var propertyTwo:String;
}

While Debugging / Profiling the class, I'm noticing that each time a property is changed - the instance of FooBar is calling ".dispatchEvent()" on it. But my class doesn't extend EventDispatcher.

What gives?

Does this mean, at compile time, my class automatically extends EventDispatcher or some other class with the ability to dispatch events? How could I listen to the PropertyChangeEvent if my class doesn't have the "addEventListener" method declared in it?

bigp
  • 3,913
  • 3
  • 27
  • 52
  • Never noticed this, interesting! – Exort Sep 29 '11 at 18:58
  • A bit of a discovery update: actually, all the methods declared in the IEventDispatcher interface are available to that class. The code will compile fine, I just gave it a try. Another developer encountered this same mysterious background activity while making his own calls to "dispatchEvent()" in his Bindable class. Technically, that would mean that only one property or Getter/Setter needs to be declared [Bindable] to cause the class to implement IEventDispatcher, not necessarily the entire class. – bigp Sep 29 '11 at 19:06
  • And here's the source of the above mentionned developer who found this too: http://old.nabble.com/-Bindable--and-dispatchEvent-td13765582.html – bigp Sep 29 '11 at 19:07

1 Answers1

3

When you use the [Bindable] metadata the Flex compiler generates a lot of code for you. If you want to know what happens exactly take a look at the answers to What does the Flex [Bindable] tag do? and the links that are posted there.

To answer your question: No, your class doesn't extend EventDispatcher. However, the compiler changes your class so that it will implement the IEventDispatcher interface. The generated implemenation of that interface uses an instance of EventDispatcher.

Community
  • 1
  • 1
Gerhard Schlager
  • 3,096
  • 1
  • 29
  • 51
  • After looking at that old.nabble.com thread (posted in the comments in my question above), the IEventDispatcher interface was my second guess. Thanks to confirm that and providing some resources :) – bigp Sep 29 '11 at 19:17
  • Just going to post this here to eliminate some clicks in case someone wants to view the slides directly, explaining the generated code: http://www.slideshare.net/michael.labriola/diving-in-the-flex-data-binding-waters-presentation?src=embed – bigp Sep 29 '11 at 19:21