PDA

View Full Version : Ext.Ajax suggestion



mykes
31 Mar 2008, 6:25 PM
Correct me if I'm wrong...

I suggest that a retry() method be added to the Ext.Ajax class. As implemented, if you want to implement retry, you end up doing something recursive, which can cause your javascript serious problems:



var retry_count = 10;
function do_request() {
Ext.Ajax.request({
url: some_url,
success: function(response, obj) {
... whatever you do when it succeeds
},
failure: function(response, obj) {
if (--retry_count > 0) {
// retry!
do_request(); // again! (Recurse)
}
else {
alert('error!');
}
}
});
}
As you can see, the do_request() call inside the failure handler is going to recurse 10 times. If you don't limit it, it could recurse too many times you get a browser error. It's creating up to 10 nested closures and Ext.Ajax objects, too.

If retry() is implemented, there would be no recursion:


Ext.Ajax.request({
retries: 10,
url: some_url,
success: function(response, obj) {
... whatever you do when it succeeds
},
failure: function(response, obj) {
if (--obj.retries > 0) {
// retry!
retry(); // again! (but not recursive!)
}
else {
alert('error!');
}
}
});

The retry() method would also be useful if you are simply doing the same ajax request many times. An example might be an RSS reader that uses Ajax to request updates from the server periodically; the Ajax request would be identical each "poll."

jack.slocum
31 Mar 2008, 8:20 PM
There's an event you can use to wire up that behavior and extend Ext.Ajax (globally) with what you want pretty easily:



Ext.Ajax.on('requestexception', function(conn, response, options){
if(options.retry){
options.retry--;
Ext.Ajax.request(options);
}
});


(Note: this is untested and off the top of my head! ;))

mykes
1 Apr 2008, 6:23 AM
Two questions:
Is extending Ext.Ajax globally a good thing?

and

The failure handler gets called when? That is, would it get called 10x?

esoteric
1 Apr 2008, 6:31 AM
Depends on what you mean by good? Do you want this to happen with every Ajax request in your application? If you do then it is good, if you don't then it is bad.

It gets called when a failure happens and you would have to preset "options.retry" to whatever value you want so if you want it to be 10 then set it to 10 then it would be retried 10 times.

mykes
1 Apr 2008, 6:45 AM
I got that Jack's example would retry 10x if .retry is set to 10.

It's just that you only want to retry certain types of transactions.

It seems that Jack's code would add a small amount of overhead to those requests you don't want to retry. It's safe for requests that have no .retry at all, even.

My point is that it seems that extending global singleton objects like Ext.Ajax isn't a good answer because you never know what that might do to extensions or otherwise other peoples' code you might be using.

In this case, 'retry' doesn't look like it could possibly harm anything. Unless, that is, Ext.ux.UploadDialog sets it's internal Ajax.request config.retry to something for its own internal purpose. For example.

esoteric
1 Apr 2008, 6:56 AM
There is no 'retry' unless you set it, that's his and my point. If you set 'retry' then it will 'retry' that many times.

jack.slocum
1 Apr 2008, 7:01 AM
As esoteric said, it only gets called when an error occurs. It only actually happens if retry is set. The overhead is very minimal and is probably less code than it would take to add it internally.

Extending core Ext singletons via events is not a bad thing, it's a good thing. That's what the events are for. ;)

mykes
1 Apr 2008, 7:04 AM
There is no 'retry' unless you set it, that's his and my point. If you set 'retry' then it will 'retry' that many times.

Think about the example I just gave, regarding Ext.ux.UploadFileDialog. What if it uses config.retry for his ajax.requests for some entirely different purpose. Jack's code would hijack the program control flow in a way that would likely break this wonderful extension. Not only does it hijack the control flow, it alters a variable that the extension defines and uses.

Modifying the behavior of a global singleton object in Ext is basically changing the way Ext works. You can't expect anyone else's code to work if you've modified Ext's behavior so it doesn't work as advertised.

mykes
1 Apr 2008, 7:25 AM
My last post on this topic.

Suppose I release a nifty extension called Ext.ux.WebChatPanel. Web Chat needs to send anything you type to the server so everyone else sees it in their browsers. It needs to retry on failure, otherwise you end up typing to /dev/null.

So I globally alter Ext.Ajax's behavior and add .retry to the config/options. Now anyone who installs and uses my extension has their Ext.Ajax behavior altered. There could be a lot of head scratching as to why their existing code suddenly broke. It's also a hard "bug" to find.

jack.slocum
1 Apr 2008, 5:41 PM
Give the option a name that avoids any confusion. webChatPanelHttpRetry: 10