6 Replies Latest reply on Jun 16, 2009 10:55 PM by Madhav Subedi

    EventListener for a variable

    SiHoop Level 1

      How do I create a listener for a variable. I need to execute a function when a variable changes value.

        • 1. Re: EventListener for a variable
          leybniz Level 4

          use setters and getters mechanism,

           

          private var _something:*;

           

           

          public function set something(value:*):void {

          _something = value;

          }

           

          public function get something():* {

          return _something;

          }

           

          If you feel this message answers your question or helps, please mark it respectively

          1 person found this helpful
          • 2. Re: EventListener for a variable
            CoreyRLucier Adobe Employee

            ...or just make your variable [Bindable] and listen for the PropertyChangeEvent.PROPERTY_CHANGE event on the owning component.  To determine which property changed in your event handler just query event.property (where event is of type PropertyChangeEvent).

             

            -Corey

            • 3. Re: EventListener for a variable
              leybniz Level 4

              each [Bindable] tag forces mxml compiler to generate a lot of "s-h-i-t-t-y" Event-driven code, Events are slow and it's not performance wise to overuse bindings  in cases when you could simply go with the setter

              • 4. Re: EventListener for a variable
                CoreyRLucier Adobe Employee

                To clarify, the answer sorta depends on if the requirement is A) or B)...I was thinking B) when I read the initial post...

                 

                A) If the method that needs to be fired when the variable changes is local to the object itself, then a getter/setter is fine and all that is required.

                 

                B) If the method is on wholly different object, then there would need to be some notification to the other object's method that the variable changed.

                 

                In this case (B) I was not suggesting a binding per say, I was suggesting [Bindable] metadata.

                 

                What **** does the compiler generate with [Bindable] metadata? Well, in fact the compiler generates a getter and setter for you, and dispatches the change notification when the setter is invoked.  It's simply a convenience mechanism for (B).

                 

                I completely agree with you that an explicit binding would be overkill.

                 

                -C

                • 5. Re: EventListener for a variable
                  SiHoop Level 1

                  This looks as though it will be very helpful, but I don't understand how to use it. Is it possible for you to show me a simple example where the get / set methodology is used to capture the change of a variable?

                  • 6. Re: EventListener for a variable
                    Madhav Subedi Level 4

                    private var _something:*;

                     

                     

                    public function set something(value:*):void {

                    _something = value;

                    /*

                    put your necessary code here,

                    as at this point you are sure that your variable is changed.

                    */

                    }

                     

                     

                     

                    or if you are doing something only when the variable is changed(not set the old value):

                     

                     

                    public function set something(value:*):void {

                    if(_something != value)

                    {

                         _something = value;

                         /*

                         put your necessary code here,

                         as at this point you are sure that your variable is changed.

                         */

                    }

                    }

                     

                    hope it clears.