简体   繁体   English

Rails UJS:在触发新的 ajax 请求时中止先前触发但仍处于活动状态的 ajax 请求

[英]Rails UJS: abort previously triggered but still active ajax requests when triggering a new ajax request

I have a Ruby on Rails 4.2 app.我有一个 Ruby on Rails 4.2 应用程序。

I have 3 buttons that trigger ajax requests with Rails UJS我有 3 个按钮使用 Rails UJS 触发 ajax 请求

<section id="buttons"> 
  <%= link_to image_tag(image),
        button1_modal_path,
        remote: true, 
        id:"icon1",
        data: { disable_with: '' } %>
  <%= link_to image_tag(image),
        button2_modal_path,
        remote: true, 
        id:"icon2",
        data: { disable_with: '' } %>
  <%= link_to image_tag(image),
        button3_modal_path,
        remote: true, 
        id:"icon3",
        data: { disable_with: '' } %>
</section>

I wish to implement the following pattern:我希望实现以下模式:

  • user clicks on button 1 launching a ajx xhr request (let(s call it ajaxRequest1)用户单击按钮 1 启动 ajx xhr 请求(我们称之为 ajaxRequest1)

  • the ajax is still being processed (and whatever state it is on : sent, already dealt with by server, on itw way back...) but has not arrived (200) and user clicks on button 2 triggering another xhr ajax request (ajaxRequest2) ajax 仍在处理中(无论它处于何种状态:已发送,已由服务器处理,在返回途中......)但尚未到达(200)并且用户单击按钮 2 触发另一个 xhr ajax 请求(ajaxRequest2 )

  • I would like in that case that all other active ajax requests (that is to say in this case ajaxRequest1) are canceled.在这种情况下,我希望所有其他活动的 ajax 请求(也就是说在这种情况下是 ajaxRequest1)都被取消。 If there was more than only one more requests, it would cancel them all except the last ajax request (that is to say ajaxRequest2)如果多了一个请求,除了最后一个ajax请求(也就是ajaxRequest2),它会取消所有请求

I tried this using Rails UJS ajax events but it does not work (it actually cancels ANY ajax request to be triggered:(:我使用Rails UJS ajax 事件尝试了这个,但它不起作用(它实际上取消了要触发的任何 ajax 请求:(:

on('ajax:beforeSend',function(event, xhr, settings){
  xhr.onreadystatechange = null;
  xhr.abort();
}

NOTE: I would like in the best case that I don't only cancel the fact the browser is waiting the ajax request return and display its content but ALSO that if the request has been launched but not yet hit the server, it is canceled before it does hit the server, in order not to overload the server with useless ajax request .注意:我希望在最好的情况下,我不仅取消浏览器正在等待 ajax 请求返回并显示其内容的事实,而且还希望如果请求已启动但尚未到达服务器,则它会在之前被取消它确实命中了服务器,以免用无用的 ajax 请求使服务器过载

If only one rails remote call should run at any time, you can remember the current XHR and abort it when a new one starts, ie如果任何时候只有一个 rails 远程调用应该运行,你可以记住当前的 XHR 并在新的启动时中止它,即

var currentXhr;
currentXhr = null;

$(document).on('ajax:send', function(_event, xhr) {
  if (currentXhr) {
    currentXhr.abort();
  }
  currentXhr = xhr;
  return true;
});
$(document).on('ajax:complete', function(_event, _xhr, _status) {
  currentXhr = null;
});

To cancel request 1, you should call abort on the XMLHttpRequest object according to request 1, for example要取消请求 1,您应该根据请求 1 对XMLHttpRequest对象调用 abort,例如

ajaxRequest1.abort();

The meaning of your code is to abort a request before it is sent.您的代码的含义是在发送请求之前中止请求。

on('ajax:beforeSend',function(event, xhr, settings){
  xhr.onreadystatechange = null;
  xhr.abort();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM