jQuery on serverside with taconite
Home Home Contact us Contact
home News Projects Tests Files Follow manatlan on Twitter Sign in
back 2009/08/21 15:44

jQuery on serverside with taconite

In my quest to find the better ajax method ;-). I'd found a way to send back multiple actions easily, using the jQuery Taconite plugin (thanks to rupert.lloyd's recipe, which gave me this idea)

Taken from taconite's website :

The jQuery Taconite Plugin allows you to easily make multiple DOM updates using the results of a single AJAX call. It processes an XML command document that contain instructions for updating the DOM.

On serverside, I had just created a simple class helper to build out the taconite xml format.

class jQuery(object):
    def __init__(self): self._x=[]

    def __getattr__(self,cmd):
        def _d(*args,**kargs):
            _p=lambda a: a.replace("&","&amp;").replace('"',"&quot;").replace('>',"&gt;").replace('<',"&lt;")
            assert len(args)>0
            if kargs: assert "content" in kargs

            if "content" in kargs: # element commands
                self._x.append(u"""<%s select="%s">%s</%s>""" % (cmd,_p(args[0]),kargs["content"],cmd))
            else:
                if cmd=="eval": #eval commands
                    self._x.append(u"<eval>\n<![CDATA[\n%s\n]]>\n</eval>" % args[0])
                else:   # non-element commands
                    a=[u'''%s="%s"'''%(k,_p(v)) for k,v in zip(["select","arg1","arg2","arg3","arg4"],args)]
                    self._x.append(u"""<%s %s/>""" % (cmd,u" ".join(a)))
        return _d

    def __repr__(self):
        return u"<taconite>\n%s\n</taconite>" % "\n".join(self._x)

Now, imagine a simple html page like this :

<html>
    <head>
        <script type="text/javascript" src="/static/jquery.js"></script>
        <script type="text/javascript" src="/static/jquery.taconite.js"></script>
    </head>
    <body>
        <div id="content"></div>
        <button onclick="$.get('ajax');">test</button>
    </body>
</html>

And this python handler on the serverside :

class AjaxHandler:
    def GET(self):
        self.header("Content-type","text/xml")
        j=jQuery()
        j.append("#content",content="hello.")
        j.attr("#content","style","border:2px dotted red")
        j.eval("alert();")
        return j

The html button test, will call the ajax url, which will answer by appending some content in the flow of div#content, by setting a border to div#content, and by evaluating the javascript code "alert()", on the clientside. (according to taconite commands like described here).

Nifty ;-)

RSS Python Powered Get Ubuntu
©opyleft 2008-2019 - manatlan