Skip to content

Instantly share code, notes, and snippets.

@zo0m
Last active September 18, 2015 21:12
Show Gist options
  • Save zo0m/0166cb2772e2d5ea8280 to your computer and use it in GitHub Desktop.
Save zo0m/0166cb2772e2d5ea8280 to your computer and use it in GitHub Desktop.
Titanium UI Factory example
/**
* Inside controller :
*
* var customGUIManager = new CustomGUIManager($);
*
* customGUIManager.createLabel('sampleUIClassName', 'SampleText', { left : '15dp'}),
*
*/
function CustomGUIManager(controller) {
this.styleCache = {};
this.createStyleByClass = function(className, apiName) {
if (apiName) {
return controller.createStyle ({ classes : className, apiName: apiName});
}
return controller.createStyle ({ classes : className });
};
this.getCachedStyleIfExists = function(className, apiName) {
if (!this.styleCache[className])
{
this.styleCache[className] = this.createStyleByClass(className, apiName);
}
return this.styleCache[className];
};
this.createUI = function (styleName, apiName, addionalParams, createElementWithStyleFunction) {
var style = this.getCachedStyleIfExists(styleName);
var element = createElementWithStyleFunction(style, apiName);
if (addionalParams) {
for (var key in addionalParams) {
element[key] = addionalParams[key];
}
}
return element;
};
this.createView = function(styleName, content, addionalParams) {
var view = this.createUI(styleName, 'View', addionalParams, Ti.UI.createView);
if (content) {
var contentArray = content;
if (content.constructor !== Array)
{
contentArray = [content];
}
contentArray.forEach(function(item) {
view.add(item);
});
}
return view;
};
this.createTextField = function(styleName, value, hintText, addionalParams) {
if (!addionalParams) {
addionalParams = {};
}
if (value) addionalParams.value = value;
if (hintText) addionalParams.hintText = hintText;
return this.createUI(styleName, 'TextField', addionalParams, Ti.UI.createTextField);
};
this.createLabel = function(styleName, text, addionalParams) {
if (!addionalParams) {
addionalParams = {};
}
if (text) addionalParams.text = text;
return this.createUI(styleName, 'Label', addionalParams, Ti.UI.createLabel);
};
this.createButton = function(styleName, title, addionalParams) {
if (!addionalParams) {
addionalParams = {};
}
if (text) addionalParams.title = title;
return this.createUI(styleName, 'Button', addionalParams, Ti.UI.createButton);
};
this.createTableViewRow = function(styleName, content, addionalParams) {
if (!addionalParams) {
addionalParams = {};
}
var tableViewRow = this.createUI(styleName, 'TableViewRow', addionalParams, Ti.UI.createTableViewRow);
if (content) {
var contentArray = content;
if (content.constructor !== Array)
{
contentArray = [content];
}
contentArray.forEach(function(item) {
tableViewRow.add(item);
});
}
return tableViewRow;
};
this.Update = {
updateLabel : function(label, params)
{
if (label.baseText)
{
label.text = label.baseText.printf(params);
}
}
};
}
@ilDon
Copy link

ilDon commented Sep 18, 2015

While trying to integrate customGUIManager into my project I get these errors.

[ERROR] :  Script Error {
[ERROR] :      column = 2029;
[ERROR] :      line = 1;
[ERROR] :      message = "undefined is not an object (evaluating 'Ti.UI.Android.PROGRESS_INDICATOR_DIALOG')";
[ERROR] :      sourceURL = " ... /alloy/styles/ListTab.js";
[ERROR] :      stack = " ... /alloy/styles/ListTab.js:1:2029\nglobal code@file: ... /alloy/styles/ListTab.js:2:70\nrequire@[native code]\ncreateStyle@file: ... /alloy.js:182:25\ncreateStyle@file ...;
[ERROR] :  }
[ERROR] :  Script Error Module "alloy/styles/ClientsListTab" failed to leave a valid exports object
[ERROR] :  ErrorController is up. ABORTING showing of modal controller

I'm passing a controller $ in which I have some Android specific controls, inside a condition like this if (Ti.Platform.Android) { ... }
When running the app without customGUIManager it works.
I'm calling customGUIManager like this:

var customGUIManager = new CustomGUIManager($);
var emptyView = customGUIManager.createLabel('emptyList', L('EMPTY'));

Do you have any ideas? Thank's!

EDIT:

The same problem occurs when trying this in the controller itself:

var emptyView = Ti.UI.createLabel({ text : L('EMPTY') });
var style = $.createStyle({
    classes: 'emptyList',
});
$.emptyView.applyProperties(style);
$.winList.add(emptyView);

I guess then it is a problem with Appcelerator and the way createStyle works..

PS Appcelerator Studio SDK 4.0.0.GA on Mac Yosemite building to iOS 4S Simulator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment