正在发生什么?
- 表单已经提交
- rails-ujs禁用按钮(使用
data-disable-with
功能)
- 表单请求成功
- rails-ujs启用按钮
- turbolinks-rails向重定向位置发出请求(可能是缓慢的请求,导致按钮处于启用状态)
解决方案
我们需要在第4步之后重新禁用按钮。为此,我们将监听ajax:success
事件,并使用setTimeout
禁用它。这确保了它会在Rails完成其操作后被禁用。(您可以使用requestAnimationFrame
代替setTimeout
,但它的支持不如后者广泛)。
为了防止按钮被缓存为禁用状态,我们将在它被缓存之前重新启用它。(注意使用one
而不是on
以防止before-cache处理程序执行多次。)
我注意到您正在使用jQuery和jquery-ujs,因此我将在下面的代码中使用这些库中的函数。将此包含在您的主JavaScript文件中的某个位置。
jquery-ujs
;(function () {
var $doc = $(document)
$doc.on('submit', 'form[data-remote=true]', function () {
var $form = $(this)
var $button = $form.find('[data-disable-with]')
if (!$button.length) return
$form.on('ajax:complete', function () {
setTimeout(function () {
$.rails.disableFormElement($button)
}, 0)
})
$doc.one('turbolinks:before-cache', function () {
$.rails.enableFormElement($button)
})
})
})()
Rails-ujs / jQuery.
;(function () {
var $doc = $(document)
$doc.on('ajax:send', 'form[data-remote=true]', function () {
var $form = $(this)
var $button = $form.find('[data-disable-with]')
if (!$button.length) return
$form.on('ajax:complete', function () {
setTimeout(function () {
$button.each(function () { Rails.disableElement(this) })
}, 0)
})
$doc.one('turbolinks:before-cache', function () {
$button.each(function () { Rails.enableElement(this) })
})
})
})()
rails-ujs / vanilla JS
->
rails-ujs / 原生JS
Rails.delegate(document, 'form[data-remote=true]', 'ajax:send', function (event) {
var form = event.target
var buttons = form.querySelectorAll('[data-disable-with]')
if (!buttons.length) return
function disableButtons () {
buttons.forEach(function (button) { Rails.disableElement(button) })
}
function enableButtons () {
buttons.forEach(function (button) { Rails.enableElement(button) })
}
function beforeCache () {
enableButtons()
document.removeEventListener('turbolinks:before-cache', beforeCache)
}
form.addEventListener('ajax:complete', function () {
setTimeout(disableButtons, 0)
})
document.addEventListener('turbolinks:before-cache', beforeCache)
})
请注意,这将禁用所有带有 data-disable-with
按钮的 data-remote
表单直到下一次页面加载。您可能希望更改 jQuery 选择器,仅将此行为添加到所选表单中。
希望对您有所帮助!
data-remote=true
属性,同时在按钮上使用了data-disable-with
属性,从代码角度来看,我认为这已经足够了。只是缺少一个明确的问题,我希望我已经帮助澄清了。 - Dom Christie