相当于 Jquery 的 ajaxStart ajaxStop 的 SuperAgent
SuperAgent equivalent of Jquery's ajaxStart ajaxStop
我的团队目前正试图摆脱我们的 jQuery。我们已经设法摆脱了所有选择器,并且正在从我们的 ajax 调用中重构它,但我们正在尝试重新创建 ajaxStart 和 ajax停止功能。
我一直在查看 SuperAgent 文档,但找不到与此等效的内容。有谁知道 SuperAgent 中有类似的东西,或者知道如何用事件监听器或其他东西重新创建它?
我的替代方法是直接向每个请求添加显示更改,这是我想避免的 200 多行。
window.onload = function() {
$(document)
.ajaxStart(function(){
document.getElementById('ajaxSpinner').style.display = 'block';
})
.ajaxStop(function(){
document.getElementById('ajaxSpinner').style.display = 'none';
});
}
编辑:我们已经弄清楚如何在我们的代码库中使用已接受的答案。我们已将所选答案中的代码移动到我们在任何使用 SuperAgent 的地方都需要的模块中。在我们的每个调用中,我们现在都包含 .use(Module.stats)。到目前为止,这个解决方案似乎有效,但我们还没有开始跨浏览器测试。
感谢您的帮助!
Edit2:场合要求我们重建应用程序。接受的答案不适用于最新版本的 SuperAgent,我们不得不将其回滚到版本 1.7.2。
https://github.com/visionmedia/superagent/issues/861#issuecomment-173413292
希望以上link对您有所帮助
引用代码:
function stats(req) {
req.once('request', function() {
// ajaxstart
req._startedAt = Date.now();
});
req.once('error', function() {
// an error,the 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);
}
我的团队目前正试图摆脱我们的 jQuery。我们已经设法摆脱了所有选择器,并且正在从我们的 ajax 调用中重构它,但我们正在尝试重新创建 ajaxStart 和 ajax停止功能。
我一直在查看 SuperAgent 文档,但找不到与此等效的内容。有谁知道 SuperAgent 中有类似的东西,或者知道如何用事件监听器或其他东西重新创建它?
我的替代方法是直接向每个请求添加显示更改,这是我想避免的 200 多行。
window.onload = function() {
$(document)
.ajaxStart(function(){
document.getElementById('ajaxSpinner').style.display = 'block';
})
.ajaxStop(function(){
document.getElementById('ajaxSpinner').style.display = 'none';
});
}
编辑:我们已经弄清楚如何在我们的代码库中使用已接受的答案。我们已将所选答案中的代码移动到我们在任何使用 SuperAgent 的地方都需要的模块中。在我们的每个调用中,我们现在都包含 .use(Module.stats)。到目前为止,这个解决方案似乎有效,但我们还没有开始跨浏览器测试。 感谢您的帮助!
Edit2:场合要求我们重建应用程序。接受的答案不适用于最新版本的 SuperAgent,我们不得不将其回滚到版本 1.7.2。
https://github.com/visionmedia/superagent/issues/861#issuecomment-173413292
希望以上link对您有所帮助
引用代码:
function stats(req) {
req.once('request', function() {
// ajaxstart
req._startedAt = Date.now();
});
req.once('error', function() {
// an error,the 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);
}