Volatil3
Volatil3

Reputation: 14978

Titanium appcelerator: Passing reference in other Windows

I am using multiple window apps being controlle via Tabgroup. I am using .js files for each window which are passed as URL with each window. I also have a function.js file where I am writing routines for reusable code;for instance Form generation or table View etc. Now I got stuck in a situation where I have to call a function which generates form element which are in WindowA. In WindowA I explicitly defined all required form elements. Now what do I pass in my function routine that it gets the reference of already created form element.

Please note that TableView created in separate Window, say WinB and I am generating rows by calling a routine written in functions.js. Ia m getting undefined error despite of variables are global in WinA.

How can I get the reference of those variables in entire application?

Upvotes: 0

Views: 2417

Answers (2)

Jeff Bonnes
Jeff Bonnes

Reputation: 1130

I highly recommend NOT using Ti.App to pass variables - even if it works. It is not best practice. I know you won't want to, but Take the time refactoring your code to not use windows with urls - go to a single instance / commonJS approach.

See https://wiki.appcelerator.org/display/guides/Mobile+Best+Practices and https://wiki.appcelerator.org/display/guides/CommonJS+Modules+in+Titanium

Also see https://github.com/appcelerator-titans/App for current best practice code.

It will take a while to refactor, but you app will be much more stable, and your code will be much cleaner.

Upvotes: 3

kirilloid
kirilloid

Reputation: 14304

First of all you can declare global variable in e.g. Ti.App object. AFAIR, this or some similar thing didn't work properly on iPhone.

If this will not work, there's a trick with closure:

myVar = {field: 'some value'};
Ti.App.getMyGlobalVar = function() {
    return myVar;
}

Also, I'm not sure whether Ti.App or some other namespace will work. It has been several months, since I used Titanium appcelerator.

Upvotes: 0

Related Questions