|
Val's Blog
Lots of stuff for Web 2.0 freaks and Javaholics
|
|
|
Ed Yourdan: "There is nothing in the programming field more despicable than an undocumented program." |
[ Login ] |
|
It should come as no surprise to any well-informed Web 2.0 developers out there that the Ajax tech box can quickly morph into Pandora's box if not handled with care. This is mainly due to the fact that the
Experience has shown that web developers are more concerned about knowing when they have to initiate new connections than thinking about the conditions under which they should have to abort them. What this means is that basically very few developers make use of the
On some project, we are using the jQuery JavaScript library and a host of jQuery plugins for enriching jQuery's core to best suit our needs. jQuery provides excellent support for Ajax by means of its general-purpose utility function called
One of the plugins we are using is the jQuery Form plugin. One of the useful features of that plugin is to provide a way to ajaxify any HTML form using the
There are many ways to solve this problem. One of them is to modify the Form plugin to return the
Basically, the jQuery AOP plugin provides very primitive support for weaving before, around and after advice into existing code as well as removing any previously weaved advice. Even though this plugin does not provide full-fledged AOP capabilities (i.e., no introduction support, no aspect modularization support, no wildcards in pointcut expressions), it is lightweight (943 bytes!!) and it does cover our need, which basically consists of wrapping all calls to In terms of implementation, our need would be covered by the following code. Our code is actually a little more involved than that, but the following code basically shows the main ideas of what we wanted to achieve:
var ajaxRequests = [];
//registers all unterminated Ajax requests
function registerAjaxRequests() {
var advice = function(invocation) {
var ajaxRequest = invocation.proceed();
if(ajaxRequest.readyState < 4) {
ajaxRequests.push(ajaxRequest);
}
return ajaxRequest;
}
$.aop.around({target: jQuery, method: 'ajax'}, advice);
}
// abort pending ajax requests
function abortAjaxRequests() {
while (ajaxRequests.length > 0) {
var ajaxRequest = ajaxRequests.shift();
if (ajaxRequest.readyState != 4) {
ajaxRequest.abort();
}
}
}
The job of the
The other function called
So, using the jQuery AOP plugin that heavily builds upon the dynamic and reflective nature of the JavaScript language, we could very easily get around a design flaw in the jQuery Form plugin which would prevent us from getting hold of the
TrackBacks[0]
Comments[0]
Posted by val on December 5, 2007 2:53:31 PM CET
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Content © Val | Powered by Pebble 1.9.1 |