Writing and reading scoped variables via client side Javascript

Thursday, April 17, 2014 7:00 PM UTC

For the last 3 weeks my co-workers and me were working on a giant project that is really too complicated to be named here, but there is one detail I like to cover in this post: how to handle scoped variables flexible with client side Javascript?

Today I faced this problem: I need to call CSJS from a web page, sending some stuff to the backend (via REST call to a TomEE server), get something back and change the page. Before loading the next page I have to compute some stuff in the backend which lead me to the usage of scoped variables, e.g. sessionScope-stuff, as I don't want to take everything in the URL as parameters. But: how could I achieve that a session-scoped variable will be filled via client side Javascript?

Searching...

I google-found some posts about phase-listener-beans and wild mixtures of SSJS and posted form content, but - to be honest - I didn't even understand what these guys were talking about. I like the transparent way, my own code which I understand (and hopefully others, too) so I decided to create my own approach. This post is about how I did it.

If you know me a little you know I like it simple. Less code and even less extra stuff to reach the goal. I recall the times not using Domino but other application servers that use servlets. So why not using this knowledge to create a generic solution for fetching and storing data via the good old request/response model?

The good old HttpServlet

The core is a servlet (yes, a real HttpServlet-) class that is inititialized via a so called XAgent XPage. The servlet itself is defined as a request-scoped bean so there will be no hassle with recycling objects and stuff after the calling - I don't even use Notes objects here, just the ExtlibUtil and the JsonJavaObject classes. JSON is also a key here as I exchange the data in JSON format - both setting and getting. At this point I can only set one value to one key per request but in future releases I will extend my servlet to understand whole JSON objects with key-value-maps to store a bunch of scoped variables.

REST & JSON are your friends

The client calls are - of course - REST calls (e.g. Dojo XHR requests both GET and POST). They are defined in two simple functions in a CSJS library. There is the timing problem with asynchronous REST calls, so you have to code your own functions within the "positive" Dojo XHR function where the data object is provided. You cannot be sure to get a valid value returned from that function in time - even though I've set the call to be a synchronous one. I don't wanna post code here as you can examine it for yourself in the Github repository here: https://github.com/zeromancer1972/CSJSScopes

From this repo you can also create an NSF version for yourself to fiddle around with my solution. As I love live demos I put the database with a simple UI on my server, too: http://mardou.dyndns.org/scopes.nsf/index.xsp

This might not be the most elegant way to achieve that kind of "stunt" but it is my own one. If you have other/better suggestions please let me know in the comments Smile

Happy easter, by the way!

Update

I just noticed that my solutions also work in XPiNC - great! Grin






Leave a comment right here