[Zope3-dev] Re: Ajax in Zope 3

Benji York benji at zope.com
Tue Dec 6 18:43:44 EST 2005


Florent Guillaume wrote:
> Myself I absolutely love the approach taken by CrackAjax
> (http://www.aminus.org/blogs/index.php/phunt/2005/10/06/subway_s_new_ajax_framework) 

It's funny you mention that.  I was intrigued by that too, but I can 
only characterize his implementation as a toy. :(

I've been working on something similar but have tried to be a bit more 
"professional" about it (unit tests, trying to be a good designer, 
etc.).  Like CrackAjax it only does translation of Python syntax to 
JavaScript, but it does a good job and I really like the ability to 
define client-side methods on the view.  Here's an example from my 
prototype (the canvas tag only works in Safari, Konquerer, and Firefox 1.5):

from anguine.view import AnguineView
from anguine.decorator import clientside
from zope.app.publisher.browser import BrowserView

class TestView1(BrowserView, AnguineView):

     @clientside
     def getContext():
         canvas = document.getElementById('test_canvas')
         return canvas.getContext('2d')

     @clientside
     def draw():
         ctx = getContext()
         ctx.translate(75,75)
         for i in range(6):
             ctx.save()
             ctx.fillStyle = 'rgb('+(51*i)+','+(255-51*i)+',255)'
             for i in range(6):
                 ctx.rotate(Math.PI*2/(i*6))
                 ctx.beginPath()
                 ctx.arc(0,i*12.5,5,0,Math.PI*2,true)
                 ctx.fill()
             ctx.restore()

     @clientside
     def clearCanvas():
         ctx = getContext()
         ctx.clearRect(0, 0, 150, 150)

Here's the ZPT:

<html metal:use-macro="context/@@standard_macros/view">
   <script
       metal:fill-slot="headers"
       tal:content="structure view/_anguine__js_code"/>
   <body metal:fill-slot="body" tal:omit-tag="">
     <br>
     <canvas id="test_canvas" width="150" height="150"
         style="border: 1px solid black;"></canvas>
     <button onclick="draw()">Draw</button>
     <button onclick="clearCanvas()">Clear</button>
   </body>
</html>

I consider this a mad scientist experiment, so I don't expect it to be 
officially included anywhere or even get any work from me once the 
novelty wears off.  You are warned; I don't want angry villagers with 
pitchforks and torches coming to my castle. :)
-- 
Benji York
Senior Software Engineer
Zope Corporation


More information about the Zope3-dev mailing list