↓ Skip to main content

30 Second Dojo

·244 words

I read Rasmus’s 30 second AJAX tutorial [via] and though it is nice enough in its own respect, it is missing two things: Dojo and Python.

So I rewrote it using Dojo. I am really liking Dojo, last Friday at the shop I used termie’s Dojo Done Quick (main file seems to be down or moved) to roll my first own widget, but this is really just about the IO part.

Main improvement here is that after a while you get sick rolling your own XmlHttpRequest all the time, concatenating your own request parameters and checking for readyStates.

Rather than rolling your own library, use one which is terrific and takes most of the redundancy out (working HTML):

<html>

<head>

<script type="text/javascript" src="dojo/dojo.js"></script>

<script type="text/javascript">
dojo.require("dojo.io.*");

function sendRequest(action) {
// Documentation here: http://manual.dojotoolkit.org/io.html
dojo.io.bind({
url: '/cgi-bin/30action.py', // URL to call
content: {action: action},
// mimetype: 'text/plain' is implied
// method: 'GET' is implied
// sync: false (asynchronous) is implied
load: function(type, data, evt) {
var parts = data.split('|');
dojo.byId(parts[0]).innerHTML = parts[1];
}
// error: function(type, errorObject) {} omitted for brevity
})
}
</script>

</head>

<body>

<a href="javascript:sendRequest('foo')">[foo]</a>

<div id="foo">
</div>

</body>


</html>

11 lines of Javascript.

And the simple Python code:

#!/usr/bin/env python
import os

print 'Content-type: text/plain'
print

query = os.environ['QUERY_STRING']
parts = [part for part in query.split('&') if '=' in part]

query = {}
for part in parts:
	query[part.split('=')[0]] = part.split('=')[1]

if 'action' in query and query['action'] == 'foo':
	print "foo|foo done"
Alper Cugun
Author
Alper Cugun

Related

Eclipse 3.2

·259 words
I am running on a new Eclipse 3.2.0 in the shop right now. It’s pretty nice with some cosmetic tweaks and it feels a bit faster.