Refreshing one window from another
I've got an application with 2 tabs. Each tab is controlled by its own script.
The first tab (tab1.js) is a feed of items. The second (tab2.js) is a 'favorites' tab.
I'm trying to get it so that 'favoriting' one of the items in tab 1 triggers tab 2 to update automatically, to show that new favorite item.
My favorites are stored in a DB, so the new favorite appears if you stop/start the app again, but this is obviously undesirable.
Is it possible to add an event listener to app.js or tab1.js which calls a function in tab2.js? I've seen other responses that suggest adding event listener functions in app.js, but this doesn't keep the logic of each tab apart, which seems rather clumsy.
4 Answers
-
Mark is right if you do it on the focus event, as I think you should probably do it(else you might generate the favs Window a bunch of times before getting in it, which is useless and could lead to poor performances).
-
Perhaps a more terse description of this problem is: I need to be able to target a root window, or a function defined in a root window, from a different root window.
Is there a global dictionary of the root windows that have been created or anything? At least then I could call fireEvent on one of them.
Any more ideas guys?
-
Hi Dave
I had to do something like that.
assuming on tab1.js you insert into table and on tab2.js you have the following
win.addEventListener('focus', function() { getData(); });
getData() function is
function getData(){ data = []; var rows = db.execute('SELECT * FROM fav'); etc....
does that help?
-
Maybe you should not refresh it completly until it get focus. You could update the number of badges and when the favs windows get focus, then you update ?
Anyway, here's how I would probably do it :
Include a JS file in the two windows, something like ui.favs.inc.js or whatever you want to name it. You include that and then with your custom listener you've got to fire a function from that file that controls your favorite UI or favorite tab badge. I do something like that in an app I am currently working on.
That might not be really clear, sorry, but I still hope that helps a bit !