Titanium JIRA Archive
Alloy (ALOY)

[ALOY-441] Markup event handlers defined as function expressions can't manually be triggered til after controller code is executed

GitHub Issuen/a
TypeBug
PriorityMedium
StatusOpen
ResolutionUnresolved
Affected Version/sAlloy 0.3.4
Fix Version/sn/a
ComponentsRuntime
Labelsn/a
ReporterTony Lukasavage
AssigneeUnknown
Created2012-12-20T22:38:54.000+0000
Updated2018-03-07T22:25:53.000+0000

Description

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");

Comments

No comments

JSON Source