Hi,
I’m trying to build a menu from a database of actions.
I want to do something like:
var menu = plugins.window.createPopupMenu();
menu.addmenuItem(my_text,my_action);
my_text is the label for the menu
my_action is a string containing the name of the method to execute.
This does not work since Servoy expects my_action to be a method.
So I tried:
var menu = plugins.window.createPopupMenu();
menu.addmenuItem(my_text,solutionModel.getForm(event.getFormName()).getFormMethod(my_action));
This does not work. How can I get this to work?
You need to use eval here.
var myFunc = eval(my_action)
then pass myFunc in. Just make sure my_action doesn’t included the (). So it should be like globals.method_name, NOT globals.method_name()
Thanks, that works fine. I have had some problems in my solution since I used eval to actually execute code. I’m trying to remove all this now.
I guess using eval() simply to get the method is OK.
why would you use eval there?
why not just:
var menu = plugins.window.createPopupMenu();
menu.addmenuItem(my_text,globals.my_action);
(so not as a string and not with () after it)
Or is your my_action not really a “top level” method of java?
also with this:
solutionModel.getForm(event.getFormName()).getFormMethod(my_action)
you mix runtime and solution model stuff.
That call will not return a js function object but a Solution model ScriptMethod object. Which is the persist object (you can see this as the text file you have in eclipse for a script)
The problem is when you have a record in the db, and the value in the record is “globals.myaction” or “forms.formname.myaction”, you can’t easily pass that into things like the popup menu, or the headless client plugin, so you have to use eval to get the method object. Although I suppose you could try and parse it and turn it into javascript dot notation.
you can easily pass that in
globals[record.methodname]
or
forms.formname[record.methodname]
please try to avoid eval… its slow.
Of course if it has a full path you need to parse it first…
forms[record.formnameportion][record.methodnameportion]
if (record.methodname startswith “globals.”) globals[record.methodname.substring(8)]
things like that