Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created July 13, 2011 18:00
Show Gist options
  • Save aaronksaunders/1080879 to your computer and use it in GitHub Desktop.
Save aaronksaunders/1080879 to your computer and use it in GitHub Desktop.
Silly example to show global scope without using Ti.App
Titanium.UI.setBackgroundColor('#ffffff');
var myglobalscope = {}; // global scope
Ti.include( // ADD FILE HERE TO HAVE ACCESS TO GLOBAL SCOPE
'ui.js',
'something_else.js'
);
//Use our custom UI constructors to build the app's UI
myglobalscope.ui.createApplicationTabGroup();
// using global variable to open tab group
myglobalscope.ui.tabGroup.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);
}
})();
//Wrap all code in a self-calling function to protect the global namespace
(function() {
//Create sub-namespace
myglobalscope.ui = {};
//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