Global Variable Arrays?

I would like to make a global variable that stores an array of every record id viewed in order so the user can go back and forward in the list.

I tried this:

globals.counter++
globals.prev_person_id[globals.counter]=globals.present_person_id
globals.present_person_id=person_id

but the array doens’t seem to work…

Is this possible?

I do something similar but for some reason (I don’t remember why) I don’t use a defined global, but a “global defined on the fly”. So my method starts like

if (lastRecs == undefined)
{
	lastRecs = new Array();
}

Where lastRecs is that global…

If that works for you, it might be worth a question why it doesn’t work with a global :wink:

Servoy “globals” are strictly typed (text, integer, etc.). When you create a JavaScript global variable (without using “var” in front of it - and not using a Servoy global) - that variable can hold anything (arrays, objects, recordsets, etc).

Hope this helps.

Sounds reasonable. When thinking about that I just wondered what happened if you put a global holding a multi-dimensional Array into a form and display that :lol:

You can use MEDIA type globals to store arrays

var myArray = new Array();
myArray[0] = “a”
myArray[1] = “b”
myArray[2] = “c”

globals.arrayStorage = myArray //global should be MEDIA type

for(var i=0 ; i< globals.arrayStorage.length ; i++)
{
application.output(globals.arrayStorage*)*
}

Maarten - COOL! I learn something new (and AWESOME!) about Servoy every day. 8)

Thanks!

I knew about that but somehow I remember that when I tried it didn’t behave right somehow. That’s why I used that lastRecs instead… Maybe that was an older issue that is solved by now.

I remember that when I tried it didn’t behave right somehow

We haven’t experienced any trouble using global arrays so far.
If you do have some spare time left, we’d appreciate it
if you could put it to a test again…urhmm..yeah sure, why not 8)…since I’m using Servoy…

Thanks.

Maarten,

In your example, you first created a variable as an array and then assigned this variable to the global of type = media.

Is that step necesarry?

What I tried to do is the following:
globals.abc = new Array();

And then in sevaral different methods, I try to assing values to the various positions in the array, like:

globals.abc = y;

But, these values never are stored in the global. is this correct behavior?

The only way I can get my logic to work is by creating a JavaScript global on the fly (abc = new Array():wink: and then it works like a charm.

To avoid memleaks, at the end of my method, I set this global variable to null (abc = null;). Is that code correct for clearing the variable?

Paul

1)Using globals directly as an Array won’t work.
(will ask Johan if this can be done)
For now, indeed, the only way to temp store arrays is declaring/setting them via javascriptObject and using a global (type=media) only as a storage place.

2)clearing globals that are used for larger storage, like html text or arrays,
is a wise thing to do since they may start building up and consuming lots of memory during the whole session of a client.

bcusick:
Servoy “globals” are strictly typed (text, integer, etc.). When you create a JavaScript global variable (without using “var” in front of it - and not using a Servoy global) - that variable can hold anything (arrays, objects, recordsets, etc).

Hope this helps.

Are all Servoy “globals” automatically put into memory when a solution opens, or are they only put into memory when they are used?

Dean

Ok…

Been following global array threads, and now am loading a global with an array (set as media).

The data is going in, and I can see its there.

Question:

Now, is there a way to reference the elements of the array with a blob loader or other method to display and print within servoy?

ie,

var fs_numb = globals.fs_temp_print.length
	for (var i = 1; i <= fs_numb; i++ )	{
		
		globals.fs_temp_print2 = globals.fs_temp_print2 + '<TD><img src= "media:///servoy_blobloader?global="'+fs_temp_print[i]+'"></TD>'
		
	}

or

var fs_numb = globals.fs_temp_print.length
	for (var i = 1; i <= fs_numb; i++ )	{
		
		globals.fs_temp_print2 = globals.fs_temp_print2 + '<TD><img src= "media:///servoy_blobloader?global=fs_temp_print[i]"></TD>'
		
	}

Neither of the above work. Can’t seem to reference the elements of the global array to show the images contained in them using the blobloader.

Is this possible? Any other method for dynamically generating an array of images to display in HTML?

The reason:

Our servoy application utilized chemical structures. The text data (mol format) is minimal to save in a db. We can display these in servoy itself using beans, but can’t print them very well (ie, getImage is very low resolution).

Our chemical beans have ways to generate nice, printable images BUT we don’t want to save these in the DB (ie, 1-15MB per image for a high resolution image of a large chemical structure - 300dpi).

We’re able to generate the images, store them in a global array (type media) but can’t get servoy to display this array to actually print.

Any ideas?

We could create a temp table for this, but would prefer to keep it in memory so that DB writes / deletes are avoided for these large, generated files and its kept on the client machine.

I read that in 3.1 + you can reference a global using the blobloader. Can this be updated to reference an array element of a global if its an array?

Or is there another method to do this? :slight_smile:

Thanks!

NCM
FSCI

Both code examples are not correct for sure.

The correct code would be:

var fs_numb = globals.fs_temp_print.length 
   for (var i = 1; i <= fs_numb; i++ )   { 
       
      globals.fs_temp_print2 = globals.fs_temp_print2 + '<TD><img src= "media:///servoy_blobloader?global=fs_temp_print['+i+']"></TD>' 
       
   }

You have to merge the value of “i” into the string, not “i” itself, or the value of “fs_temp_print*”.*
Can you try if that works? If not, I the syntax you’re trying is not supported.
What I then suggest is instead of creating a global containing an Array that contains the images, create new globals on the fly that contain the images. You can just do:
* *globals.xxxxx = yyyyy* *
Where xxx is the name for your new global and yyyyy is offcourse the value, in this case the image.
If you do it this way, you can reference these new globals in the blobloader.
Do note that you are now in control of clearing the newly created globals when you do not need them anymore. This means that if you do not need them anymore, you have to set theit value to null:
* *globals.xxxxx = null;* *
If you do not do that, the globals will stay in memory for the entire duration that the client session exists and when you create quite a few of them and put 1 - 1.5 Mb images in them, you will soo need a lot of memory…
Hope this gets you further.
Paul

pbakker,

Thanks for looking. I was just trying all kinds of syntax to see if was possible.

I did use try the [‘+i+’] notation as well yesterday - still a no go.

All I got were broken images, so the blob loader most likely isn’t array friendly.

So if I auto-generate several globals, and use them in a method that returns a print preview - what is the best trigger to guarantee that after the print preview is closed the clean up method runs?

Thanks,

NCM
FSCI

If I’m not mistaken, when you call showPrintPreview, the rest of the script halts, untill the user has either printed or canceled the printPreview.

So, you could cleanup straight after calling the printPreview, I think.

Basically: you need to cleanup when you don’t need them anymore.

Paul