| ryah ( @ 2007-05-08 15:41:00 |
| Entry tags: | projects, upload progress bar |
Ajax.Pull
ajax_pull-0.1.zip
This is my Ajax stream pulling code. It's very simple: you make a XMLHttpRequest call to a server which streams a list of JSON objects separated by semi-colons. You give the library a handler function and this function gets called with each new JSON object that comes from the server.
The idea is that you can build things like upload progress bars without polling the server for each update.
If browsers followed the specification, this would work. I can assure you it won't work in Safari (419) due to a bug but I'm encouraged by
natan's hopeful prediction that with OSX 10.5, Apple will release Safari bug fixes to all users. Probably doesn't work in IE either. But maybe someone will still find it interesting, like, theoretically.
To try it
- run
ruby test_mongrel.rbto start a test web server on port 4711. - Navigate to http://localhost:4711/files/test.html.
Do you see an ASCII face and numbers updating once per second?
Writing javascript/prototype is hilarious. Monstrous statements like this are considered clean (I think)
slice.extractJSON().each((function(statement) {
this.handler(eval( '(' + statement + ')' ));
this.pullPointer += statement.length + 1;
}).bind(this));