Titanium Community Questions & Answer Archive

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

Passing Arrays throughout windows

Hi,

I am trying to pass an array to a new window which I "outsourced" in its own .js file. This does not seem to work for me. I can pass single variables without a problem, but no arrays. This is how I am trying to do this:

app.js:

var myarray = ["one", "two", "three"];

var win = Titanium.UI.createWindow({  
title:'Test',
url:'includes/win.js',
myarray:myarray
});
win.open();

win.js:

var myarray = win.myarray;
Titanium.API.info(myarray.length); //oddly this outputs '3'
Titanium.API.info(myarray[1]); //but this does not output anything

Any clue what I am doing wrong?

Thanks! Felix

— asked May 3rd 2010 by Felix Eggbert
  • arrays
  • passing
0 Comments

7 Answers

  • Accepted Answer

    Hey Felix,

    Hmmm…what you're doing worked fine for me when I tried it. Possible that anything else in your code is getting in the way?

    Here's the full contents of my app.js and win.js files (I have them in the same folder so the path is different than in your case, but that shouldn't matter):

    app.js

    var myarray = ["one", "two", "three"];
    
    var win = Titanium.UI.createWindow({  
        title:'Test',
        url:'win.js',
        myarray:myarray
    });
    win.open();
    

    win.js

    var win = Titanium.UI.currentWindow;
    var myarray = win.myarray;
    Titanium.API.info(myarray.length); //oddly this outputs '3'
    Titanium.API.info(myarray[1]);
    

    @Stan you're right, the variables are local. You're able to pass variables from parent to child as custom properties, so the first step is to add it to the new window object, then from within that object you can read that variable out by accessing the currentWindow object.

    — answered May 3rd 2010 by Dan Giulvezan
    permalink
    0 Comments
  • var myarray = Titanium.UI.currentWindow.myarray;
    

    this doesn't work ?

    — answered May 3rd 2010 by Dan Tamas
    permalink
    0 Comments
  • I'm still somewhat new to javascript but if you add var like that aren't you making it a local variable. Meaning it can only be used inside the original function? Try removing the var.

    — answered May 3rd 2010 by Stan Thompson
    permalink
    0 Comments
  • sorry, I forgot to take this into the snippet…i tried it like this (to make my previous example complete):

    win.js:

    var win = Titanium.UI.currentWindow;
    var myarray = win.myarray;
    Titanium.API.info(myarray.length); //oddly this outputs '3'
    

    Addition:

    Titanium.API.info(myarray[1]);
    

    this outputs "undefined"…

    — answered May 3rd 2010 by Felix Eggbert
    permalink
    0 Comments
  • sorry, I forgot to take this into the snippet…i tried it like this (to make my previous example complete):

    win.js:

    var win = Titanium.UI.currentWindow;
    var myarray = win.myarray;
    Titanium.API.info(myarray.length); //oddly this outputs '3'
    

    Addition:

    Titanium.API.info(myarray[1]);
    

    this outputs "undefined"…

    — answered May 3rd 2010 by Felix Eggbert
    permalink
    0 Comments
  • Hey Felix,

    Hmmm…what you're doing worked fine for me when I tried it. Possible that anything else in your code is getting in the way?

    Here's the full contents of my app.js and win.js files (I have them in the same folder so the path is different than in your case, but that shouldn't matter):

    app.js

    var myarray = ["one", "two", "three"];
    
    var win = Titanium.UI.createWindow({  
        title:'Test',
        url:'win.js',
        myarray:myarray
    });
    win.open();
    

    win.js

    var win = Titanium.UI.currentWindow;
    var myarray = win.myarray;
    Titanium.API.info(myarray.length); //oddly this outputs '3'
    Titanium.API.info(myarray[1]);
    

    @Stan you're right, the variables are local. You're able to pass variables from parent to child as custom properties, so the first step is to add it to the new window object, then from within that object you can read that variable out by accessing the currentWindow object.

    — answered May 3rd 2010 by Dan Giulvezan
    permalink
    0 Comments
  • Sorry for the double post, Q&A had a hiccup and I resubmitted.

    — answered May 3rd 2010 by Dan Giulvezan
    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.