Rails 5.1:如何覆盖 rails-ujs 中的 allowAction 以使用自定义确认对话框

Rails 5.1: How to override allowAction in rails-ujs to use a custom confirmation dialog

在使用 jquery_ujs 的 Rails 5.1 之前的版本中,我们可以通过覆盖 $.rails.allowAction 将浏览器的确认弹出窗口替换为我们自己的,如 here 所述.

从使用 rails-ujs 的 Rails 5.1+ 开始,$.rails.allowAction 不再可用。我们如何在 Rails 5 中用我们自己的默认确认覆盖 Rails' 而不必切换回 jquery_ujs

提前致谢。

我还没有找到调整 rails_ujs 的好方法,所以我想出了这个解决方法(使用 CoffeeScript):

```

$(document).on 'mousedown', 'a[data-confirm]', (e) ->
  e.preventDefault()
  link = $(e.target)

  message = link.data 'confirm'
  modal = $('.modal.confirmation')
  modal.find('.content').text(message)
  approve = modal.find('.approve')
  approve.attr('data-method', link.data('method'))
  approve.attr('href', link.attr('href'))

  modal.modal('show')

```

Mousedown 事件允许我的事件处理程序首先执行(它在 rails_ujs 使用的单击事件之前执行)

我遇到了同样的挑战,我对此进行了更多的研究。我在此过程中发现的内容可以在这里阅读:https://medium.com/store2be-tech/how-to-use-sweetalert2-for-your-rails-5-1-rails-ujs-confirms-without-jquery-8a5b516b2a1

这里是最终的解决方案:

(function() {
  var handleConfirm = function(element) {
    if (!allowAction(this)) {
      Rails.stopEverything(element)
    }
  }

  var allowAction = function(element) {
    if (element.getAttribute('data-confirm-swal') === null) {
      return true
    }

    showConfirmationDialog(element)
    return false
  }

  // Display the confirmation dialog
  var showConfirmationDialog = function(element) {
    var message = element.getAttribute('data-confirm-swal')
    var text = element.getAttribute('data-text')

    swal({
      title: message || 'Are you sure?',
      text: text || '',
      type: 'warning',
      showCancelButton: true,
      confirmButtonText: 'Yes',
      cancelButtonText: 'Cancel',
    }).then(function(result) {
      confirmed(element, result)
    })
  }

  var confirmed = function(element, result) {
    if (result.value) {
      // User clicked confirm button
      element.removeAttribute('data-confirm-swal')
      element.click()
    }
  }

  // Hook the event before the other rails events so it works togeter
  // with `method: :delete`.
  // See https://github.com/rails/rails/blob/master/actionview/app/assets/javascripts/rails-ujs/start.coffee#L69
  document.addEventListener('rails:attachBindings', function(e) {
    Rails.delegate(document, 'a[data-confirm-swal]', 'click', handleConfirm)
  })

}).call(this)

您可以使用 Rails.confirm 覆盖它,例如使用 CoffeeScript:

Rails.confirm = (message, element) ->
  # your code

例如让确认文本显示 2 秒:

WAITING_CLASS = "waiting-for-confirmation"
TIMEOUT = 2000

Rails.confirm = (message, element) ->
  if element.classList.contains(WAITING_CLASS)
    true
  else
    element.dataset.beforeConfirm = element.textContent
    element.textContent = element.dataset.confirm
    element.classList.add(WAITING_CLASS)

    timeout TIMEOUT, ->
      element.classList.remove(WAITING_CLASS)
      element.textContent = element.dataset.beforeConfirm

    false

参见:https://github.com/rails/rails/blob/master/actionview/app/assets/javascripts/rails-ujs/features/confirm.coffee

timeout以及一个简单的函数,它反转了setTimeout参数:

var timeout = function(time, callback) {
  setTimeout(callback, time)
}