Flex event will fire only once

In an AIR application, I have a personal variable and a setter:

private var _saveResult
public function set saveResult( result:String ):void
{
    _saveResult = result;
    dispatchEvent( new resultUpdatedEvent( _saveResult ));
}

      

The first time I set "saveResult" an event will fire. But it will never fire again unless I restart the application.

If I change the installer to:

public function set saveResult( result:String ):void
{
    _saveResult = result;
    if ( result != null)
    {
        dispatchEvent( new resultUpdatedEvent( _saveResult ));
    }
}

      

The problem goes away, I can set the variable many times and the event fires every time.

My question is:

Am I doing something wrong here? If not, can someone please explain to me what is going on? If so, what should I do?

Thank!

+2


source to share


2 answers


In the comments ... There were no event dispatcher problems. I misdiagnosed the problem, the REAL problem was that if you have a [Bindable] property and you are using a setter and you set it to the current value, flex will ignore it. So, you have several options:

1) give the getter and set other names. Sounds like a "bad idea", but it fixes the problem.



2) remove [Bindable] from class (my problem) or property. If the class does not implement IEventDispatcher, you will need to do so. You can just "extend Sprite" to see how it works, but that seems like a "bad idea" as a solution, so I implemented the IEventDispatcher for the example at the end of this page: http://livedocs.adobe.com/flash/ 9.0 / ActionScriptLangRefV3 / flash / events / IEventDispatcher.html

3) I'm sure there is a way around this error, but I don't need the class to be [Bindable], so I haven't found a workaround.

+1


source


It looks like you are building your event wrong. The first parameter of an Event object must always be a string. So in this case, you want to always use the same string so that you can listen to the event. What does your resultUpdatedEvent class look like? You want it to look something like this:

package myEvents
{
    import flash.events.Event;

    public class PropertyChangeEvent extends Event
    {
        public static const PROPERTY_CHANGE:String = "propertyChange";

        public var result:String = "";

        // Public constructor.
        public function PropertyChangeEvent (type:String, 
            result:String="") {
                // Call the constructor of the superclass.
                super(type);

                // Set the new property.
                this.result= result;
        }


        // Override the inherited clone() method.
        override public function clone():Event {
            return new PropertyChangeEvent(type, result);
        }
    }
}

      

So when you go to dispatch your event, you can create an event like this:



new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE, result);

      

Thus, you are listening for the "PropertyChangeEvent.PROPERTY_CHANGE" event, which never changes. The problem is that your event listener is probably listening for the event represented by the string saved in the result, and obviously this changes after the first set time, so there is no way to assign a listener to it.

More on how events work in AS3: http://livedocs.adobe.com/flex/3/html/help.html?content=events_02.html

+3


source







All Articles