5 Replies Latest reply on Jun 30, 2009 10:46 AM by yancaoshi

    How to set event handler dynamically?

    yancaoshi

      Hi all,

       

      Usually, we add event listener like this:

      myCanvas.addEventListener("click", onClick)

       

      How can I set the second parameter so that I can add click handler dynamically?

       

      For example, I have functions clickHandler1, clickHandler2, ...

      And another variable fname: String will contains value1, value2, ...

       

       

      Thanks and regards,

        • 1. Re: How to set event handler dynamically?
          Michael Hovey

          You could do something like this:

           

          public function firstFunction():void
          {
               trace("First Function");
          }
          
          public function secondFunction():void
          {
               trace("Second Function");
          }
          
          public function callFunctionByName(name:String):void
          {
               this[name]();
          }
          
          public function addEventHandlerByName(handlerName:String):void
          {
               this.addEventListener(Event.ENTER_FRAME, this[handlerName]);
          }
          
          this.callFunctionByName("firstFunction"); //traces 'First Function'
          this.callFunctionByName("secondFunction"); //traces 'Second Function'
          

           

          Let me know if that works for you.

          1 person found this helpful
          • 2. Re: How to set event handler dynamically?
            aaravind

            Just try out event.loadcomplete..ie after loadind something in canvas it listerns the event

            • 3. Re: How to set event handler dynamically?
              yancaoshi Level 1

              Hi all,

               

              Please don't give solution literally because that's a simplied scenario.

              Real requirement is that we need to create a standalone class which will manage all RemoteObject request. And the key is to add event listener dynamically, so that we can simply pass handler function name which will process result from server later on.

               

              Currently, it is not solved yet. Please suggest more.

               

              Thanks and regards,

              • 4. Re: How to set event handler dynamically?
                Michael Hovey Level 1

                If you read my post above and apply it to your specific situation, you should be able to solve it. The example I provided shows how to pass and call a function by its name. If you already have a reference to the function, you can simply change the parameter from a String and pass in the function directly.

                 

                If you're still having troubles getting this, please provide an example of what you're trying to do so we can show the solution in context.

                • 5. Re: How to set event handler dynamically?
                  yancaoshi Level 1

                  Hi Michael,

                   

                  Thanks for your posting. I have made it just now with your help.

                   

                  Best wishes,