PDA

View Full Version : Buffered Ajax Requests



DhakouaniM
26 Apr 2008, 7:02 AM
Hi guys !

I needed a way to buffer Ajax requests in the same way it is currently done a function in Ext.util.DelayedTask.

Here's my code, in case anyone would need that too...


Ext.ux.BufferedAjaxRequest = function(options) {
// options: take all Ext.Ajax.Request input parameters and store them globally
// also takes a 'buffer' parameter for the overriden
// DelayedTask (defaults to 0), expressed in milliseconds
var baseOptions = options || {};
Ext.applyIf(baseOptions, {
params: {},
buffer: 0
});
var requestId;
var lastOptions;

this.request = function(options) {
// Allows to override constructor parameters
var buffer = options.buffer || baseOptions.buffer;
delete options.buffer;
if (requestId) {
this.cancel();
Ext.Ajax.abort(requestId);
}
var params = Ext.apply({}, baseOptions.params);
Ext.apply(params, options.params || {});
delete options.params;
lastOptions = Ext.apply({}, baseOptions);
Ext.apply(lastOptions, options);
Ext.apply(lastOptions, {
params: params
});
this.delay(buffer);
};
var _request = function() {
requestId = Ext.Ajax.request(lastOptions);
};

Ext.ux.BufferedAjaxRequest.superclass.constructor(_request, this);
};
Ext.extend(Ext.ux.BufferedAjaxRequest, Ext.util.DelayedTask);


Then, you use it like a regular Ext.Ajax.request(...) but you add a 'buffer' parameter:

this.ajax = new Ext.ux.BufferedAjaxRequest({
url: NileCMIS.RootURL,
params: {
option: 'a',
task: 'b',
object: 'c'
},
callback: this.callback,
scope: this,
buffer: 200
});

this.ajax.request({
params: params
});


You can override any of the parameters passed in the constructor when executing the 'object'.request call...

Mehdi