Passing Parameters to Scripts Attached to Events

Discuss all feature requests you have for a new Servoy versions here. Make sure to be clear about what you want, provide an example and indicate how important the feature is for you

Passing Parameters to Scripts Attached to Events

Postby chartpacs » Sat Jul 22, 2006 8:09 am

Hello,

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
Code: Select all
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.

Regards,
Sean
User avatar
chartpacs
 
Posts: 188
Joined: Wed May 10, 2006 6:50 pm
Location: Cleveland, OH

Re: Passing Parameters to Scripts Attached to Events

Postby Riccardino » Sat Jul 22, 2006 10:37 am

What about
Code: Select all

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...
ciao, ric
User avatar
Riccardino
 
Posts: 911
Joined: Thu Apr 24, 2003 11:42 am
Location: Ferrara, Italy

Postby chartpacs » Sat Jul 22, 2006 11:32 pm

Hi Riccardino,

I got this to work:
Code: Select all
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.

I'd still like a more formal way, though.

Thanks for your help,
Sean
User avatar
chartpacs
 
Posts: 188
Joined: Wed May 10, 2006 6:50 pm
Location: Cleveland, OH

Postby kazar » Sun Jul 23, 2006 1:33 am

chartpacs wrote: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 ...

Code: Select all
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:

1. Easy to re-use the parameter definition in other ways/places

2. Can see what the parameter is while coding your method

kazar
User avatar
kazar
 
Posts: 367
Joined: Sat Sep 13, 2003 9:59 pm
Location: New York City

Postby chartpacs » Sun Jul 23, 2006 2:01 am

Hi Ilyse,

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?
User avatar
chartpacs
 
Posts: 188
Joined: Wed May 10, 2006 6:50 pm
Location: Cleveland, OH

Postby kazar » Sun Jul 23, 2006 2:51 am

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.
User avatar
kazar
 
Posts: 367
Joined: Sat Sep 13, 2003 9:59 pm
Location: New York City

Postby chartpacs » Sun Jul 23, 2006 3:16 am

Oh I see. You just have one big text field to parse, like:
utils.stringMiddleWords(globals.myParameters, 8, 1)

Silly question: How do you see what the parameter is while coding?
User avatar
chartpacs
 
Posts: 188
Joined: Wed May 10, 2006 6:50 pm
Location: Cleveland, OH

Postby kazar » Sun Jul 23, 2006 3:40 am

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)
User avatar
kazar
 
Posts: 367
Joined: Sat Sep 13, 2003 9:59 pm
Location: New York City

Postby chartpacs » Sun Jul 23, 2006 5:31 am

Ilyse, thanks.

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.

Regards,
Sean
User avatar
chartpacs
 
Posts: 188
Joined: Wed May 10, 2006 6:50 pm
Location: Cleveland, OH

Postby kazar » Sun Jul 23, 2006 6:49 am

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]:

Code: Select all
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:

3. 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"

kazar
User avatar
kazar
 
Posts: 367
Joined: Sat Sep 13, 2003 9:59 pm
Location: New York City

Postby swingman » Sun Jul 23, 2006 8:53 am

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...
Christian Batchelor
Certified Servoy Developer
Batchelor Associates Ltd, London, UK
http://www.batchelorassociates.co.uk

http://www.postgresql.org - The world's most advanced open source database.
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Postby chartpacs » Sun Jul 23, 2006 9:14 am

Hi Christian,

This "virtual column" trick sounds really neat. Could you give an example of how they work?
User avatar
chartpacs
 
Posts: 188
Joined: Wed May 10, 2006 6:50 pm
Location: Cleveland, OH


Return to Discuss Feature Requests

Who is online

Users browsing this forum: No registered users and 13 guests