Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing javascript variables across popups / singleton pattern

 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an easy way we can share variables across popups? I have a web application that displays documents to the user. It allows user to lock/unlock documents for editing. If one user has locked a document, the other users see a "lock" message. This message is displayed in several places, including popups

Now, because some users forget to unlock their document, or they close the browser halfway, "admin" users have "terminate lock" permission. The Admin user can terminate another user's lock.

My problem is that if a admin user is viewing a document with the lock message on the main screen. He opens a popup which again displays the lock message. The lock message is displayed on the popup. He terminates the lock on the popup, so the message goes away on the popup and he can continue working. Now, if the user closes that popup, the lock message is still displayed on the main screen.

Is there a good design pattern that I can use so that once the lock message is cleared from one place, it gets cleared in all places. SOmething like a singleton pattern, but which goes across popups. My understanding is that javascript variables are not shared across popups. So, if I have a javascript variabale var isLocked defined in lockInfo.js, and I include that js file in 2 seperate jsp pages, then each jsp page will get it's own copy of isLocked
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Play with this:

window.opener.foo = "bar";

It sets a variable in the parent window.

Eric
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thank you. I thought about it, but I wanted to avoid navigating across frames and windows.

a) If the opener is in a frame, and the variable is outside the frame I have to do window.opener.top.foo = "bar". If the javascript that checks for the variable is inside a frame itself, then I have to do top.opener.foo = "bar" or maybe top.opener.top.foo="bar". THe code becomes a bit messy because the person reading the code will have to mentally walkthrough the frames and the popups

b) If I change the layout of the frames, etc, I will find that my javascript code will start breaking

I guess I didn't give sufficient information. I know how to access variables across windows, frames etc. However, I want to avoid explicitly going across windows and frames to get the the variable. As far as possible I would avoid having windows and frames dependent on other windows and frames. I was wondering if there is a well-established pattern that allows multiple frames/popups to share data.

Since, I am more familar with Java, I was thinking that if I wanted to do something like this in Java, I would have a Singleton pattern coupled with listener pattern. A singleton object can wrap the state of the lock. It can also accept listeners that can be fired whenever the state of the lock changes. So, all the "views" (and I consider the popups and the frames as views) can register to receive the events. Whenever the state of the lock changes, the views will receive the event and update themselves

I am getting stuck because I don't know how to have a singleton object across frames/popups. In Java, if I have static functions, I can access them from anywhere in the same app. So, I can have a static factory method that returns a singleton java object. But, how do I do that in Javascript?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple answer is no, JavaScript has no locking, has no session. You have to rely on what you have.

You can think of fancy soultions that use Ajax and what not, but it seems too complex.

I think that if your user closes a pop up window you should just refresh the window.opener and have it auto update. Personally I hate frames and pop ups since they over complicate designs and are not reliable.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java != JavaScript

There is no place (like a JVM) for objects to exist independent of loaded pages.

Objects must exist in a loaded page, and you must perform cross-page referencing to access them from elsewhere.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Thanks both. Since, there is no way to avoid cross-page referencing, I guess that's what I have to do *grumble* *grumble*
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic