Jan 6, 2012 9:53 AM
38 Posts
topic has been resolvedResolved

Declaring global variable in XPage

  • Category: Server Side JavaScript
  • Platform: Windows
  • Release: 8.5.3
  • Role: Developer
  • Tags: Xpage,xpages
  • Replies: 7
How can we declare a global variable in SSJS that can be used anywhere in an XPage? 
 
Basically I want to have a database object initialized once and then used anywhere or any control in XPage. For e.g.
 
var processDB:NotesDatabase = session.getDatabase("<SERVER NAME>", "<DATABSE NAME>"); 
 
Then I want to be able to use the processDB object in computed field, input text, etc in the same XPage so that I won't have to initialize the object again and again.
 
While experimenting I found that if I put the code in beforePageLoad, afterPageLoad & beforeRenderResponse I was able to use the object. But is it the standard way? Is there a better way to do it?
Jan 6, 2012 10:06 AM
586 Posts
Re: Declaring global variable in XPage
 I'm kind of brain fried from some other stuff I'm working on...
 
I'm not sure I've tried it that way...  I think I'm more apt to put that code into a SSJS function in a script library and just import that.  I guess the object might not "live" and it would be recreated each time but I'm not sure if that's a real concern from a performance basis. 
 
I could be wrong I guess...  But still I'd rather use a function...
Jan 6, 2012 1:24 PM
116 Posts
Re: Declaring global variable in XPage
dataContexts may be your answer here. They're very easy, they just have two properties of note -
 
  • var, the name by which you want to reference it
  • value, which you can compute with SSJS
They can be scoped to an XPage, a Custom Control, or a Panel. So they're very flexible. The only slight issue is you have to go to the All Properties panel to access them (just below data). Once you use them, you'll use them a lot.
 
If you're calculating everything in the dataContext, you shouldn't encounter any problems with recycling of your Domino objects.
Jan 9, 2012 9:57 AM
288 Posts
Paul, do you have an example of this?
In particular, how you would store a domino doc? How would you access it on a control? Are these a solution to storing Domino data in a "session scope" type variable (since that can't be done)? thanks, Howard
Jan 7, 2012 11:44 AM
38 Posts
Re: Declaring global variable in XPage
 Thanks Paul! It worked.
Jan 12, 2012 10:32 AM
586 Posts
Re: Declaring global variable in XPage
 Howard,
 
you can't store a document object in Scope because it can't be serialzed.  The solution, i THINK, if you want to do something like that is to convert the notesItems into a hashMap.   
Jan 13, 2012 9:48 AM
288 Posts
*I know, but, Paul said you can store it in the context...
Jan 26, 2012 11:55 AM
4 Posts
Re: Declaring global variable in XPage
Hi,
 
I'm having trouble with this - it seemed to work to start with, but I'm now having problems.
 
Like you, I am trying to make a global variable of a Notes object (a view in one case and a document in another)
As far as I can see the DataContext variables are subject to the same rule/problem as scoped variables, in that they cannot be domino objects.
I get the page come up correctly the second time I access it (the first time the db path that I need to find them is not set in applicationScope, but I'll worry about that later).
The third and subsequent accesses I get an error: 
java.io.NotSerializableException: lotus.domino.local.Document
lotus.domino.local.Document
 
To get out of this I need to restart the http task on the server.
 
I did notice that if I set the application properties such that the xpages are all cached in memory the problem goes away. Whilst I'll probably do that for the production system as long as the load is not too high, I'd rather do this properly now to prevent real problems in future.
 
So do I have to move back to putting them in the beforePageLoad (which like the top question I was going to do originally) or is there something I've missed?
 
Thanks
Tom.