Passing events to (Athena) event handlers.
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
On 01:45 pm, iacovou@gmail.com wrote:
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?
While I'm not sure I read this message thoroughly enough to be sure, I think it sounds okay. Definitely putting a patch on a trac ticket is the right way to get started. You may have noticed JavaScript unit tests lying around in our various codebases. A good example of some pretty thorough unit testing is in Mantissa.Test.TestRegionModel. You'd need to add coverage for your event handler stuff that used these tests and ran under trial. Some other test examples, that will be even closer to the feature you are trying to implement, are in Nevow.Test.TestWidget.WidgetTests.test_connectDOMEvent*.
On Wednesday 18 July 2007 22:08:33 glyph@divmod.com wrote:
You may have noticed JavaScript unit tests lying around in our various codebases. A good example of some pretty thorough unit testing is in Mantissa.Test.TestRegionModel. You'd need to add coverage for your event handler stuff that used these tests and ran under trial.
Some other test examples, that will be even closer to the feature you are trying to implement, are in Nevow.Test.TestWidget.WidgetTests.test_connectDOMEvent*.
Hi Glyph et al. I'm having a little trouble running (or indeed, understanding) the unit testing infrastructure. This is in relation to the patch I mentioned previously that allows the event to be passed along with the node to event handlers. I've read through http://divmod.org/trac/wiki/DivmodNevow/AthenaTesting and also http://blackjml.livejournal.com/21602.html You mentioned "trial", but the pages above refer to "nit". From what I've worked, trial is the general purpose testing infrastructure and nit is a Nevow-specific thing that actually requires a browser (rather than using a standalone JS interpreter, say). Does trial use nit, nit use trial, or are they completely separate? (And I've got no idea how "nose" fits into all this). I can see that Nevow/nevow/js/Nevow/Test/TestWidget.js contains exactly the sort of tests that I want to extend, just like you said. I intend to leave the existing tests alone (to prove that passing and event to methods that aren't expecting events still work) and a new test that receives and examines the event (to prove that the event getting passed is indeed the type, etc, I expect)[1]. I've managed to run the nevow tests by running "nit nevow" and pointing Firefox to http://localhost:8080. However, when I run open Firebug and go to the "Script" tab I would expect to see "Nevow.Test" as an option in the dropdown list of modules. However, it looks like this isn't included at all, which means that Firefox hasn't loaded it because the test "page" doesn't include it. It occurred to me that it's possible that it's "pushed" to the browser dynamically, but it's not available even after the tests have run. At that point I'd definitely expect all relevant JS to have been loaded. (The list of JS files is: Divmod, Divmod.Base|Defer|Inspect|Runtime| Runtime.Tests, Nevow, Nevow.Athena, Nevow.Athena.Test, Nevow.Athena.Tests, Nevow.Athena.Tests.Resources. Is the JS code you pointed me to not run by the "nit nevow" tests? If not, how can I run that test code? Thanks, Ricky [1] Plus any other tests that fail as a result of the changes; for example, Nevow/nevow/test/test_athena.py:test_handlerMacro() checks a serialized athena handler against a hardcoded string; if _handlerFormat is changed, this will fail and need updating.
participants (2)
-
glyph@divmod.com
-
kgi