{ "id": "62294", "key": "TIMOB-1662", "fields": { "issuetype": { "id": "1", "description": "A problem which impairs or prevents the functions of the product.", "name": "Bug", "subtask": false }, "project": { "id": "10153", "key": "TIMOB", "name": "Titanium SDK/CLI", "projectCategory": { "id": "10100", "description": "Titanium and related SDKs used in application development", "name": "Client" } }, "fixVersions": [ { "id": "11258", "description": "Holding Pen for Triaged Issues", "name": "Backlog", "archived": false, "released": false } ], "resolution": { "id": "2", "description": "The problem described is an issue which will never be fixed.", "name": "Won't Fix" }, "resolutiondate": "2016-08-19T16:50:43.000+0000", "created": "2011-04-15T02:58:44.000+0000", "priority": { "name": "Trivial", "id": "5" }, "labels": [ "android", "include", "ios", "variables" ], "versions": [], "issuelinks": [], "assignee": { "name": "emerriman", "key": "emerriman", "displayName": "Eric Merriman ", "active": true, "timeZone": "America/Los_Angeles" }, "updated": "2017-03-16T21:40:10.000+0000", "status": { "description": "The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.", "name": "Closed", "id": "6", "statusCategory": { "id": 3, "key": "done", "colorName": "green", "name": "Done" } }, "components": [ { "id": "10202", "name": "Android", "description": "Android Platform" } ], "description": "{html}

I have a project in which I use Ti.include('filename.js') to\nkeep the app design modular. So here's the scenario.

\n

app.js declares variable mySetting
\napp.js includes file doSomethingWithSetting.js

\n

The code in doSomethingWithSetting.js cannot find the variable\nmySetting.

\n

A Q&A has also been created previously by someone else\nexperiencing the same problem: \nhttp://developer.appcelerator.com/question/49751/working-code-cant-...

{html}", "attachment": [], "flagged": false, "summary": "Included .js files cannot see previously declared vars", "creator": { "name": "adamrenklint", "key": "adamrenklint", "displayName": "Adam Renklint", "active": true, "timeZone": "America/Los_Angeles" }, "subtasks": [], "reporter": { "name": "adamrenklint", "key": "adamrenklint", "displayName": "Adam Renklint", "active": true, "timeZone": "America/Los_Angeles" }, "environment": null, "comment": { "comments": [ { "id": "126306", "author": { "name": "stephentramer", "key": "stephentramer", "displayName": "Stephen Tramer", "active": true, "timeZone": "America/Los_Angeles" }, "body": "{html}

Could've sworn this was a dupe but I don't see one.

{html}", "updateAuthor": { "name": "stephentramer", "key": "stephentramer", "displayName": "Stephen Tramer", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2011-04-15T02:58:45.000+0000", "updated": "2011-04-15T02:58:45.000+0000" }, { "id": "126307", "author": { "name": "stephentramer", "key": "stephentramer", "displayName": "Stephen Tramer", "active": true, "timeZone": "America/Los_Angeles" }, "body": "{html}

... By which I mean this is NOT a dupe.

{html}", "updateAuthor": { "name": "stephentramer", "key": "stephentramer", "displayName": "Stephen Tramer", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2011-04-15T02:58:45.000+0000", "updated": "2011-04-15T02:58:45.000+0000" }, { "id": "133701", "author": { "name": "pdowsett", "key": "pdowsett", "displayName": "Paul Dowsett", "active": true, "timeZone": "Europe/London" }, "body": "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.\r\n\r\nTested on:\r\nAndroid 2.2, Titanium 1.7.0 (06/02/11 11:34 c951553...) - passes 2 out of 4 tests\r\niOS 4.1, 4.2, 4.3, Titanium 1.7 latest build (June 2011) - passes 3 out of 4 tests\r\n\r\nSee inline comments for pass/fails:\r\n\r\n{code:lang=javascript|title=app.js}\r\nvar win1 = Titanium.UI.createWindow({\r\n title:'Window 1',\r\n backgroundColor:'blue',\r\n fullscreen:false\r\n});\r\n\r\nwin1.open();\r\n\r\nvar testVar1 = \"succeeded\";\r\nTi.include('include_test1.js');\r\n\r\nTi.API.info(\"testVar2 which was defined in include_test1.js and output in app.js: \" + testVar2);\r\n\r\nwin1.addEventListener('click', function(){\r\n var testVar3 = \"succeeded\";\r\n Ti.include('include_test2.js');\r\n\r\n // exception: testVar4 is not defined on Android, but **works** on iOS\r\n Ti.API.info(\"testVar4 which was defined in include_test2.js and output in app.js: \" + testVar4);\r\n});\r\n{code}\r\n\r\n{code:lang=javascript|title=include_test1.js}\r\nTi.API.info(\"testVar1 which was defined in app.js and output in include_test1.js: \" + testVar1);\r\nvar testVar2 = \"succeeded\";\r\n{code}\r\n\r\n{code:lang=javascript|title=include_test2.js}\r\n// exception: testVar3 is not defined on both Android and iOS\r\nTi.API.info(\"testVar3 which was defined in app.js and output in include_test2.js: \" + testVar3); \r\nvar testVar4 = \"succeeded\";\r\n{code}\r\n", "updateAuthor": { "name": "pdowsett", "key": "pdowsett", "displayName": "Paul Dowsett", "active": true, "timeZone": "Europe/London" }, "created": "2011-05-16T10:39:26.000+0000", "updated": "2011-06-07T15:45:53.000+0000" }, { "id": "133726", "author": { "name": "pdowsett", "key": "pdowsett", "displayName": "Paul Dowsett", "active": true, "timeZone": "Europe/London" }, "updateAuthor": { "name": "pdowsett", "key": "pdowsett", "displayName": "Paul Dowsett", "active": true, "timeZone": "Europe/London" }, "created": "2011-05-16T12:54:17.000+0000", "updated": "2011-05-16T12:54:17.000+0000" }, { "id": "173151", "author": { "name": "aevans", "key": "aevans", "displayName": "Arthur Evans", "active": true, "timeZone": "America/Los_Angeles" }, "body": "Testing against 1.8.0 beta, it appears that the iOS behavior is more in line: testVar 4 is undefined for both platforms.\r\n\r\n", "updateAuthor": { "name": "aevans", "key": "aevans", "displayName": "Arthur Evans", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2011-11-17T13:01:56.000+0000", "updated": "2011-11-17T13:01:56.000+0000" }, { "id": "173228", "author": { "name": "aevans", "key": "aevans", "displayName": "Arthur Evans", "active": true, "timeZone": "America/Los_Angeles" }, "body": "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:\r\n\r\n`\r\nvar win1 = Titanium.UI.createWindow({\r\n title:'Window 1',\r\n backgroundColor:'blue',\r\n fullscreen:false\r\n});\r\n\r\nwin1.open();\r\n\r\nvar testVar1 = \"succeeded\";\r\nTi.include('include_test1.js');\r\n\r\nTi.API.info(\"testVar2 which was defined in include_test1.js and output in app.js: \" + testVar2);\r\n\r\nvar test = function(){\r\n var testVar3 = \"succeeded\";\r\n Ti.include('include_test2.js');\r\n\r\n // exception: testVar4 is not defined on Android, but **works** on iOS\r\n Ti.API.info(\"testVar4 which was defined in include_test2.js and output in app.js: \" + testVar4);\r\n};\r\n\r\ntest();\r\n`\r\n\r\nAnd change include_test2.js to:\r\n\r\n`\r\nvar testVar4 = \"succeeded\";\r\nTi.API.info(\"testVar1: \" + testVar1);\r\nTi.API.info(\"testVar2: \" + testVar2);\r\n`\r\n\r\nAll 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.\r\n\r\nConclusion: include file has access to the current root execution context. But: trying to include a file from an event handler has unpredictable results.\r\n\r\n\r\n", "updateAuthor": { "name": "aevans", "key": "aevans", "displayName": "Arthur Evans", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2011-11-17T22:36:15.000+0000", "updated": "2011-11-17T22:36:15.000+0000" }, { "id": "393623", "author": { "name": "cbarber", "key": "cbarber", "displayName": "Chris Barber", "active": true, "timeZone": "America/Chicago" }, "body": "{{Ti.include()}} has been deprecated since Titanium SDK 3.3.0 in favor of CommonJS modules and {{require()}}.", "updateAuthor": { "name": "cbarber", "key": "cbarber", "displayName": "Chris Barber", "active": true, "timeZone": "America/Chicago" }, "created": "2016-08-19T16:50:43.000+0000", "updated": "2016-08-19T16:50:43.000+0000" }, { "id": "413154", "author": { "name": "lmorris", "key": "lmorris", "displayName": "Lee Morris", "active": false, "timeZone": "America/Los_Angeles" }, "body": "Closing ticket as the issue will not fix.", "updateAuthor": { "name": "lmorris", "key": "lmorris", "displayName": "Lee Morris", "active": false, "timeZone": "America/Los_Angeles" }, "created": "2017-03-16T21:40:10.000+0000", "updated": "2017-03-16T21:40:10.000+0000" } ], "maxResults": 8, "total": 8, "startAt": 0 } } }