Tue 08 Mar 2005
How Does It Work?
Here's a CL-AJAX version of the
ModernMethod tutorial for SAJAX, which does exactly the same thing in PHP (though with a bit less functionality on the server side, as far as I can tell). I've cribbed some of the text to make the similarity as similar as possible. Apologies to ModernMethod, and to my readers for the abysmal
<pre> formatting.
- Include the library:
(asdf:operate 'asdf:load-op :cl-ajax)
- Initialise it and tell it what functions you wish to export:
;; the function we will be exporting
;; to JavaScript:
(defexported multiply (x y) (* x y))
;; set the server running:
(install-handler
(http-listener-handler *listener*)
(make-instance 'ajax-function-handler)
*site-url* nil)
(start-listening *listener*)
- Setup your HTML (including the JavaScript the library generates)
<html>
<head>
<title>Test</title>
<script>
<!--
here Araneida inserts the results of
(build-preamble *site-url*)
-->
function set_math_result(result) {
document.getElementById("z").value = result;
}
function do_the_math() {
var x, y;
x = document.getElementById("x").value;
y = document.getElementById("y").value;
// The Lisp function (multiply) has been
// linked to a javascript function named
// ajax_multiply() with defined args. call it.
// Also specify if XML will be expected.
ajax_multiply(x, y, set_math_result, false);
}
</script>
</head>
<body>
<input type="text"
name="x" id="x"
value="2" size="3">
*
<input type="text"
name="y" id="y"
value="3" size="3">
=
<input type="text"
name="z" id="z"
value="" size="3">
<input type="button" name="check"
value="Calculate"
onclick="do_the_math(); return false;">
</body>
</html>
Thanks to security features in most modern browsers this won't work if you just save it locally — I threw a quick
static-file-handler into Araneida to serve the saved HTML, and it worked like a charm:
That's the raw XML response, the form from the original SAJAX example calling into CL-AJAX, and Vim showing the (frankly awful) generated code.
Posted at 2005-03-08 15:57:06 by Richard • Link to How Does It Work?
Small addition to cl-ajax
If you want to be able to return arbitrary expressions, not just strings (i.e. to have the response printed) change line
301 from
(format (request-stream request) (run-function fun-valid args keys))))to
(format (request-stream request) "~A" (run-function fun-valid args keys))))This shouldn't break anything, but makes your functions more versatile. It still won't allow arguments to be evaluated, though… I leave turning that on as an exercise for the reader!
Update: I've rolled this into the source. First point update!
Posted at 2005-03-08 14:51:33 by Richard • Link to Small addition to …
Great Widget idea
mr100percent on
MacNN suggested a great Tiger widget:
Anyone who knows how to create a web page can create a simple widget. I know only very basic HTML, but I'm going to put together an interface to http://www.everything2.com in widget form when I can get my hands on it.
Good God, I'd never get anything done. Whenever I visit that site I spend hours just clicking around.
Stuff To Do With Your Own Blood is an old favourite.
Posted at 2005-03-08 14:00:21 by Richard • Link to Great Widget idea
cl-ajax 0.1
I've finished it… run an Araneida server with a handler, and export a few functions. The library generates Javascript to call the exported functions — all you have to do to make a rich Web application is write a few callbacks to write data into the DOM, and wham!
The ASDF package is
here; the GPG signature is
here. I've even
put up a CLiki page, and a projects page on this site will follow when I get round to it. Let me know how you get on!
Posted at 2005-03-08 13:52:27 by Richard • Link to cl-ajax 0.1