Set default application-wide timeout for xhr requests in Dojo (so that JsonRest calls are affected too)

若如初见. 提交于 2019-12-08 03:55:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!