dojo xhrPost 进度(回调)事件侦听器?

dojo xhrPost progress (callback) event listener?

在 dojo API 文档中,我可以找到 loaderror 的回调,但我缺少 progress.

的回调
load: function(data) {
    dojo.byId("response").innerHTML = "Form posted.";
},
error: function(error) {
    dojo.byId("response").innerHTML = "Error...";
}

XMLHttpRequest API 提供了为 progress 事件注册事件侦听器的可能性:

var oReq = new XMLHttpRequest();

oReq.addEventListener("progress", updateProgress, false);

oReq.addEventListener("load", transferComplete, false);

oReq.addEventListener("error", transferFailed, false);

oReq.addEventListener("abort", transferCanceled, false);

oReq.open();

// progress on transfers from the server to the client (downloads)
function updateProgress (oEvent) {...}

dojo有没有一些机制,类似于XMLHttpRequestAPI?

查看 dojo/xhr 文档。它提到仅当浏览器支持这些事件时才能处理进度事件。

以下是文档中的代码片段。

dojo/request/xhr() returns 一个 promise 由响应的已处理数据实现。如果提供,错误将被定向到 errback。 进度 数据将提供给 进度处理程序 如果提供并且浏览器支持 XHR2 进度事件.

require(["dojo/request/xhr"], function(xhr){
  xhr("example.json", {
    handleAs: "json"
  }).then(function(data){
    // Do something with the handled data
  }, function(err){
    // Handle the error condition
  }, function(evt){
    // Handle a progress event from the request if the
    // browser supports XHR2
  });
});

浏览器平台上的 new dojo/request module supports progress events. It will automatically call the new dojo/xhr 模块。

不要将这些 API 与旧的、已弃用的对应项混淆。模块的包含和调用方式存在主要差异:

  1. 不同的文件必须以不同的方式包含(AMD 而不是 require(dojo.*)
  2. 必须以不同的方式调用方法并且 return 必须使用不同的类型。新请求 API 使用承诺而不是回调。

使用以下指南将您的代码转换为新的 API(可能需要更新到更新版本的 Dojo):


更新: 如果您必须 使用 Dojo 1.6...

您有两个选择:

  1. 仅在需要 progress 事件的部分使用 Dojo 1.6 旁边的更现代的 Dojo。新的 AMD 架构意味着新库与 require() 回调块隔离。 AMD 架构还意味着您可以只加载新 xhr 所需的现代 Dojo 的最少部分。
  2. progress 事件功能反向移植到 Dojo 1.6。您可以添加另一个回调和 code doesn't seem that complex。 (不要忘记考虑差异,因为您没有使用承诺。)