Saturday, December 16, 2006

Updated sample and ajax.lib.js

The quick example now has some basic formatting and a link to run it.

More interestingly, from my perspective at least, I've updated the AjaxRequest and AjaxRequestManager objects.

The AjaxRequest object now has four events (open, send, load, and abort), and now has an actually readable onreadystatechange listener by using a quick trick in order to keep this references intact:

this.xhr.onreadystatechange = new Function("AjaxRequest.prototype.stateChanged.apply(requests["+id+"], arguments);");

The AjaxRequestManager now emulates support for event listening, in that it can accept addEventListener and removeEventListener calls. But instead of dispatching events, it auto-adds the listeners to each of the AjaxRequest objects it creates. This makes it a lot easier to write, for example, a Throbber object, since it can get added as a listener to each request in order to keep an eye on things while keeping things nice and loosely coupled.

Edited so you can read the code...

Labels: , ,

2 Comments:

Jason said...

Oh man.. Why haven't we been using Apply, Call and Watch all along?!

These really do seem like deep dark secrets of javascript..

I'd recommend posting references to them in the book to show people that "See I'm not making this up"..

Its too bad that JS doesn't really know what 'THIS' means.. but what can ya do..

11:39 AM  
Frozen O said...

'I'd recommend posting references to them in the book to show people that "See I'm not making this up"..'

Posted and noted for inclusion in the book. :-)

4:30 PM  

Post a Comment

Links to this post:

Create a Link

<< Home