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
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.
-
var myarray = Titanium.UI.currentWindow.myarray;
this doesn't work ?
-
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.
-
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"…
-
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"…
-
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.
-
Sorry for the double post, Q&A had a hiccup and I resubmitted.