Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-9356] MobileWeb: Storing a label to a variable named "status" causes the label to be caste to a string.

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionInvalid
Resolution Date2012-06-04T12:33:35.000+0000
Affected Version/sRelease 2.1.0
Fix Version/sn/a
ComponentsMobileWeb
Labelsn/a
ReporterDawson Toth
AssigneeChris Barber
Created2012-06-04T09:00:46.000+0000
Updated2014-01-28T23:57:36.000+0000

Description

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.

Comments

  1. Bryan Hughes 2012-06-04

    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.
  2. Chris Barber 2012-06-04

    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?
  3. Bryan Hughes 2012-06-04

    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.

JSON Source