How to call function on load of window
I'm using a tabgroup which has a tab that is associated to a window (external js file.) At the bottom of the external js file I call a function "loadData();" which retrieves and displays the data for that window.
When I first click on the tab it loads the data just fine. However, if I go back to the home window then click on the tab again, it doesn't call loadData() this time.
How do I get it to call loadData() every time that tab is clicked? I tried this but it did nothing:
Ti.UI.currentWindow.addEventListener('focus', loadData);
5 Answers
-
hi justin
Try this
Ti.UI.currentWindow.addEventListener('open', loadData);
if you cant set it right with the above code then take a look at the scope cause variables wont be available across windows…
-
Thanks for the reply but it didn't work, loadData never gets called, not even on the first tab click. Did I mention this is Android? In case that makes a difference…
-
Hey justin,,
i think it might be due the scope of the variables… can you post us some sample code so that i can check if it was due to the scope…
i really ran into this kind of problem in my project last week….
-
Sure thing. Here is my app.js:
// this sets the background color of the master UIView (when there are no windows/tab groups on it) Ti.UI.setBackgroundColor('#000'); //create windows. var winHome = Ti.UI.createWindow({ title:'Home', backgroundColor:'#fff', url: "home.js" }); var winScoreboard = Ti.UI.createWindow({ title: 'Scoreboard', backgroundColor: '#fff', url: "scoreboard.js" }); //create tabs. var tabGroup = Ti.UI.createTabGroup(); var tab1 = Ti.UI.createTab({ icon: 'KS_nav_views.png', title: 'Home', window: winHome }); var tab2 = Ti.UI.createTab({ icon: 'KS_nav_ui.png', title: 'Scoreboard', window: winScoreboard }); tabGroup.addTab(tab1); tabGroup.addTab(tab2); tabGroup.open();
And here is the relevant part of my scoreboard.js:
//global vars. var console = { log: Ti.API.info }; //controls. var win = Ti.UI.currentWindow; var progress = Ti.UI.createActivityIndicator({ height: 50, width: 10 }); //load the sports then the games. win.addEventListener('open', loadSports); function loadSports() { //show progress. progress.message = "Loading sports..."; progress.show(); //make ajax call to get sports. var loader = Ti.Network.createHTTPClient(); loader.open("GET", url + "/GetSports"); loader.onload = displaySports; loader.send(); }
-
I realized that there were a few bugs (really more than a few) in Appcelerator that were causing the issue and was able to work around it:
- Window events don't get fired when switching tabs on a tab group. Many others have reported this bug.
- When clicking on a tab for a second time, it's supposed to show the tab's content as it was from the first click. However there are screen repainting bugs with Android related to this. Two cases I saw where the screen wouldn't repaint the content:
a. When setting clearOnEdit on a TextField, the second time you go to the tab it will show the TextField as blank.
b. When having content in a view within a table row, all of the content within the view will disappear and all you can see are the empty rows.
How do I know that it's a repaint issue? All you need to do is scroll down and you'll see the rows populated, scroll back up and the rows that were empty now are populated.
I really hope that they put some time into making things work for Android, I've run into more bugs than I can count in just a week of dev.