Problem
Storing a label in a variable and then calling window.add(with that variable) causes an app exception. Directly adding the label to the window does not, nor does cascading an assignment with the window.add call.
Error
{quote}
Uncaught TypeError: Object [object Object] has no method '_setParent' :8083/:4421
declare._add :8083/:4421
declare.add :8083/:13596
(anonymous function)
{quote}
Line 8083 is:
_add: function(view) {
view._setParent(this);
this.children.push(view);
this.containerNode.appendChild(view.domNode);
view._hasBeenLaidOut = false;
this._triggerLayout(this._isAttachedToActiveWin());
},
Reproduction
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var breakTheApp = true;
if (breakTheApp) {
var status = Ti.UI.createLabel({
text: 'This breaks.'
});
win.add(status);
}
else {
var status2;
win.add(status2 = Ti.UI.createLabel({
text: 'This works.'
}));
}
win.open();
Workaround
See the reproduction's "else" case.
The problem has to do with the variable name itself. Changing the var name to, say, "label" fixes the problem. It seems to be a webkit thing only. All webkit based browsers I tested exhibited this behavior (Safari, Chrome, Android browser), but none of the non-webkit based browsers do (Firefox, Opera, IE). I'm keeping this ticket open in case it's something funky we do, but lowering the priority since it's easily worked around.
The problem is because Webkit apparently has a variable called "status" in the window object. Others have the same issue: http://stackoverflow.com/questions/1234453/variable-assignments-using-jquery-failing-in-safari. I'd be curious if you can just wrap the example above in a (function(){ /* code goes here */ }()) and see if that also would fix it. In short, this is something outside of the hands of Mobile Web. Perhaps the AST parser could detect and warn about it?
Good idea Chris. In fact we should warn for any browser objects being used inadvertently (document, window, etc) and we could just treat "status" as one of them. I created ticket TIMOB-9367 to address the general issue, which also addresses this one in specific, so I'm resolving this ticket.