Titanium Community Questions & Answer Archive

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

Reload App.js?

Hi, I'm making an app with a language setting.

I'm first setting the language to the iPhone locale in app.js
like this:

if (!Titanium.App.Properties.getString('locale')) {
    var language = Titanium.Platform.locale;
    Titanium.App.Properties.setString('locale',language);
}

On a settings page I then override this with a new setString.
This works good, however I need to restart the app for the change to take effect.

I'm calling the language files in a included file like this:

var language = Titanium.App.Properties.getString('locale');

if (language == 'sv') {
    Ti.include('json/lang/swedish.js');
}
else if (language == 'en') {
    Ti.include('json/lang/english.js');
}
else if (language == 'de') {
    Ti.include('json/lang/deutsch.js');
}

This is included on every page. So basically I need to refresh app.js, is there any way to do that? Or a better way to incorporate a language setting?

Thanks

— asked May 28th 2010 by Mattias Svedhem
  • app.js
  • iphone
  • language
  • refresh
0 Comments

4 Answers

  • Accepted Answer

    there's no direct way to reload the app.js or main application context.

    to probably do what you're talking, you'd want to put everything in a new window which is a separate JS context (vs. app.js) and then reload that window.

    your window could simply be a non-visual window that opens other windows, tabs, etc. but could then be used to reload (by closing and re-opening) on a language change.

    — answered May 28th 2010 by Jeff Haynie
    permalink
    0 Comments
  • maybe you can do this with a little trick by using Titanium.App which is available in the whole app:

    var language = Titanium.Platform.locale;
    Titanium.App.Properties.setString('locale',language);
    Titanium.App.language = language;
    

    when calling your language files simply use:

    var language = Titanium.App.language;
    

    so you have the setting saved for the next run of the app and also available for actual run.

    hope that helps.

    — answered May 28th 2010 by Markus Gerlach
    permalink
    0 Comments
  • Exactly what do you mean with
    "which is a separate JS context (vs. app.js) and then reload that window."
    ?

    — answered May 28th 2010 by Mattias Svedhem
    permalink
    0 Comments
  • In your app.js, create a new window to another file:

    function reopen()
    {
      var window = Ti.UI.createWindow({url:"myapp.js"});  
      window.open();
    }
    reopen();
    
    Ti.App.addEventListener('reload',reopen);
    

    Then in myapp.js when you want to reload the app, call

    Ti.App.fireEvent('reload');
    
    — answered May 28th 2010 by Jeff Haynie
    permalink
    1 Comment
    • Tried this and it made the app unresponsive (nothing is interactive) after it loads the js file. Ti.App.restart() doesn't work anymore either. I wish it was easier to simply kick the app in the pants for a quick refresh.

      — commented August 24th 2011 by Jim Carter III
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.