Rails ujs 和远程表单,如何停止以编程方式提交表单的表单提交

Rails ujs and remote form, How to stop form submission the programmatically submit form

我正在使用 rails form_with 作为远程表单。在提交之前,我想显示一个带有动态消息的自定义确认框。确实确认框后我想最终提交表格。 我想出了 'ajax:beforeSend' 事件处理程序:

const form = document.getElementById('assign_sessions_to_employees')
form.addEventListener(
  'ajax:beforeSend',
  (event) => {
    event.preventDefault();
    swal.fire({
      title: 'Are you sure ?',
      text: `You are about to spend ${expectedExpenditure()} credits.`,
      showCancelButton: true,
    }).then((result) => {
      if (result.isConfirmed) {
        console.log('submitting')
        Rails.fire(form, 'submit');
      }
    })
  }
)

这很好用,但是当我 运行 Rails.fire(form, 'submit'); 时,当我最终想提交表单时,这个重新触发 'ajax:beforeSend' 我陷入了循环。

使用 form_with 和 rails ujs 实现此行为的正确方法是什么?

如果您使用函数声明(也称为命名函数),您可以使用 EventTarget.removeEventListener():

删除事件处理程序
function handleConfirmation(event){
  let form = event.target;
  event.preventDefault();
  swal.fire({
     title: 'Are you sure ?',
     text: `You are about to spend ${expectedExpenditure()} credits.`,
     showCancelButton: true,
  }).then((result) => {
     if (result.isConfirmed) {
       console.log('submitting');
       form.removeEventListener('ajax:beforeSend', handleConfirmation);
       Rails.fire(form, 'submit');
     }
    })
  }
}
const form = document.getElementById('assign_sessions_to_employees')
form.addEventListener('ajax:beforeSend', handleConfirmation);

另一种方法是在事件处理程序中设置一个数据属性:

const form = document.getElementById('assign_sessions_to_employees')
form.addEventListener(
  'ajax:beforeSend',
  (event) => {
    if (event.target.dataset.confirmed) { return };
    event.preventDefault();
    swal.fire({
      title: 'Are you sure ?',
      text: `You are about to spend ${expectedExpenditure()} credits.`,
      showCancelButton: true,
    }).then((result) => {
      if (result.isConfirmed) {
        console.log('submitting');
        form.dataset.confirmed = true;
        Rails.fire(form, 'submit');
      }
    })
  }
)