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.
6 Answers
-
New window that open a JS file via the URL property == new context
file included using Ti.include(<url to file>) != new contextadding anything to the Ti.<anything> namespace is a bad idea
it is better to do the following:
myApp = {}; Ti.include('ui.js');
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:
https://github.com/appcelerator/tweetanium/tree/master/mobile/Tweetanium -
You can put variables in Ti.App so they can be accessed from anywhere
Ti.App.myData = { //... };
-
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'; -
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.
-
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'};
-
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.
cheers,
Chris