Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Android fails to call global function from subcontext

I'm trying to give subcontext windows access to values in the global context, according to the documentation in the Titanium.UI.Window API reference document. It works seemingly OK on iPhone, but not on Android. Any help would be appreciated!

app.js:

function x() { return 123; }
var win = Ti.UI.createWindow({ url:'win.js' });
win.x = x;
win.open();

win.js:

var label = Ti.UI.createLabel({ text:Ti.UI.currentWindow.x() });
Ti.UI.currentWindow.add(label);

Running this on iPhone produces the expect result: a page displaying the number "123." Running this on Android produces the following error:

TypeError: Cannot call property x in object ti.modules.titanium.ui.WindowProxy@43ddc0e8. It is not a function, it is "object". (app://win.js#10197)

— asked July 22nd 2010 by Josh Whiting
  • android
  • iphone
  • mobile
  • scope
1 Comment
  • Further research leads me to believe this kind of object/function sharing between contexsts is an iphone-only feature. The "Passing Data (windows)" example in the Kitchen Sink app demonstrates this but it's wrapped in an iPhone platform conditional.

    — commented July 22nd 2010 by Josh Whiting

3 Answers

  • The ability to assign objects in a window from the global context appears to be an iPhone-only feature.

    Passing data between windows on Android needs to be done differently, but I'm not sure how. For instance, if I have a global user id that I need to pass to a window that handles taking in user input and posting it up to a server, I'm not sure how to get that user id into the window context in Android. That's a separable question, though, which I've opened here

    — answered July 22nd 2010 by Josh Whiting
    permalink
    0 Comments
  • Any news about this problem?

    — answered January 12th 2011 by Stefan B
    permalink
    1 Comment
    • see code posted… it seems to work for me

      — commented January 12th 2011 by Aaron Saunders
  • This works for me, check the log console for function output

    // app.js
    
    function x() { return 123; }
    var win = Ti.UI.createWindow({ 
       x:x, 
       title:'Test Window', 
       url:'win.js', 
       width:'100%', 
       height:'100%' });
    win.open();
    

    new window context

    // win.js
    Ti.API.info("initializing tabwin");
    var currentWin = Ti.UI.currentWindow;
    
    Ti.API.info("initializing tabwin " + currentWin.x());
    
    — answered January 12th 2011 by Aaron Saunders
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.