Queuing ajax requests with MooTools 1.2
UPDATE: This might be sorta useless. Please see this comment.
There have been a variety of occasions where I only want one instance of the Request object, but want to make sure that every request I attempt gets run without canceling another, or screwing around with onCompletes. The following is a rather simple implement that takes care of this problem:
Request.implement({ queue: function(sendArg){ if(!$defined(this.queued)) this.queued = []; this.queued.push(sendArg); this.processQueue(); }, processQueue: function(){ if(this.timer) $clear(this.timer); if (!this.check()) this.timer = this.processQueue.delay(250, this); else { if ($defined(this.queued[0])) { this.send(this.queued.shift()); this.processQueue(); } else this.fireEvent('onQueueEmpty'); } } });
This will basically stack requests and send them in the order received. If there is no stack, the request will be sent immediately.
The usage is the same as the normal Request send method, however there is now an onQueueEmpty event to tell when all requests in the queue have been sent.








July 30th, 2008 at 3:47 pm
Very, very useful, I was working on something similar.
July 30th, 2008 at 4:19 pm
@keif: i agree =)
I am pretty surprised that something like this hasn’t made its way into the core yet.
August 6th, 2008 at 2:12 am
The request class in mootools 1.2 already includes chaining!
The check method allows you to queue, cancel or ignore requests while there is already a request running (identical to the Fx classes) depending on the option you provide. (look at the source!)
So to do this, you don’t need to add any code (ie @atom, it is already in the core…)
Simply provide {link: ‘ignore’ | ‘chain’ | ‘cancel’} as required. The default is ignore, so you won’t have seen it and I’ve no doubt it’s not documented yet…
*rolls eyes
Honestly, since the forums were shut this sort of thing seems to be happening a lot.
To implement ‘onChainComplete’ (like you did for the queue), just add an event to onSuccess that checks the length of the chain
ps. I *Adore* your design…
August 6th, 2008 at 3:00 am
@adamnfish you are absolutely correct =)
My implement is indeed useless. It is not in the documentation, but setting ‘link’:'chain’ does work to chain/queue requests.
After testing I think I have either discovered a MooTools bug, or a Firebug bug, or some other manner of oddity. When using ‘link’:'chain’, I am not seeing a response logged in Firebug, however the response does exist, as I can still use it when passing to the onSuccess function.
So, uh, someone look into that or something.
August 20th, 2008 at 4:01 pm
“When using ‘link’:’chain’, I am not seeing a response logged in Firebug, however the response does exist, as I can still use it when passing to the onSuccess function.”
Same here, firebug doesn’t show the request until it’s already done. And when it’s done, it tells me that it only took 1ms (which can’t be true, I’ve put a sleep(2) in my php). Odd, odd, odd… link: ‘chain’ however works like a charm! I hope they’ll update the docs soon.
December 6th, 2008 at 12:46 am
it looks like someone else thought thinks queueing needs a little help, however his is mroe useful and doesn’t just stack, which you can already apparently do:
http://www.clientcide.com/code-snippets/ajax/new-plugin-requestqueue/