superagent way to do ajaxStar and ajaxStop

Created on 20 Jan 2016  路  5Comments  路  Source: visionmedia/superagent

In jquery
there is ajaxStar and ajaxStop to listen the ajax event.

how to do that with superagent?

because I'm using React, so I give up jquery, though it has ajax hepler, I do not need any dom manipulation, including jquery looks a kind of waste

Most helpful comment

Seems I did not make myself clear.
the link you show me is just a sentence.
2016-01-20_233530

what can this .abort method do with ajaxStar/Stop?

You may read the api first.

after read all the api, I still can not find any api I can use to implement my goal. :cry:

can you look this question in stackoverflow?
http://stackoverflow.com/questions/33219794/superagent-equivalent-of-jquerys-ajaxstart-ajaxstop

the question author is faced with the same problem with me.

All 5 comments

http://visionmedia.github.io/superagent/#aborting-requests

You may read the api first.

Seems I did not make myself clear.
the link you show me is just a sentence.
2016-01-20_233530

what can this .abort method do with ajaxStar/Stop?

You may read the api first.

after read all the api, I still can not find any api I can use to implement my goal. :cry:

can you look this question in stackoverflow?
http://stackoverflow.com/questions/33219794/superagent-equivalent-of-jquerys-ajaxstart-ajaxstop

the question author is faced with the same problem with me.

I am sorry. I made a mistake.

function stats(req) {
  req.once('request', function(){
    // ajaxstart
    req._startedAt = Date.now();
  });
  req.once('error', function(){
    // an error锛宼he request fail
  });
  req.once('end', function(){
    // ajaxstop
    var use = Date.now() - req._startedAt;
  });
}

function get(url) {
  return request.get(url)
    .use(stats);
}

Request.prototype._end = Request.prototype.end;
Request.prototype.end = function(fn) {
  this.emit('request');
  this._end(fn);
}

wow~

In current superagent, the equivalent functionality would be:

  1. listen for the 'request' event in place of ajaxStart
  2. Use the .end() callback in place of ajaxStop

I would consider a PR to have superagent emit an end event (for responses, aborts, and errors) in the same place we invoke the .end() callback so there's a consistent event model without having to mix event handler and callbacks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tj picture tj  路  9Comments

tbo picture tbo  路  4Comments

t3hmrman picture t3hmrman  路  9Comments

vlinder picture vlinder  路  8Comments

littlee picture littlee  路  8Comments