5 Replies Latest reply on Nov 30, 2006 9:33 AM by ur_dtrain

    menuitem event

    g00fymad
      hi to all,

      can anyone inform me how to dynamically invoke a function from its signature as a string?

      i have something like this;


      <mx:MenuBar width="100%" labelField="@label" cornerRadius="5" itemClick="MenuItemClick(event)">
      <mx:XMLList>
      <menuitem label="File" click="File_Click" />
      </mx:XMLList>
      <mx:MenuBar>

      then i would have this


      <mx:Script>
      <![CDATA[
      import mx.controls.Menu;
      import mx.events.MenuEvent;

      private function MenuItemClick(event:MenuEvent):void {

      var str:String = event.item.@click;
      // call the function str() where str is the @click property of the event.item object
      }
      ]]>
      </mx:Script>


      i would also like to know how to pass in parameters to the function i will invoke like if the menuitem is toggled or not

      can anyone help please?

      Maurice.
        • 1. Re: menuitem event
          ntsiii Level 3
          Perhaps the techniques discussed in this article might help:
          http://www.adobe.com/devnet/flex/articles/e4x.html

          Tracy
          • 2. Re: menuitem event
            ntsiii Level 3
            Though probably not. There is not really a way to directly do what you ask. ActionScript does not now and never has fully supported the JavaScript "eval()" function.

            Perhaps you should back up a bit and tell us what you are trying to do, maybe we can suggest some approaches.

            Tracy
            • 3. menuitem event
              g00fymad Level 1
              Hi and thanks for the reply.

              My background is C# and java although I have done quite a bit of Actionscript lately.
              What i have is a menu and from that menu i want to call a function for each item, but rather that just listening to the itemclick event and then using item indexes in a switch{case} to goto each function, I thought i would be a little smarter and just tag the function name for each item onto that item in the xml. So when the item is clicked I have access to what function i want to call, but it is only a String. In c# I could actually activate this function from its signature, something which i cannot find in actionscript.

              I currently have it implemented using the indexing method described above, but as the menu grows the code gets ugly, which i don't want

              I will aslo need to be able to add items to the menus dynamically, which i can do, but i also need to tag a function to that item when it is added, and the function is obviously created dynamically as well for those items.

              hope this clears things up
              regards,
              Maurice.
              • 4. Re: menuitem event
                ntsiii Level 3
                Right, you can't do that directly in AS.

                We use a two step process. First, we take the string representation of some user created function and generate a string that is a full mxml component, that includes the user defined function in it.

                Then we use that component in the app and can access the functions.

                I don't think this will help you though.

                Tracy
                • 5. Re: menuitem event
                  ur_dtrain Level 1
                  You might try casting your returned string as a Function(), then using the call() method. Something like this perhaps:

                  Function(baseObj[xmlString]).call(null,paramStr);

                  baseObj would be where the function is defined, baseObj[myString] references the function, but it might be returned as a generic object, therefore the cast to Function(). The 'null' implies to call the function itself from baseObj, instead of redirecting.

                  Like to hear if this works.