Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created July 13, 2011 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronksaunders/1081203 to your computer and use it in GitHub Desktop.
Save aaronksaunders/1081203 to your computer and use it in GitHub Desktop.
Titanium Appcelerator Quickie: Global Variables, Scopes & NO Events, Part 2
Titanium.UI.setBackgroundColor('#ffffff');
var myglobalscope = {}; // global scope
Ti.include( // ADD FILE HERE TO HAVE ACCESS TO GLOBAL SCOPE
'ui.js',
'something_else.js'
);
myglobalscope.ui.createBaseWindow().open();
//Wrap all code in a self-calling function to protect the global namespace
(function() {
// Create sub-namespace
myglobalscope.somethingelse = {};
/**
* useless test to show we are accessing a global variable again
*/
myglobalscope.somethingelse.showGlobalVar = function() {
alert("global var set in app.js " + myglobalscope.ui.tabGroup.tabs[0].title);
}
var value = 0;
myglobalscope.somethingelse.showValue = function(){
alert("global value " + value);
};
myglobalscope.somethingelse.setValue = function(newValue){
value = newValue;
};
})();
//Wrap all code in a self-calling function to protect the global namespace
(function() {
//Create sub-namespace
myglobalscope.ui = {};
//
//create base window
myglobalscope.ui.createBaseWindow = function() {
var win = Ti.UI.createWindow({
title:"Window One"
});
win.add(Ti.UI.createLabel({
color: '#576996',
height: 20,
textAlign: 'center',
top: 15,
text: 'Window 1'
}));
var aButton = Titanium.UI.createButton({
title: 'Click Me',
width: 100,
height: 35,
top: 100
});
aButton.addEventListener('click', function () {
myglobalscope.somethingelse.setValue(15);
myglobalscope.somethingelse.showValue();
});
win.add(aButton);
return win;
}
//
//Create the main application tab group
myglobalscope.ui.createApplicationTabGroup = function(_args) {
var tabs = Ti.UI.createTabGroup();
var windowOne = Ti.UI.createWindow({
title:"Tab One",
});
//for now, just create some simple tabs named how we would want them
tabs.addTab(Ti.UI.createTab({
title:"Tab One",
window:windowOne
}));
var oneButton = Titanium.UI.createButton({
title:"Click Me",
width: 100,
height:35,
top:50,
left: 75
});
windowOne.add(oneButton);
// calling a function from somewhere else... global again!!
oneButton.addEventListener('click', function() {
myglobalscope.somethingelse.showGlobalVar();
});
tabs.addTab(Ti.UI.createTab({
title:"Tab Two",
window:Ti.UI.createWindow({
title:"Tab Two",
})
}));
// setting global variable
myglobalscope.ui.tabGroup = tabs;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment