global var undefined in Android

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

Recently I tried to build my project on droid and I'm receiving errors that my global var is undefined.

example: app.js

var APP = {};
 
Ti.include('lib/ui.js');
inside my ui.js file I do this
(function(){
APP.ui = {}
 
APP.ui.Somefunction = function()...
 
})();

This used to work, but not sure why it's bombing any ideas? Working excellent in iPhone.

Any ideas would be appreciated.

thanks

— asked 11 months ago by matt s
1 Comment
  • I can't answer you directly, but something seems funky on Android in regards to scope, I agree.

    On the other hand, Ti.include is marked for obsoletion, and it is recommended not to use it.

    What I do with my global vars is that I collect them in an object (like you do) and then require my modules with that global as an argument.

    ie.

    App = {}
     
    something = require('something')
     
    something.doSomething(App, var1, var2)

    — commented 11 months ago by Esben Maaløe

1 Answer

There is a difference between global scope handling on iOS and android, but I thought it was limited to CommonJS modules and the require() call.

The difference is that on iOS, you can access variables in the global scpe from within a CommonJS module, but on android you can't. The bug is on the iOS side, since you should not be able to access globals from within CommonJS modules. As I understand it, that loophole should eventually be closed (and, as Esben points out, include() will eventually not be supported).

However, I'm surprised this issue would affect you with an include() call.

If cross-platform is important to you, and if you want your code to be maintainable over the long term, you should move your to a CommonJS structure and use require(). Use a technique like Esben presents to remove your dependence on globals.

Your Answer

Think you can help? Login to answer this question!