Pass array content between js files

You must Login before you can answer or comment on any questions.

This is the follow-up of my previous thread here: http://developer.appcelerator.com/question/127384/passing-array-to-new-window

I want to pass a variable (an array, in this case) between two *.js files as I need to re-use the information entered by the user. The app does not use a database and it does not need to. I understood from the other thread I should use a JSON stringify approach which I tried without succes.

Here is an example with three files:

init.js

var transferVars = [];
01.js
Ti.include('init.js');
transferVars.push({'test': true});
02.js
Ti.include('init.js');
var transferredVars = JSON.stringify(transferVars);
var testResult = transferredVars['test']
alert(testResult);
This gives "undefined" as result... What am I doing wrong?

— asked 5 months ago by Pieter Kubben
2 Comments
  • each time you inlcude init.js you get a new file that is why the value is undefined

    — commented 5 months ago by Aaron Saunders

  • Ouch.... of course, you are completely right!

    — commented 5 months ago by Pieter Kubben

2 Answers

Accepted Answer

did yout try just firing an event from one file that the other file is listening for? The payload of the event is the array you are passing

— answered 5 months ago by Aaron Saunders
answer permalink
4 Comments
  • try structuring your code like this... which i dont think is as good as using commonjs structure, but will work

    myApp = {};
    myApp.global = [];
    myApp.global_string =  "HELLO EVERY BODY";
    include('file1.js');
    include('file2.js');
    your myApp name space will be available in all files

    — commented 5 months ago by Aaron Saunders

  • Sounds good, added this to app.js (as I understand init.js is not the place to do so). Now something goes wrong in the Ti.include() files...

    No matter where I have the following line of code

    var win = Ti.UI.currentWindow;

    I get this error: Result of expression 'win' [undefined] is not an object.

    Read somewhere else to delete the iphone-dir in /build/iphone, but this does not solve the issue.

    Any ideas?

    — commented 5 months ago by Pieter Kubben

  • different question but, Ti.UI.currentWindow is only available when you open a window with a url

    — commented 5 months ago by Aaron Saunders

  • Show 1 more comment

Aaron's solution works, but creates another problem in my code which made me use a workaround for now. Should consider using a database in the future, maybe.

Thanks for the help!

Your Answer

Think you can help? Login to answer this question!