Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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.

— asked July 19th 2010 by David Cranwell
  • android
  • app.js
  • eventlistener
  • favorite
  • tabs
0 Comments

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).

    — answered July 19th 2010 by Jean-Philippe Boily
    permalink
    0 Comments
  • 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?

    — answered July 19th 2010 by David Cranwell
    permalink
    1 Comment
    • If you want to call a function from another window, the easiest way (I think) is to write that function in an external file and include it in each window you might need it.

      functions.inc.js

      function myGlobalFunction(param1){
         ...
      }
      

      win1.js

      Titanium.Include('functions.inc.js');
      
      myGlobalFunction('my win1 Value');
      

      win2.js

      Titanium.Include('functions.inc.js');
      
      myGlobalFunction('my win2 Value');
      

      I don't think there is global dictionnay…you can't call let's say win1.myFunction(value).

      — commented July 20th 2010 by Jean-Philippe Boily
  • 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?

    — answered July 19th 2010 by Mark Pierce
    permalink
    3 Comments
    • Cheers Mark, thats certainly the kind of thing i'm after however adding an event listener to the favorites window, within the JS file for the favorites window, appears to have no effect.

      app.js:

      var win1 = Titanium.UI.createWindow({  
          title:'Latest',
          backgroundColor:'#EEE',
          url:'latest.js'
      });
      var tab1 = Titanium.UI.createTab({  
          icon:'KS_nav_ui.png',
          title:'Latest',
          window:win1,
      });
      
      var win2 = Titanium.UI.createWindow({  
          title:'Favorites',
          backgroundColor:'#EEE',
          url:'favorites.js'
      });
      var tab2 = Titanium.UI.createTab({  
          icon:'KS_nav_ui.png',
          title:'Favorites',
          window:win3
      });
      
      tabGroup.addTab(tab1);  
      tabGroup.addTab(tab3); 
      
      tabGroup.open();
      

      favorites.js:

      var win = Titanium.UI.currentWindow; 
      var tab = Titanium.UI.currentTab;
      
      win.addEventlistener('focus',function(){
      alert('hello')
      })
      

      It seems that on android the JS script for a window is only ever run the first time its called, but in the above example the focus event doesn't even run when the app first starts or when I first click on the favorites tab! Nor does it run when I click between the two tabs any more times.

      I'm guessing that I should probably addEventListener to 'win2' in app.js but if I did that I can't work out how to target my event listener function at the calling window.

      — commented July 19th 2010 by David Cranwell
    • Sorry I tell a lie - focus() is called the first time the tab is viewed, but doesn't get called any time I click the tab thereafter.

      — commented July 19th 2010 by David Cranwell
    • Hey Mark, I just wanted to thank you for this answer. 2 years later I found this thread via Google and this answer was exactly what I was looking for. Thanks!

      — commented May 4th 2012 by Casey Crookston
  • 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 !

    — answered July 19th 2010 by Jean-Philippe Boily
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.