7 Replies Latest reply on Aug 25, 2008 8:39 AM by Misenko

    Invoke a method

    Misenko
      Hi, how can I dynamically call a method? Let's say, I have a method doSomething(), textfield and a button. User writes doSomethig() into the textfield a press the button. How to assign this method to the click event handler of a button ? thanks
        • 1. Re: Invoke a method
          Culme Level 1
          Hi Misenko.
          I ran a quick test, and to my surprise this works (see attached code).
          (Parantheses not entered in the text box though - I hope that is no problem.)

          It's obviously rather risky with the risk of mis-spelling etc (!) but you're probably aware of that.

          hth / Culme
          • 2. Invoke a method
            Misenko Level 1
            quote:

            Originally posted by: Culme
            Hi Misenko.
            I ran a quick test, and to my surprise this works (see attached code).
            (Parantheses not entered in the text box though - I hope that is no problem.)

            Wow! That's great. I thought it's a bit moore complicated :) I'm going to try it at once.
            quote:


            It's obviously rather risky with the risk of mis-spelling etc (!) but you're probably aware of that.


            Yes, it should be risky but I think error handler deals with it like a charm:)

            Thanks a lot
            • 3. Re: Invoke a method
              Gregory Lafrance Level 6
              And this code allows you to pass args via the TextInput. This code is fragile though, because if you have doOtherStuff in the TextInput it works, but if you have doTherStuff() it fails. Also, you need to add code for methods that have other than 2 args. But it illustrates how to start thinking about possibly extending on Culme's solution.
              • 4. Re: Invoke a method
                Misenko Level 1
                Thanks guys. Looks very good. Just wonder if it is possible to dynamically call methods from event listener. I've tried yours methods but it is not working. You know, something like this :

                myButton.addEventListener(MouseEvent.CLICK, this[sAction]() ); - this one doesn't work....throws runtime exception. Compiler runs ok.

                thanks
                • 5. Re: Invoke a method
                  Gregory Lafrance Level 6
                  I'm not sure exactly why you want to do this (please enlighten us!!!), but this may help:
                  • 6. Re: Invoke a method
                    jylaxx Level 1
                    Did you try to create a standard listener function and then call your method from this listener ?
                    • 7. Re: Invoke a method
                      Misenko Level 1
                      Thanks guys,

                      quote:

                      Originally posted by: Greg Lafrance
                      I'm not sure exactly why you want to do this (please enlighten us!!!), but this may help:


                      Because I need to dynamically create buttons by user where user (during creating) can assign some predefined functions (string) which are executed by flex then. That's the reason why I wanted to assign this "string function" directly to the event listener of the button.

                      quote:

                      Did you try to create a standard listener function and then call your method from this listener ?


                      There is a problem 'cos every button can have any function so every button has to have different event listener.

                      But I've solved this problem, I have only one event listener for all the buttons, but I have extended button class for one property... name of the function to invoke :)

                      M.