Reloading a tab that uses xhr
Is there any other way to force a tab to make its xhr call and reload data besides putting it in the focus event?
If I don't use focus to fire the discreet function that defines my xhr code, then the data doesn't reload when I return to the tab. But if I use focus to call the discreet function, then the app crashes when I return to the tab, providing no description of what caused the crash in the Titanium console.
4 Answers
-
I'm afraid i need to ride on this question, because i have the same problem, somewhat.
I have a tabgroup and each page has an XHR call. i need to refresh each page with a new XHR call somehow… focus event is not the solution. for some reason, 'focus' is fired 3 times everytime u click a tab. (probably why it's causing your app to crash)
-
I am assuming you have a window or a view inside the TabControl? You might try moving you XHR code to the open event in another control?
In My application, I use the open event for those kind of actions… dont know if it makes a difference or nt
-
I'm having the same thing and could use help with it. In windows/checkout.js, I have just:
Ti.include('../redux.js'); var win = Titanium.UI.currentWindow; win.addEventListener('focus', MyApp.UI.Checkout.CartDisplay);
and then MyApp.UI.Checkout looks something like the code below. It works sometimes, but occasionally (frequently, even) crashes the app.
var CartTable; MyApp.UI.Checkout = { CartDisplay: function() { MyApp.API.Store.cartContents({ callback: MyApp.UI.Checkout.CartView }); }, CartView: function() { var cart = JSON.parse( this.responseText ); var win = Ti.UI.currentWindow; if ( CartTable ) { win.remove( CartTable ); } var tableview = new TableView({ id: 'CartWindowTableView' }); var data = []; // build array of TableViewRow objects tableview.data = data; CartTable = tableview; Titanium.UI.currentWindow.add(tableview); } }; MyApp.API.Store = { cartContents: function(args) { // returns cart contents var xhr_cart = Ti.Network.createHTTPClient(); xhr_cart.open("POST",MyApp.config.api.url); xhr_cart.onload = args.callback; xhr_cart.send({ app: 'cart', method: 'contents' }); } };
This code looks pretty solid to me, so if anyone knows a better pattern to keep the app from crashing, I would appreciate some feedback!
-
I have the same issue. I'm doing a News Reader, so from the channels list the app must open a particular channel using xhr, but this work only the first time, if a use win.addEventListener to "focus" works… but is this the best solution?
var win= Ti.UI.currentWindow; var data = []; var xhr = Ti.Network.createHTTPClient(); var urlchannel="http://v2.0.news.tmg.s3.amazonaws.com/feeds/news.xml"; win.addEventListener("focus", function(){ urlchannel = Titanium.App.Properties.getString("url"); xhr.open("GET",urlchannel); xhr.send(); }); xhr.onload = function() {....};