Problem description
When using LiveView, the global variable Alloy.Globals is not defined in modules. Disabling LiveView would not throw the error.
Code to reproduce
Create an Alloy app with this code:
index.js
var win = Ti.UI.createWindow({
backgroundColor:'#ffffff'
});
Ti.API.info(Alloy.Globals.test);
var Test = require('test');
new Test();
win.open();
lib/test.js
function Test() {
Ti.API.info(Alloy.Globals.test);
};
module.exports = Test;
Add the global variable in alloy.js:
Alloy.Globals.test = "TEST";
Actual behavior
When running the app with LiveView on, the following error is thrown:
[INFO] : [LiveView] Error Evaluating app.js @ Line: 2
[ERROR] : TypeError: 'undefined' is not an object (evaluating 'Alloy.Globals')
[ERROR] : File: app.js
[ERROR] : Line: 2
[ERROR] : SourceId: 323744256
[ERROR] : Backtrace:
[ERROR] : #0 Controller() at :21
[ERROR] : #1 () at :214
[ERROR] : #2 anonymous() at :15
[ERROR] : #3 () at file://localhost/Users/dcassenti/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/4822E77B-90A1-4555-8E22-DA55E5976F16/DavideAlloyTestApp.app/app.js:628
[ERROR] : #4 () at file://localhost/Users/dcassenti/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/4822E77B-90A1-4555-8E22-DA55E5976F16/DavideAlloyTestApp.app/app.js:493
[ERROR] : #5 () at file://localhost/Users/dcassenti/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/4822E77B-90A1-4555-8E22-DA55E5976F16/DavideAlloyTestApp.app/app.js:460
[ERROR] : #6 () at file://localhost/Users/dcassenti/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/4822E77B-90A1-4555-8E22-DA55E5976F16/DavideAlloyTestApp.app/app.js:392
[ERROR] : #7 () at file://localhost/Users/dcassenti/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/4822E77B-90A1-4555-8E22-DA55E5976F16/DavideAlloyTestApp.app/app.js:663
Without LiveView, the error is not shown. Note that in index.js the error is not thrown.
Marking TIMOB-14903 a duplicate of this since this has a simpler reproducible case.
I found an easy fix for this by removing all the
var
s from the variable declarations in the alloy generated app.js this allows for my original fix to work. To test do the following: 1. disable the alloy plugin so it doesn't overwrite you app.js 2. In your app.js generated by alloy removevar
everywhere you see it. 3. RunThis should fix the error
As stated in a conversation with [~csullivan] offline, we should in theory be able to do that, but I don't think I'm comfortable doing so on a patch release of Alloy. Given that there are known (and likely unknown) disparities between how global scope is handled on our runtimes/platforms, there seems a high change for some sort of regression to result from this, if not many. I think a change like this should likely wait for a minor release of Alloy, namely 1.3.0. If this is a course of action that we all agree on, a ticket should be logged to ALOY covering all the particulars and I can look into implementing it. After that comes the real task, which will be the extensive testing that will have to be done to ensure that everything still behaves as expected.
We might actually want to fix this via liveview so it also covers non alloy apps that rely on this same global scope feature/bug. I am looking into the fix now. I will have to make sure to only remove the var from local lexical scope vars
[~mxia] or [~tlukasavage] here is the pr on master https://github.com/appcelerator/liveview/pull/58 if one of you could please test to verify this does in fact solve the issue for you. I am unable to reproduce the above test case with this PR.
Yes, I'll do the testing here.
Local testing looks good. Merged the PR.
Tested Environment: Appcelerator Studio: 3.2.0.201312121648 SDK:3.2.0.v20131212122847 alloy: 1.3.0-cr acs: 1.0.10 npm: 1.3.2 titanium: 3.2.0-cr3 titanium-code-processor: 1.1.0-cr2 Xcode:5.0.2 OS: Mac OSX 10.9 Device: iphone simulator(v7.0.3) Could see Alloy.Globals declared values in app.js while liveview enabled. Hence verified and closing.