Georg Kastenhofer
Georg Kastenhofer

Reputation: 1417

dojo xhrPost progress (callback) event listener?

In the dojo API documentation I can find callbacks for load and error but I am missing a callback for progress.

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

The XMLHttpRequest API provides the possibility, to register event listeners for the progress event:

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) {...}

Has dojo some mechanism, similar to the XMLHttpRequest API?

Upvotes: 2

Views: 569

Answers (2)

Leftium
Leftium

Reputation: 17903

The new dojo/request module supports progress events. It will automatically call the new dojo/xhr module on browser platforms.

Don't confuse these API's with the old, deprecated, counterparts. There are key differences in the way the modules are included and called:

  1. Different files must be included in a different manner (AMD instead of require(dojo.*))
  2. The methods must be called differently and return different types. The new request API uses promises instead of callbacks.

Use the following guides to convert your code to the new API (may require updating to a more recent version of Dojo):


update: If you must use Dojo 1.6...

You have two options:

  1. Use a more modern Dojo next to Dojo 1.6 only for the part that needs the progress event. The new AMD architecture means the new library is isolated to the require() callback's block. AMD architecture also means you can load only the minimal parts of the modern Dojo required for the new xhr.
  2. Backport the progress event feature to Dojo 1.6. You can add another callback and the code doesn't seem that complex. (Don't forget to account for differences because you're not using promises.)

Upvotes: 1

frank
frank

Reputation: 3330

Have a look at the dojo/xhr document. It mentions how progress event can be handled only if the browser supports those events.

Following is a code snippet from the documentation.

dojo/request/xhr() returns a promise that is fulfilled with the handled data of the response. Errors will be directed to the errback if provided. Progress data will be given to the progress handler if it is provided and if the browser supports XHR2 progress events.

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
  });
});

Upvotes: 1

Related Questions