5.3. Handling Events

Event handlers are bound to events using the OnChange method.

method Application.OnChange (sym, fn)
{
    local    chfn = cons (sym, fn);
    ._ChangeFunctions = cons (chfn, ._ChangeFunctions);
    on_change (sym, fn);
    chfn;
}

The OnChange method is a wrapper for the on_change function that does the actual binding of the event handler. First the OnChange method creates a two-member list (chfn) consisting of a symbol (sym) and the function that should run (fn) when the symbol changes value. That short list is added to the class's _ChangeFunctions list. All lists are constructed using the Gamma cons function. Finally, the on_change function links the symbol to the event-handling function. What gets returned, chfn, is a two member list—exactly what the unwrapped on_change function would have returned.

One way to remove an event handler is with the RemoveChange method.

method Application.RemoveChange (chfn)
{
    ._ChangeFunctions = remove (chfn, ._ChangeFunctions);
    remove_change (car(chfn), cdr(chfn));
}

This is a wrapper on remove_change, to be used when you need to remove just a single function from _ChangeFunctions rather than all of them. See also RemoveAllChanges and RemoveAllEventHandlers.