6 Replies Latest reply on Aug 3, 2009 6:42 PM by phil1943

    reusing event function

    phil1943 Level 1

      Hello, just wondering what correct practice is here.

       

      I have an event listener which calls a function. Of course that function has to take an event argument.

      Now, what if I want to call that function from somewhere else where I dont have an event to pass to it.

       

      Is it correct just to pass any value in order to satisy the argument requirement ?

       

      Thanks for any advice.

        • 1. Re: reusing event function
          Michael Borbor Level 4

          If the event is required you have to pass it,  whenever you want to attach

          an event listener otherwise you don't. Of course you can always use the

          event.currentTarget to know who triggers the event.

          • 2. Re: reusing event function
            phil1943 Level 1

            hello, thanks but what I meant was this:

             

            Here is my event listener:

            this.addEventListener(Event.CHANGE, adjustHeightHandler);

             

            Here is the function it calls:

            function adjustHeightHandler(event):void{...

             

            Now what if I want to call that same function from somewhere else in my code eg.

             

            adjustHeightHandler();

             

            I must pass an argument otherwise I get an error. However I cannot pass an event as there is none from this call location.

            So would it be correct practice to just pass any old variable or even "this" in order to satisfy the argument requirement ?

            eg.

             

            adjustHeightHandler(this);

             

            or

             

            adjustHeightHandler(anyOldVariable)

             

            I know it works this way, my question is simply is it acceptable to do this.

            • 3. Re: reusing event function
              Michael Borbor Level 4

              I see, if you want to do that probably the way to go would be to dispatch

              manually an event

              http://livedocs.adobe.com/flex/3/html/help.html?content=events_07.html

               

              Also you could make the event argument optional, or you could create a

              subset of statements in another function and then call this function within

              the event handler and whenever else in your code where you need it.

              • 4. Re: reusing event function
                Muzak Level 3

                Don't call the handler, dispatch a change event.

                 

                 dispatchEvent(new Event(Event.CHANGE));
                 

                 

                Edit: Looks like Michael answered that at the same time

                 

                 

                I've also seen people do the following:

                 

                function myEventHandler(event:Event=null):void {
                     // do stuff
                }
                
                // somewhere else in the code
                myEventHandler();
                

                Or:

                 

                function myEventHandler(event:Event):void {
                     // do stuff
                }
                
                //somewhere else in code
                myEventHandler(null);
                


                I'm not a big fan of that approach though, as in the Event handler you can't use the event argument without checking if it exists (not null) nor use its properties (if any).

                So stuff like event.currentTarget is out of the question.

                • 5. Re: reusing event function
                  phil1943 Level 1

                  thank you.

                  How could I make the event argument optional ?

                  • 6. Re: reusing event function
                    Michael Borbor Level 4

                    private function adjustHeightHandler(event:Event=null)