Titanium Community Questions & Answer Archive

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

Global variables in titanium

I want some global variables in Titanium.
Is there any way to set these variables.
Please Help
Thanks..

— asked November 10th 2010 by Jacob John
  • iphone
0 Comments

8 Answers

  • Ti.App.myGlobalVar = 'something';
    
    — answered November 10th 2010 by Roger Chapman
    permalink
    0 Comments
  • Not quite.. but you can set/retrieve properties..

    Have a gander at…
    http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.App.Properties-module

    — answered November 10th 2010 by Critter
    permalink
    0 Comments
  • Variables you declare in app.js will be available on that page, as well as within any file that you include with Ti.include(). However, they will not be available in files associated with windows/views that you link in via those containers' url property.

    // vars you set like this
    var my_var = 124;
    
    // will be available in included files
    Titanium.include('my_other_file.js');
    
    // but not in linked in windows like this
    var win = Titanium.UI.createWindow({
        url:'mainwin.js',
        ...
    });
    

    The 1.4 branch lacks support for linked in CSS style sheets as you'd use with a web page. To simulate that, I create a js file declaring constants that set colors, backgrounds, etc. Then, as needed, I include that file in each of my app's other files. It won't work to pass values between files or persist values. For that, use Properties or the database as others have suggested.

    — answered November 10th 2010 by Tim Poulsen
    permalink
    2 Comments
    • is there a way to update variables and get the updated value in linked in windows anyway ?

      — commented April 1st 2011 by xavier villamuera
    • Watch out with this approach. It will work on iOS, but not on Android. Variables from app.js are not available to any files opened with include() or require(). However, if you create modules that return a function, you can then execute that function with your global variables as parameters.

      — commented February 15th 2013 by remko posthuma
  • Thanks for the great answers here…

    I have a similar project, except that I'm trying to pass an array of objects, so:

    I would like something like:

    Ti.App.friends[8342873] = {
       name: "Mike",
       age: 26,
       height: 6.1
    }
    

    But when I call:

    Ti.App.friends[dynamic.id].name
    

    I get an error friends [undefined]. Any ideas?

    I tried another syntax:

    Ti.App.friends = [{
       name: "Mike",
       age: 26,
       height: 6.1
    }];
    

    But then I have to call:

    Ti.App.friends[0].name
    

    Which works, except I need to call each friend by id, not by the index of the array.

    I would really appreciate some help on this, thanks!

    — answered November 18th 2010 by Michael Newell
    permalink
    2 Comments
    • Please open your own question, Michael, and then I am certain someone will offer you some useful advice. Please make sure that you give it a meanful title, so someone facing the same problem in future can find your solution easily.

      — commented November 18th 2010 by Paul Dowsett
    • i believe that you should use e.source.index or something like that.. but i think you should've already got that..

      — commented March 25th 2014 by Liang Yan
  • I would include underscore.js
    Then you can do this:

    var myFriend = _.detect(Ti.App.friends, function(friend){ return friend.id == dynamicId });
    alert(myFriend.name);
    
    — answered November 30th 2010 by Roger Chapman
    permalink
    0 Comments
  • Thanks for the great answers here…

    I have a similar project, except that I'm trying to pass an array of objects, so:

    I would like something like:

    Ti.App.friends[8342873] = {
       name: "Mike",
       age: 26,
       height: 6.1
    }
    

    But when I call:

    Ti.App.friends[dynamic.id].name
    

    I get an error friends [undefined]. Any ideas?

    I tried another syntax:

    Ti.App.friends = [{
       name: "Mike",
       age: 26,
       height: 6.1
    }];
    

    But then I have to call:

    Ti.App.friends[0].name
    

    Which works, except I need to call each friend by id, not by the index of the array.

    I would really appreciate some help on this, thanks!

    — answered November 18th 2010 by Michael Newell
    permalink
    0 Comments
  • This has been asked before.

    In short there is no official support: The official recommended way is to put something in app JS and then get it by events OR alternatively pass copies of it window to window.

    However, some people have put their own variables/objects off of Titanium.App which seems to work, but isn't officially supported (so it may stop in some future SDK).

    — answered November 10th 2010 by Mike Robinson
    permalink
    2 Comments
    • Using "Ti.App" to store global variables will not stop in the future as you can add any object to another JavaScript object, as it's completely dynamic. This is to do with JavaScript not the SDK. In theory you could use any Titanium object that is available on all the windows. ie. the following should also work:

      Ti.UI.myObject = 'something';
      Ti.Network.myObject2 = 'something else';
      Ti.API.myObject3 = 'something more';
      etc etc etc
      

      Using "Ti.App" just makes more sense, and is good to keep it consistent.

      — commented November 10th 2010 by Roger Chapman
    • My response was simply meant to say, you won't find any of the Appcelerator staff recommending it. They have mentioned events numerous times as a solution to global variables, but never have they suggested adding on to their objects.

      While I agree it's not a Javascript limitation….say in 1.8.5 Titanium no longer has Ti.App, and instead they decide to call it Ti.AppSettings your app will entirely break. I agree its unlikely but I was highlighting the fact the Appcelerator fact doesn't recommend it.

      — commented November 18th 2010 by Mike Robinson
  • — answered November 10th 2010 by Critter
    permalink
    2 Comments
    • You can…see below. Properties are for storing persistant variables, like user setting.

      — commented November 10th 2010 by Roger Chapman
    • Why this answer appeared blank or ellipsis? This should be deleted. Stating an answer is no joke, man.

      — commented May 16th 2014 by David Dimalanta
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.