Question

In relation to this question, How do I fully intercept AND requeue http requests using dojo

I am interested in something similar, would dojo/aspect better suit?

Looking at the documentation here, http://dojotoolkit.org/reference-guide/1.9/dojo/aspect.html The example given shows how to intercept dojo.xhr, but dojo.xhr is deprecated since 1.8??. and replaced by dojo/request. For the new API, dojo/request/notify is recommended by dojo documentation.

I am confused which one to use or should I use both methods? So I can cover all cases where some legacy codes are still using the old dojo.xhr API.

The other complication I can think of for using both methods, may be both methods are called for the same xhr request, thus duplicating the work.

Était-ce utile?

La solution

dojo.xhr is deprecated in favor of dojo/request/xhr, the documentation for which you can find here.

You can still use dojo/aspect as in the example. I've created a fiddle that does just that. The relevant code follows.

require([
    'dojo/request/xhr',
    'dojo/aspect'
], function(xhr, aspect) {
    aspect.before(xhr, 'post', function() {
        console.log('before POST');
    });

    // use xhr.post here...
});

If you still want to cover legacy code that uses dojo.xhr, you can extract the function you use in aspect and pass it to both objects:

function beforeXhr() {
    // ...
}

aspect.before(dojo, 'xhr', beforeXhr);
aspect.before(xhr, 'post', beforeXhr);

I haven't figured out how to use dojo/aspect on functions that are returned to the require callback, which means you'd have to repeat the aspect calls for 'GET', 'PUT', 'DELETE', etc. If anyone knows how to use aspect with a function object as in this example, I'd love to know.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top