Global variables in titanium
I want some global variables in Titanium.
Is there any way to set these variables.
Please Help
Thanks..
8 Answers
-
Ti.App.myGlobalVar = 'something';
-
Not quite.. but you can set/retrieve properties..
Have a gander at…
http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.App.Properties-module -
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.
-
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!
-
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);
-
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!
-
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).
-
…