Titanium Community Questions & Answer Archive

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

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.

— asked November 3rd 2010 by Chad Nantais
  • focus
  • xhr
0 Comments

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)

    — answered May 19th 2011 by Shaun Chan
    permalink
    0 Comments
  • 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

    — answered November 5th 2010 by Aaron Saunders
    permalink
    0 Comments
  • 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!

    — answered May 19th 2011 by Todd Wade
    permalink
    2 Comments
    • Guys, possibly unlike the protocol in other forums, when a thread has died and does not relate to the current Titanium version, it is much better for you to open a new question. This way, you have full control of it, and can mark it closed when your issue is resolved. This will help others, because they will then easily find the solution in the search results.

      Todd - my advice to you first is to simplify your usecase, get it working, and then restructure the code the way you require. Once you have a better usecase, create a new question here, and then I will help you further.

      Thank you

      — commented May 19th 2011 by Paul Dowsett
    • Thanks for the response Paul. I got better results by doing something like this:

      if ( CartTable ) {
        CartTable.setData([]);
      }
      

      instead of win.remove()ing the "old" table and creating a new one.

      As far as creating new posts, I've created a few new questions and I never get replies:

      http://developer.appcelerator.com/user/1254049/todd-wade

      Those are even a little easier to answer as they are a bit more abstract in the question, yet nothing. So I piggybacked here. I'll march on with creating new questions from now on.

      — commented May 24th 2011 by Todd Wade
  • 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()
    {....};
    
    — answered May 23rd 2011 by Giancarlo Rotta
    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.