Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created May 3, 2011 17:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonalter/953810 to your computer and use it in GitHub Desktop.
Save jonalter/953810 to your computer and use it in GitHub Desktop.
Creating Reusable Factories and Using Ti.App with a Namespace
// Please note that this is a dirty solution.
// It does work, but Appcelerator does not support it (adding things to the Ti namespace).
// There is a very good chance that it will not be possible in future releases.
var ui = {};
var labels = {};
Ti.include('ui.js');
Ti.include('labels.js');
// Build the namespace
var NS = {
ui:ui,
labels:labels
};
// Make namespace globally available
Ti.App.NS = NS;
var win = Ti.App.NS.ui.createCustomWindow();
var button = Ti.App.NS.ui.createCustomButton();
win.add(button);
var myLabel = Ti.App.NS.labels.createCustomLabel();
win.add(myLabel);
win.open();
(function(){
labels = {
createCustomLabel: function(){
var label = Ti.UI.createLabel({
top: 10,
height: 60,
width: 200,
text: 'I am a Label'
});
return label;
}
};
})();
(function() {
ui = {
createCustomWindow: function() {
var win = Ti.UI.createWindow({
backgroundColor: 'green'
});
return win;
}
};
ui.createCustomButton = function() {
var button = Ti.UI.createButton({
title: 'Button',
height: 60,
width: 150
});
return button;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment