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 variable

I remember reading or listening somewhere each .js file has its own context. in other words I cannot just define some var in app.js and have it available in each .js file. Properties is the only way to achieve this?

Another issue: say I want to play music in background regardless of which tab I am at. Each tab is a separate .js file. How would I go about doing something like this.

btw, Is there any guide to the framework itself covering the programming paradigm etc.

— asked March 13th 2010 by saggy na
  • programming

6 Answers

  • New window that open a JS file via the URL property == new context
    file included using Ti.include(<url to file>) != new context

    adding anything to the Ti.<anything> namespace is a bad idea

    it is better to do the following:

    myApp = {};

    then create in ui.js:

    myApp.ui = {
        var one = 'bla bla';
        function foo() {
            return bar;

    by creating your own namespace, it provides safety from changes in our SDK, better speed, less memory usage, and easier to code as everything is build in a single context, and it does not pollute the global namespace.

    A great working example of this is our tweetanium mobile app that can be found here:

    — answered May 31st 2011 by Matt Apperson
    • But include doesn't seem to work in Desktop.

      — commented June 13th 2011 by Greg Bulmash
    • Can someone please clarify if the namespace and include needs to be declared in every .js file used?

      For example, Kitchen Sink has a separate .js file for each tab, and then more .js files for each example. if you wanted to use a separate name space in those examples would you need to declare

      myApp = {};

      at the top of each file, or just in app.js? I've been trying to just use it in app.js but then getting errors in when other .js files are used.

      — commented July 15th 2011 by David Baxter
    • I discovered the answer to my question above while going through the docs for Titanium.Redux. The answer is yes, you do have to add the Ti.include to each .js file.

      — commented July 15th 2011 by David Baxter
    • I copied this code and tried it several times. I don't get it work and get a parse error at ui.js.

      I also tried the link "Tweetanium", but it seems to be gone or broken.

      Do you have another sample of this? I understand, that Ti.App.myData is not a good idea and I would like to create my own namespace.

      — commented August 13th 2011 by Felix Weiss
    • Thanks for Matt Apperson, Tweetanium app is a good example to answer the question. But the link seems don't work, please try this - https://github.com/appcelerator-titans/tweetanium.git.

      — commented October 18th 2011 by Michael Chau
    • This bit of code just doesn't work. I am getting the error "Invalid Property Id" on the second line of the include when I try to use it.

      — commented February 17th 2012 by John Hawkins
  • You can put variables in Ti.App so they can be accessed from anywhere

    Ti.App.myData = {
    — answered October 1st 2010 by Charles Davison
    • can you update those var's i can't seem to do it

      in app.js
      Ti.App.myData = {
      _userId:' ',
      _userEmail:' '

      in login.js
      Ti.App.myData._userId = 'fjdslkfjsldfj';

      this won't update var

      any ideas??

      — commented April 15th 2011 by travis k
    • you have to update the variable like the initial set.
      so in app.js Ti.App.myData = {_userId:'user'};
      then to set it later in another .js file call Ti.App.myData = {_userId:'user_reset'};

      — commented May 31st 2011 by Corey Snyder
    • It seems that the method is different on Iphone and Android. On Android, I can do like the following:
      Ti.App.myData = {};
      Ti.App.myData.abc = "abc";
      in Another js, I can set it by: Ti.App.myData.abc="123";

      But on Iphone, the above code can't work.

      Since I have to declear many global functions and properties which may be used and shared by many windows. Is there any convenient ways instead of setting it in each window?

      — commented July 15th 2011 by Leoncin Lee
    • i have had this same issue with updating - apparently you can't manipulate a variable in Ti.App, but you CAN set it. so you can do:

      var myData = Ti.App.myData
      myData._userId = new_user_id;
      Ti.App.myData = myData

      — commented September 7th 2011 by d b
  • As far as I understand, you can assign properties globally with Titanium.App.Properties, but can you also bind properties to window instances. For example this works:

    var oDb = Titanium.Database.open('myDB');
    var oMainWindow = Titanium.UI.createWindow({
    title:'some title'
    //pass props
    oMainWindow.db = oDb;
    oMainWindow.some_prop = 'some value';

    — answered March 13th 2010 by Alexander van der Werff
  • I'm with saggy, is there a guide to the Appcelerator Titanium framework itself? The docs give a very brief overview of the different functions within Ti, and poking around the Kitchen Sink app helps a bit, but some fleshed-out documentation would be nice.

    Of course, I suppose I get what I pay for.

    — answered October 1st 2010 by Alex Burner
  • Just so everyone sees this.

    To set a global variable in app.js use > Ti.App.myData = {_userId:'user'};

    Then if you would like to set the variable to something different later on in another .js file call again like you did in app.js.

    so in anotherFile.js to set _userId to 'user_reset' call > Ti.App.myData = {_userId:'user_reset'};

    — answered May 31st 2011 by Corey Snyder
  • Hi saggy,
    I think if you want the data to pass between tabs as well as windows, either properties or app.fireEvent are possible ways to consider.

    app.fireEvent (placed in your app.js) can be called from any context.


    — answered March 13th 2010 by Chris Reed
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.