[TIMOB-1662] Included .js files cannot see previously declared vars
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Trivial |
Status | Closed |
Resolution | Won't Fix |
Resolution Date | 2016-08-19T16:50:43.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Backlog |
Components | Android |
Labels | android, include, ios, variables |
Reporter | Adam Renklint |
Assignee | Eric Merriman |
Created | 2011-04-15T02:58:44.000+0000 |
Updated | 2017-03-16T21:40:10.000+0000 |
Description
I have a project in which I use Ti.include('filename.js') to keep the app design modular. So here's the scenario.
app.js declares variable mySetting
app.js includes file doSomethingWithSetting.js
The code in doSomethingWithSetting.js cannot find the variable mySetting.
A Q&A has also been created previously by someone else experiencing the same problem: http://developer.appcelerator.com/question/49751/working-code-cant-see-vars-after-moving-into-an-include-file"> http://developer.appcelerator.com/question/49751/working-code-cant-...
Could've sworn this was a dupe but I don't see one.
... By which I mean this is NOT a dupe.
When vars are declared and files included outside of a function (or eventListener), it works as expected on Android AND iOS (see testVar1 and testVar2 in the code below). However, on Android, when vars are declared and files included *inside* a function, an exception is generated as the variables are not known. According to Don, it seems that the incorrect scope is being used. Tested on: Android 2.2, Titanium 1.7.0 (06/02/11 11:34 c951553...) - passes 2 out of 4 tests iOS 4.1, 4.2, 4.3, Titanium 1.7 latest build (June 2011) - passes 3 out of 4 tests See inline comments for pass/fails:
Testing against 1.8.0 beta, it appears that the iOS behavior is more in line: testVar 4 is undefined for both platforms.
Note, however, that things work differently if this doesn't occur inside an event handler. For example, if we include test2 inside an ordinary function:
var win1 = Titanium.UI.createWindow({ title:'Window 1', backgroundColor:'blue', fullscreen:false }); win1.open(); var testVar1 = "succeeded"; Ti.include('include_test1.js'); Ti.API.info("testVar2 which was defined in include_test1.js and output in app.js: " + testVar2); var test = function(){ var testVar3 = "succeeded"; Ti.include('include_test2.js'); // exception: testVar4 is not defined on Android, but **works** on iOS Ti.API.info("testVar4 which was defined in include_test2.js and output in app.js: " + testVar4); }; test();
And change include_test2.js to:var testVar4 = "succeeded"; Ti.API.info("testVar1: " + testVar1); Ti.API.info("testVar2: " + testVar2);
All of the variable references work. If we add the reference to testVar3 back in, however, it breaks--testVar3 is out of scope in the include file. Conclusion: include file has access to the current root execution context. But: trying to include a file from an event handler has unpredictable results.Ti.include()
has been deprecated since Titanium SDK 3.3.0 in favor of CommonJS modules andrequire()
.Closing ticket as the issue will not fix.