[Twisted-web] Adding Athena JS handlers in render methods?

kgi iacovou at gmail.com
Tue Jul 24 06:42:36 EDT 2007


Hi all.

I hit something earlier today that has me stumped. It might be a case of 
programming blindness, but on the other hand I might just be trying to do 
something that isn't supported.

Up until now, I've been adding Athena event handlers directly in the 
docFactory's stan. However, I needed to add something with an Athena event 
handler that needs data from an asynchronous external source, so I added data 
and render methods.

These normally Just Work, in the case of "vanilla" XHTML, but the Athena 
handlers don't seem to be processed by _rewriteEventHandlerToAttribute().

In other words, instead of a proper event handler being inserted into the 
XHTML, like so:

<a href="#" onclick="return Nevow.Athena.Widget.handleEvent(this, 
&quot;onclick&quot;, &quot;onclick&quot;);">Click Me 0</a>

a tag like this appears:

<a href="#"><athena:handler handler="onclick" 
event="onclick"></athena:handler>Click Me 1</a>

Here's some sample code to illustrate this. It's not a complete example in 
that there's no JS code to be invoked when you click on the first link, but 
it's runnable enough that you can load the page and view the page source.

The first link, "Click Me 0", has correct event handlers inserted.
The second and third links ("Click Me 1" and "Click Me 2") both have incorrect 
event handlers inserted. One of them simply returns a piece of stan, the 
other also makes a supposedly asynchronous call.

Am I trying to do something unsupported, or is this a bug?

Thanks,

Ricky

==

from twisted.internet import defer

from nevow import athena
from nevow import loaders
from nevow import flat
from nevow import tags as T
from nevow.page   import renderer

_js = athena.handler ( event = 'onclick', handler = 'onclick' )

class MyElement ( athena.LiveElement ):

    docFactory = loaders.stan (
        T.div ( render = T.directive ( 'liveElement' ) ) [
          T.a ( href = '#' ) [ _js, "Click Me 0" ],
          T.br,
          T.div ( render = T.directive ( 'someStan1' ) ),
          T.br,
          T.div ( render = T.directive ( 'someStan2' ) ),
          ]
    )


    def someStan1 ( self, context, name ):
        return T.a ( href = '#' ) [ _js, "Click Me 1" ]
    renderer ( someStan1 )


    def someStan2 ( self, context, name ):
        def _cb ( r ):
            return T.a ( href = '#' ) [ _js, r ]
        d = self._getData()
        d.addCallback ( _cb )
        return d
    renderer ( someStan2 )


    def _getData ( self ):
        return defer.succeed ( "Click Me 2")




class MyPage ( athena.LivePage ):

    docFactory = loaders.stan (
        T.html [ T.head ( render = T.directive ( "liveglue" ) ),
                 T.body [ T.div ( render = T.directive ( "myelement" ) ) ],
                 ] )


    def render_myelement ( self, request, tag ):
        e = MyElement()
        e.setFragmentParent ( self )
        return e


######################################################################

from twisted.application import service, internet
from nevow        import appserver

port = 7060

application  = service.Application ( "example" )
res          = MyPage()
site         = appserver.NevowSite ( res )
webService   = internet.TCPServer ( port, site )
webService.setServiceParent ( application )



More information about the Twisted-web mailing list