<br><div><span class="gmail_quote">On 10/11/06, <b class="gmail_sendername">Jean-Paul Calderone</b> &lt;<a href="mailto:exarkun@divmod.com">exarkun@divmod.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Wed, 11 Oct 2006 12:35:42 +0200, Fabio Forno &lt;<a href="mailto:fabio.forno@gmail.com">fabio.forno@gmail.com</a>&gt; wrote:<br>&gt;Widgets.ChatWidget = Nevow.Athena.Widget.subclass('Widgets.ChatWidget');<br>&gt;<br>&gt;
Widgets.ChatWidget.methods(<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;function keyPressed (self, event) {<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(event.key()[&quot;string&quot;] == &quot;KEY_ENTER&quot;) {<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text_area = window.document.getElementById
(&quot;chat_area&quot;);<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = self.callRemote('got_text', text_area.value);<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d.addCallback(self.text_sent);<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&gt;)</blockquote><div>&nbsp;[...]</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&gt;<br>&gt;Then in the xml template I try to get a reference to the just defined<br>&gt;method in this way:<br>&gt;<br>&gt;widget = Nevow.Athena.Widget.get(<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.getElementById(&quot;athena:1&quot;);
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br>&gt;<br>&gt;widget.keyPressed<br><br>Likewise, &quot;athena:1&quot; is an implementation detail, and you can't rely on<br>your widget getting id &quot;1&quot; all the time.&nbsp;&nbsp;Instead, try using the<br>
athena:handler feature:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;div nevow:render=&quot;liveFragment&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;textarea&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;athena:handler event=&quot;onkeypress&quot; handler=&quot;keyPressed&quot; /&gt;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/textarea&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;<br><br></blockquote></div>The problem is he needs to examine the event details after it's received, and athena:handler only gives the event handler access to the node where it happened, not the event object.
<br><br>I suggest this:<br><br>YourNS.YourWidget.methods(<br>&nbsp;&nbsp;&nbsp; function __init__(self, node) { // {{{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; YourNS.YourWidget.upcall(self, '__init__', node);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DeanEdwards.addEvent(node, 'keyPressed', <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function onKeyPressed(event) { return self.onKeyPressed(event) });<br>&nbsp;&nbsp;&nbsp; }, // }}}<br><br><br>What's DeanEdwards, you ask?&nbsp; <br><br><a href="http://rafb.net/paste/results/57Orqx27.html">http://rafb.net/paste/results/57Orqx27.html
</a><br><br>Dean Edwards came up with a nifty cross-browser event handling solution, but there are others that could work here.&nbsp; I adapted his solution to the Divmod namespace style, and added line 63 so .target and .srcElement mean the same thing.&nbsp; The above URL will rot pretty quickly, so just google for &lt;dean edwards event handling&gt;.
<br><br><br>