[ALOY-969] Variable position in compiled alloy file
GitHub Issue
n/a
Type
Bug
Priority
Low
Status
Closed
Resolution
Invalid
Resolution Date
2014-03-13T13:22:06.000+0000
Affected Version/s
n/a
Fix Version/s
n/a
Components
Runtime
Labels
compiler, js
Reporter
Guile
Assignee
Tim Poulsen
Created
2014-03-12T10:30:37.000+0000
Updated
2018-03-07T22:28:25.000+0000
Description
I declare a button with a onClick event that uses a function.
This function is declare as a variable
var myFunc = function () {}
When Alloy compile it, the variable declaration is after the Button creation with the callback definition.
Then the button has an "undefined" click callback and does nothing
Is there anyway to say "this code must be before alloy code, this code can be after"
See attchmts: index.xml for the view example, index.js for the app/Controller file, and Resources index.js is the compiled file which show "myClick" is used before it is declared
function myFunc() {
// this whole function, including its contents will be hoisted to the
// top of the enclosing scope, as if you declared it before other variables
// this is called a function declaration
}
var myFunc = function() {}; // is treated as if you wrote it this way:
var myFunc;
// the rest of your variable declarations and other code comes here
// then the assignment of your function to the myFunc variable
myFunc = function() {};
var myFunc = function() {}; // is called a function statement
Tim Poulsen 2014-03-13
JavaScript hoisting issue, not Alloy problem
Guile 2014-03-14
Thank you Tim! :-)
I am aware of this and found the solution by using a function declaration (I didn't know all of the terms you said)
I still don't know how I can use a _.throttle(function () {}) event then (by example).
You're not taking into account JavaScript's [variable hoisting](http://www.kenneth-truyers.net/2013/04/20/javascript-hoisting-explained/). Rather than using a function statement, use a function declaration.
JavaScript hoisting issue, not Alloy problem
Thank you Tim! :-) I am aware of this and found the solution by using a function declaration (I didn't know all of the terms you said) I still don't know how I can use a _.throttle(function () {}) event then (by example).
We're getting a bit off topic here and Jira isn't really for help conversations. That said, check the Underscore docs http://underscorejs.org/#throttle or perhaps http://drupalmotion.com/article/debounce-and-throttle-visual-explanation If you're still stuck, ask in the Q&A forums please.
Closing as invalid. If this is incorrect, please reopen.