FEATURE: Globally scoped Javascript variables?

This may be my lack of expertise with Javascript but it would be nice to have variables that are global in scope. I know I can create a global field currently. I guess my question is, if this global is created in memory but stored in the repository as part of the solution then is this the same thing?

My thought about globally scoped variables was to initialize some as part of an onLoad method.

I can see that management might be an issue and you would have to determine variable dominance if there were duplicates but you could allow this feature as part of an onLoad and then flush the variables once a new method instantiated variables with the same names. Of course, since Javascript is a runtime environment, I think, this may not be possible as implemented in Servoy.

Just a though.

Hi Matt,

“globals” are variables and NOT columns in the database. They only “exist” in the Servoy environment. In 1.2rc6 you can also set “default” values for them - so you don’t have to load data into them when you startup your solution.

You can make as many method-specific “local” variables (within your method) as you want. (var x = 10)

The global variables were meant as sort of a “container” that can hold data (if you didn’t want to pass arrays to other methods, or for user-specific data [each client gets their own “copy” like in FMP]).

Hope this helps,

Bob Cusick

additional note:
When you declare in a script x=10 instead of var x = 10,
variable x will be usable by all scripts in the solution.
(Kind of a temp global)

When adding new records to a file, I like to have the user enter field data into variables on a form so that they can be edited by methods before the entry is accepted and added as a record to the table. Do I need to set up global variables on the form for these fields, or can method variables be used; and if the latter, please include some sample code to explain how they are referenced.

javascript variables can’t be put into your form “physically”.

As creating a bunch of globals may be very tedious
you might want to try using transactions.
If you’re not satisfied with the entries of the user,
you can roll back to your starting point.

//newRecordMethod(when creating new ecorrd)>
databaseManager.startTransaction()
controller.newRecord()

//closeMethod (when leaving the form again)>
if(myChecksAreValid)
{
databaseManager.commitTransaction()
}
else
{
databaseManager.rollbackTransaction()
}

maarten:
additional note:
When you declare in a script x=10 instead of var x = 10,
variable x will be usable by all scripts in the solution.
(Kind of a temp global)

Bamn!!! That is exactly what I was looking for. I was looking for scope.

So, just to clarify. Designating a variable by declaration using var will make it local in scope. As opposed to…

Simply assigning a value to a variable makes it global in scope for as long as the app is open.

Perfect!

It’s indeed great to use these in “solution scope”.
Upside> no need to create endless globals
Downside> can get pretty chaotic, because there’s no central list to keep track of them. If you declare x=10 in one script and forget about it, you might end up happily creating another script in a total different place that says x = 15… well, these 2 scripts might screw each other up, leaving you searching for the reason.
So be carefull out there :wink:

Here’s another way of parsing variables and avoiding double coding:

create global script that does a common task


//scriptName =scriptMultiplier
var x = arguments[0]
var y = arguments[1]
return x * y

an other script anywhere

//scriptName = scriptInAnyFormThatTriggersTheMultiplierScript
var result = globals.scriptMultiplier(4,5);

what happens:
When you activate the second script, the 4 and 5 are sequentially stored inside x and y in the global script.
The global script calculates and returns 20 back to the second script. var result ends up being 20.
note: of course you can also parse database columns