I am having an issue getting a popup menu to accept a variable for the function to call onClick of a popup menu item. Here is the code:
var _jsMenu = plugins.window.createPopupMenu();
var _jsMenuItemName = 'test'
var _jsMenuItemIcon = 'media:///test.png';
/** @type{JSMethod} _jsMenuItemMethod */
var _jsMenuItemMethod = _jsForm.getFormMethod('logout');
item = _jsMenu.addMenuItem(_jsMenuItemName);
item.setMethod(_jsMenuItemMethod);
I get a problem in Developer on the last line as follows:
The function setMethod(Function) is not applicable for the arguments (JSMethod)
I’ve tried type casting the _jsMenuItemMethod variable to {Funtion} as well as {String} and {JSMethod} but nothing works. The only way I’ve been able to make it work is to actually type the method name into the setMethod call like this:
item.setMethod(logout);
Also, if instead of using the setMethod function, I set the method in addMenuItem directly like this:
AFAIK you don’t need to get the JSMethod in order to use it in the menuitem.
just do:
var _jsMenu = plugins.window.createPopupMenu();
var _jsMenuItemName = 'test'
var _jsMenuItemIcon = 'media:///test.png';
var _form = 'myForm'
var item = _jsMenu.addMenuItem(_jsMenuItemName);
item.setMethod(forms[_form].logout);
Did you take a look at the sample code?
The samples are pretty clear on this
I may have not explained it clearly enough, in that I do not want to have to type the actual name of the method (in this case ‘logout’) into the setMethod procedure. The menus are being built dynamically and the method that each menu item will need to execute is located in a database. The value for the ‘method’ is loaded from the foundset and needs to be populate the setMethod call using that data. So the example you provided, and that the samples show for that matter, do not apply. I’m back to having to figure out how to get setMethod to accept the variable value of the method name.
Ok, so I figured out a way to get it to work. I’m not sure however if this is how it should be coded, but it works for now. So, the solution is to use an eval() to evaluate the string being pulled from the database, then assign it to a variables that I have scoped as a function as follows:
In the code above, ‘menuItems[3]’ holds the value ‘logout’. In the original code I had _jsMenuItemMethod scoped as a JSMethod however setMethod requires me to scope it as a Function.
So, that leads me to ask, what is the difference between a Function and a JSMethod? Also, I have only started working with Servoy in the last few months, so if some of my questions seem remedial it’s because I’m a noob.
A Function is a runtime instance of a javascript method, that is something you can call with parameters.
A JSMethod is part of the solution model which is just a description of a javascript method, you can do stuff with the code and name, but you cannot call it.