Skip to content

Instantly share code, notes, and snippets.

@ptquang86
Created March 27, 2012 06:42
Show Gist options
  • Save ptquang86/2213401 to your computer and use it in GitHub Desktop.
Save ptquang86/2213401 to your computer and use it in GitHub Desktop.
Titanium - Layout function
/*
Layout constructor
usage:
var oLayout = {
type: 'View',
style: { width: 100, height: 200 },
event: {
type: 'click',
callback: function(){}
},
children:[{
type : 'Label',
style : {
text : 'test'
}
}]
};
var view = layout(oLayout);
oLayout = null;
*/
function layout(oParent, scope) {
var obj,
constructor = oParent.os == null ? Ti.UI : Ti.UI[oParent.os];
if (oParent.os) {
Titanium.UI.iOS.TabbedBar;
}
obj = constructor['create' + oParent.type] ( oParent.style || {} );
//add event listener
if (oParent.event) {
if (!Array.isArray(oParent.event)) {
obj.addEventListener(oParent.event.type, oParent.event.callback);
} else {
for (var i = 0, ii = oParent.event.length; i < ii; i++) {
var ev = oParent.event[i];
obj.addEventListener(ev.type, ev.callback);
};
}
}
if (oParent.children) {
if (oParent.type != 'TableView') {
for (var i = 0, ii = oParent.children.length; i < ii; i++) {
obj.add( layout(oParent.children[i], scope) );
};
} else {
var listRow = [];
for (var i = 0, ii = oParent.children.length; i < ii; i++) {
listRow.push( layout(oParent.children[i], scope) );
};
obj.setData(listRow);
listRow = null;
}
}
//meta data
// obj._id = oParent.id;
// obj._type = oParent.type;
//add generated object to global scope
if (scope && oParent.id) {
scope[oParent.id] = obj;
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment