I’ve seen a few threads on this topic, and I’d just like to vote for its inclusion. Here’s a simple example involving the svyCRM solution:
There’s a global method for opening an URL. However, if there are 10 different forms that have fields containing URLs, then you would need:
a) 10 “mini-scripts” which do nothing more than pass the field value, like globals.openURL(form1_url)
b) 1 “mega-script” which is basically a big switch statement
var formName = application.getMethodTriggerElementName();
switch(formName)
{
case 'form1':
globals.openURL(form1_url);
break;
case 'form2':
globals.openURL(form2_url);
break;
:
:
}
Neither option seems very efficient. Passing script parameters like this is about the only thing I miss from FileMaker.
var urlfield = application.getMethodTriggerElementName()
var formName = application.getMethodTriggerFormName()
globals.openURL(forms[formName][urlfield];
This way, you just have to give the button the name of the field you want to open as URL…
var formName = application.getMethodTriggerFormName();
var buttonName = application.getMethodTriggerElementName();
// I prefix button names with 'btn_'
var fieldName = utils.stringReplace(buttonName, 'btn_', '');
var theURL = currentcontroller.getDataProviderValue(fieldName);
application.showURL(theURL);
How might I pass multiple parameters to a method? I was thinking of adding more text to the button name, using some designated character as a delimiter. However, the only character that seems to work is ‘$’. I’m not sure why–does the dollar sign have special meaning in JavaScript?
Anyway, I guess I could start naming buttons like:
btn$parameter1$parameter2$parameter3$…etc. and then convert this string into an array using the split() function.
chartpacs:
How might I pass multiple parameters to a method? I was thinking of adding more text to the button name, using some designated character as a delimiter. However, the only character that seems to work is ‘$’. I’m not sure why–does the dollar sign have special meaning in JavaScript?
Anyway, I guess I could start naming buttons like:
btn$parameter1$parameter2$parameter3$…etc. and then convert this string into an array using the split() function.
What I’ve been doing is to create a global variable with my parameter(s). (The parameter can be a single value, a delimited list, an array.) If that variable is named myParameters, name the button btn_myParameters, and replace out the “btn_” string as needed to refer to the contents of the variable in your method.
Silly example … with a global variable named myParameters, containing the single value “boo”, and a button named btn_myParameters …
plugins.dialogs.showInfoDialog( "My Dialog",utils.stringReplace(application.getMethodTriggerElementName(),"btn_",""),"OK");
(scary how easy it was, huh)
At first I thought I missed the button param’s of FMP. But when you’re working in ScriptMaker you cannot see what the button parameters are (unless you go through the script with debugging on and use the data viewer).
But in Servoy, by separating the value of the parameters from the physical button on the form by using this method, you gain two big advantages:
Easy to re-use the parameter definition in other ways/places
Can see what the parameter is while coding your method
I’m still a little confused. Maybe I’m missing something.
I like your idea of keeping all the parameters in one location. How exactly do you organize them? In your example you have a global variable named myParameters, but how would you name a second button? Do you have to create another global variable?
If your parameters are only one word long each, you can just use spaces as the delimiter and parse out what you need in various places within your method using stringMiddleWords. You can also create a calculation (instead of a global) with your params, but it’s not as convenient to see the values while coding. btw, Please take the above code as pseudo code.
click on the global variable and you’ll see it’s default value in the status bar of the method editor … I’m pretty sure tooltips also pop up for global vars (as I remember, I do not have Servoy on this computer)
My solution is relatively big, so I might need a more formal way of organizing the parameters, perhaps hard-coded in a global array. I would obviously need to reference them by index, so a button name might look like ‘btn_28’ where gParametersArray[28] = ‘param1|param2|param3’, which I can then parse.
Thanks for the suggestion of keeping them separate. Certainly makes changes easier.
Sure, you could use a startup method to initialize the global, and this way you also have the values of the parameter visible to you while working in the method editor.
Now that I’m back at my main 'puter, here’s the not-so-pseudo-code, where your button is named btn_[globalName]:
var buttonParam = "globals." + utils.stringReplace(application.getMethodTriggerElementName(), "btn_", "");
plugins.dialogs.showInfoDialog( "My Dialog", eval(buttonParam), "OK");
In other words, move your global into a script variable (var) and then do what you wish with it …
btw, a third major advantage of divorcing parameters from the name of the physical button is:
You can use the same parameter in more than one place in your solution, and if you ever need to edit it you do not need to hunt down all instances of that button name in order to change the name (and, thereby, the value of the parameter) … instead you only need to change the value of the global variable (or only change the method that sets the global) and the parameter will be changed for all buttons of that name.
Naming convention hint: Indicate in your button name that it is used to extract parameters from a global, so you rememer to not rename it because doing so would break the method it’s attached to. Example: I use “btnp_” as my prefix, meaning “button with parameter”
Hi, one thought, here is another approach. I was using various global arrays until I learned about ‘virtual’ table columns at last years’ ServoyWorld. This trick simplifies things a lot and reduces the need for global variables and parameter passing.
I now have 10+ tables of user-interface related info (Menus and screens, sql searches, users’ search-selections, etc). Everything which is the same for all users stored in normal fields, all the stuff which is specific to a user-session in virtual columns…