problem
The bulk of this issue was resolved in ALOY-438, but there still remains a small circumstance where unexpected behavior might happen. If a developer uses function expressions (as opposed to function declarations) to create an event handler function for a markup event, they will not be able to manually trigger (or remove) that event handler until after the controller code has executed. This is because in the case of function expressions, the event handler code is not executed until _after_ the developer's controller code, when the function expression is actually defined.
example showing problem
index.xml
<Alloy>
<Window id="win" onClick="handleClick"/>
</Alloy>
index.js (using function expression)
// Function expression used to define handleClick. This will work just fine.
var handleClick = function() {
// This will be successfully removed as this event will be fired some time
// after this controller's code has been executed.
$.win.off("click",handleClick);
};
$.win.open();
// This will not fire "handleClick" because when function expressions are used
// the event handler is not created until _after_ the developer's controller code
$.win.trigger("click");
index.js (using function declaration)
// With function declarations, the following code will work as expected
function handleClick() {
$.win.off("click",handleClick);
};
$.win.open();
$.win.trigger("click");
No comments