Problem Description
When you create a new alloy project and define a function inside alloy.js, If you are using LiveView the application will crash
Sample Code
alloy.js
function sum(a,b){
return a+ b ;
};
index.xml
<Alloy>
<Window class="container">
<Label id="label" onClick="doClick">Appcelerator Titanium</Label>
</Window>
</Alloy>
index.tss
".container": {
backgroundColor:"white"
},
"Label": {
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
color: "#000"
}
index.js
function doClick(e) {
alert($.label.text);
}
$.index.open();
Steps to reproduce
- Create a simple alloy project.
- Update alloy.js, index.xml, index.tss and index.js file with test code
- Enable LiveView in Appcelerator Studio
- Run on iOS simulator
- Project crash and produce errors.
Actual Results
[ERROR] : SyntaxError: Parse error
[ERROR] : File: app
[ERROR] : Line: 1
[ERROR] : SourceId: 316582208
[ERROR] : Backtrace:
[ERROR] : #0 () at file:///Users/rmitro/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/F7F894E3-3A78-4E1D-9E4E-A54A4DDE7746/QnA_Alloy.app/app.js:502
[ERROR] : #1 () at file:///Users/rmitro/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/F7F894E3-3A78-4E1D-9E4E-A54A4DDE7746/QnA_Alloy.app/app.js:469
[ERROR] : #2 () at file:///Users/rmitro/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/F7F894E3-3A78-4E1D-9E4E-A54A4DDE7746/QnA_Alloy.app/app.js:403
[ERROR] : #3 () at file:///Users/rmitro/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/F7F894E3-3A78-4E1D-9E4E-A54A4DDE7746/QnA_Alloy.app/app.js:676
Extra info
This is the error shown:
[INFO] : alloyJSHatesFunctions/1.0 (3.2.3.GA.b958a70)
[LiveView] Client connected
[INFO] : [LiveView] Error Evaluating app @ Line: 1
[ERROR] : SyntaxError: Parse error
[ERROR] : File: app
[ERROR] : Line: 1
[ERROR] : SourceId: 350635136
[ERROR] : Backtrace:
[ERROR] : #0 () at file:///Users/andrewmcelroy/Library/Application%20Support/iphone%20simulator/7.1-64/Applications/4A49D81E-DEF2-43ED-A74C-437076DE3B4E/alloyJSHatesFunctions.app/app.js:502
[ERROR] : #1 () at file:///Users/andrewmcelroy/Library/Application%20Support/iphone%20simulator/7.1-64/Applications/4A49D81E-DEF2-43ED-A74C-437076DE3B4E/alloyJSHatesFunctions.app/app.js:469
[ERROR] : #2 () at file:///Users/andrewmcelroy/Library/Application%20Support/iphone%20simulator/7.1-64/Applications/4A49D81E-DEF2-43ED-A74C-437076DE3B4E/alloyJSHatesFunctions.app/app.js:403
[ERROR] : #3 () at file:///Users/andrewmcelroy/Library/Application%20Support/iphone%20simulator/7.1-64/Applications/4A49D81E-DEF2-43ED-A74C-437076DE3B4E/alloyJSHatesFunctions.app/app.js:676
[LiveView] Client disconnected
Hello! I tested this in 3.3.0 without problem. My testcase: 1. Create a new mobile project with alloy default app. 2. Run it in the iOS simulator. 3. Modify the alloy.js with this:
4. The app didn't crash. After that, I re-run the app with the modified alloy.js without any issues. Can you please update and retest with the latest Ti Studio and Mobile SDK? Thanks!
You did not do all the required steps to reproduce this error. 1. Create a new mobile project with Alloy default app 2. ***** Enable LiveView in AppceleratorStudio ***** 3. Modify the alloy.js file with this:
4. Run the Simulator 5. Watch it crash and produce this error:
any progress or information i need provide?
to make this ticket more readable. I dumped the environment info and npm info and sdk info in this file.
One of our other Titans independently discovered this bug recently. In his investigations, it was due to a hoisting issue. He found a workaround is to use the function statement form rather than function declaration. So, in alloy.js:
Yep, that was me and the statement method solved it as Tim said.
Problem with this solution If you do
you end up creating a global variable/function.
Jason, alloy.js houses global variables and functions. The problem with the solution is that doing a normal function declaration still doesn't work. Having this issue with SDK 3.5.1. I did however rewrite my alloy.js to use "var eggo = function()" instead of "function eggo()" and it resolved the issue.
Isn't that what I said? rewrite function foo() to var foo = function() and it works.
You did. I was just confirming that it worked as I added that line in after I posted my original comment.
cool. Credit to Tim for the solution ;)
PR: https://github.com/appcelerator/liveview/pull/89
Approved. PR merged.