[Twisted-web] Passing events to (Athena) event handlers.

kgi iacovou at gmail.com
Wed Jul 18 09:45:18 EDT 2007


Hi all.

In the thread entitled "How to insert an Athena event handler using Stan?" in 
October and November 2006, Jean-Paul Calderone said:

"""
The issue with the handler you showed me is most likely that the handler
thinks its first argument is an event, but it is actually a node.  There is
no way to access the event object right now, but I have been considering
just passing it as the next argument.
"""

and:

"""
[Though] this may shortly change to

  function doFoo(self, node, event)

(which is completely backwards compatible, of course, due to the magic of
javascript sucking so much).
"""

I've just hit this particular issue. Up until now all my events have been 
heavily node-based (e.g. tabbing out of a field, submitting a form, etc), 
where you can generally get away with just having access to the node the even 
happened on.

However, not having access to the event itself renders handlers for events 
like 'onkeyup' almost useless, unless you register them with the top-level 
document (like the Nevow.Athena._checkEscape() works), and as far as I can 
tell it makes implementing certain things like drag and drop much more 
difficult.

Is there a branch with experimental support for this?

If not, I've been digging around in where I think the relevant code is, 
namely:

  Nevow/nevow/athena.py line 1062:

_handlerFormat = "return 
Nevow.Athena.Widget.handleEvent(this, %(event)s, %(handler)s);"

and:

  Nevow/nevow/js/Divmod/Runtime/__init__.js line 963:

The Javascript looks like:

Nevow.Athena.Widget.handleEvent = function handleEvent(node, eventName, 
handlerName) {
    var widget = Nevow.Athena.Widget.get(node);
    var method = widget[handlerName];
    var result = false;
    if (method === undefined) {
        Divmod.msg("Undefined event handler: " + handlerName);
    } else {
        result = Nevow.Athena.Widget.dispatchEvent(
            widget, eventName, handlerName,
            function() {
                return method.call(widget, node);
            });
    }
    return result;
};

It looks like:

  1. Adding an event argument to the handler format in the Python
     (I don't think that _rewriteEventHandlerToAttribute() needs
     modification);
  2. Adding the corresponding 'event' argument to handleEvent() in the JS;
  3. Adding IE hacks (using window.event instead of event, etc) to
     the start of handleEvent();
  4. Adding the event to the list of arguments of method.call()
     within the anonymous function passed as the fourth argument to
     dispatchEvent()

would achieve this, and as JP pointed out, this should be backwards
compatible, as no extant JS handler functions should be declaring additional 
arguments.

Note that within dispatchEvent() itself the callable is invoked as 
callable.call(widget), but it looks like the 'widget' argument is ignored 
entirely by the anonymous function, which contains its own method call with 
its own "closure-y" arguments. Because of this I reckon dispatchEvent() needs 
no modification whatsoever.

Would a patch posted as a trac ticket be acceptable? I searched trac 
for 'event' and couldn't find anything resembling this. What would the unit 
test requirements be for a change like this be?

Thanks,

Ricky



More information about the Twisted-web mailing list