问题
I just discovered that in Dojo there is no way to set an application-wide timeout for xhr calls. In theory I could easily create my own wrapper to xhr and use that. However, I am using JsonRest stores (the aim is to only ever use the stores to interact with data in the application).
Amazingly, there is no way to set the timeout for JsonRest calls in Dojo.
http://mail.dojotoolkit.org/pipermail/dojo-interest/2012-April/065594.html
Is there a way to overload the default xhr calls without having to keep my own copy of JsonRest...? Could I do it with aspects for example? Ideas/comments?
回答1:
The easiest method is probably some good old Monkey Patching.
var old_xhr = dojo.xhr;
dojo.xhr = function(options) {
var opts = options || {};
opts.timeout = Math.min(opts.timeout || Number.MAX_VALUE, 1000);
return old_xhr(options);
}
This will apply a maximum timeout of 1 second for all users of the dojo.xhr
method. It's a bit trickier using the AMD loader with a baseless dojo where every module explicitly requires dojo/_base/xhr
, but it can be done by using some of the advanced aliasing capabilities of the dojo loader to map dojo/_base/xhr
to your own module.
来源:https://stackoverflow.com/questions/11813665/set-default-application-wide-timeout-for-xhr-requests-in-dojo-so-that-jsonrest