Skip to content

Instantly share code, notes, and snippets.

@iskugor
Created June 4, 2012 12:13
Show Gist options
  • Save iskugor/2867975 to your computer and use it in GitHub Desktop.
Save iskugor/2867975 to your computer and use it in GitHub Desktop.
Titanium WTF: Window children property
var win = Ti.UI.createWindow({ backgroundColor: "#fff", navBarHidden: true, layout: 'horizontal' });
var view = Ti.UI.createView({
layout: 'horizontal'
});
var label1 = Ti.UI.createLabel({
top: 150,
left: 50,
text: 'Testing 1 ...',
height: '75dp',
width: '50%',
backgroundColor: "#000"
});
var textField = Ti.UI.createTextField({
top: 150,
left: 50,
width: '150dp',
height: '75dp',
});
var label2 = Ti.UI.createLabel({
top: 150,
left: 50,
text: 'Testing 2 ...',
height: '75dp',
width: '50%',
backgroundColor: "#000"
});
var picker = Ti.UI.createPicker({
top: 150,
left: 50,
height: '75dp',
backgroundColor: "#999",
selectionIndicator : true
});
var data = [];
data[0] = Ti.UI.createPickerRow({ title:'Bananas' });
data[1] = Ti.UI.createPickerRow({ title:'Strawberries' });
data[2] = Ti.UI.createPickerRow({ title:'Mangos' });
data[3] = Ti.UI.createPickerRow({ title:'Grapes' });
picker.add(data);
view.add(label1);
view.add(textField);
view.add(label2);
view.add(picker);
win.add(view);
win.addEventListener('open', function() {
Ti.API.info('View');
Ti.API.info(typeof view.children); // "object" - should be "[object Array]"
Ti.API.info(typeof view._children); // "undefined" - OK
Ti.API.info(view.children.length); // "4" - OK
Ti.API.info('Window');
Ti.API.info(typeof win.children); // "object" - should be "[object Array]"
Ti.API.info(typeof win._children); // "object" - should be "undefined"
Ti.API.info(win.children.length); // "0" - should be "1"
Ti.API.info(win._children.length); // "1" - should throw an exception since "_children" should be undefined
});
win.open();
@pec1985
Copy link

pec1985 commented Jun 28, 2012

Everything in JS are objects.

Open the web browser inspector and try this:

var foo = ['one', 'two', 'three'];
console.log(typeof foo);

The typeof return an "object"

:)

So I tested this code, and win._children.length throws an error:

[INFO] Window
[INFO] object
[INFO] undefined
[INFO] 1
[WARN] Exception in event callback. {
    line = 65;
    message = "'undefined' is not an object (evaluating 'win._children.length')";
    name = TypeError;
    sourceId = 166779072;
    sourceURL = " ...../app.js";
}

@iskugor
Copy link
Author

iskugor commented Jun 28, 2012

Yeah, you're right, mea culpa. I used "typeof" and thinking about "Object.prototype.toString.call(foo)". :)

I should have specified, this was an Android test. :D
On iOS, there is no "_children" property, that property exists on Android only and it seems it does what "children" should (and "children" does nothing).

@pec1985
Copy link

pec1985 commented Jun 28, 2012

Ah, no worries, I was going through your public gists and came across this one. :) Thats all.

The _children you are talking about, could be some private Titanium API, I mean, internal that somehow found its way to the JS.

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