Joshua Ohana
Joshua Ohana

Reputation: 6121

How to manage (open, close, resize) multiple windows

I am building a large application using AngularJS. The user lands on the login page, and upon login I open up one or two windows. Once open, based on various criteria, the windows will resize, close, or spawn more children. My issue is managing these!

My Angular app can only reference either its direct parent window or windows it has spawned, but not all since the scope is limited to its window. I have tried storing the window objects in localStorage/cookies but that doesn't work due to circular references in the JSON.stringify. I'm at a bit of a loss here on how to structure in a programatic way without hard-coding everything which will not work long term.

I have tried the below obviously, but it won't work because I need to be able to reference any window from any window. And I can't seem to store the var x anywhere.

var x = window.open(...)
  1. How can I programmatically store and manage (close, open, resize, reposition) multiple windows in my app.
  2. If an elegant solution does not exist, how can I store & retrieve window objects?

UPDATE

Okay looks like I figured out a way to solve this. I'm storing an array of window IDs in localStorage. Then any time I need to mess with one I read it in, call window.open(null, id).function()

Since window.open returns a reference to the already open window it seems like this should work. Still would love any additional opinions...

Upvotes: 1

Views: 220

Answers (1)

voidptr
voidptr

Reputation: 34

Not sure if this solution helps, but this is what I did. I created an angular app with multiple divs which can be resized, closed etc. where each div has its own controller. So I used the $rootScope as an event bus and posted all events to it.

Thus each controller can post and subscribe to the events on $rootScope.

Controller1

$rootScope.$broadcast('Event', { 'key': value });

Controller2

$rootScope.$on('Event', function(e, data){//process data});

You can also create a service to store and share the window ids, since services are singleton.

Upvotes: 1

Related Questions