Search code examples
apache-flexeventspropertiesbindable

Flex watch bindable property other class


I'm creating an application using Flex 4. When the app is started, it reads a XML file and populate objects. The .send() call is asynchronous, so I would want to listen/watch to this populated object, and when it has finished, dispatch an event for other classes, so they can use it.

package model{
    public class LectureService extends HTTPService{
        [Bindable]
        private var _lecture:Lecture;

        ...
}

The xml is parsed correctly and loaded inside the object lecture of the class Lecture.

If I use the MXML notation in the main.mxml app, it works fine (the object is used when the it is populated after the async request):

<mx:Image id="currentSlide" source={lectureService.lecture.slides.getItemAt(0).path} />

BUT, I have another ActionScript class and I'm not able to listen to this dispatched (by [Bindable]) event.

package components{

    public class LectureSlideDisplay extends Image
    {       
        public function LectureSlideDisplay()
        {
            super();

            this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onChangeTest);
        }

        private function onChangeTest(e:PropertyChangeEvent):void {
            trace('test');
        }

I have already tried:

  1. using (like above) addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, methodName).
  2. tried to change the [Bindable] to [Bindalbe("nameEvent")] and listen for this, nothing.
  3. using CreateWatcher method, doesn't work.
  4. tried to have a look to the generated code of the class, but didn't help me

    if (this.hasEventListener("propertyChange")){ this.dispatchEvent(mx.events.PropertyChangeEvent.createUpdateEvent(this, "lecture", oldValue, value)); }

How can I listen and have the populated object in another class? Maybe the problem is that I'm listening from another class, but in this case how can I implement this? It seems the event is dispatched, but I can't listen to it.


Solution

  • Consider to use BindingUtils class. You can found documentation here. And a couple of usage samples: one, two.