【发布时间】:2014-08-16 03:01:26
【问题描述】:
我一直在使用 Rails 4 和 UJS 并注意到 Rails 4.1 的行为(特别是 rails-ujs)不像我预期的那样。
当使用带有确认对话框的远程链接时,UJS 驱动程序会在确认弹出窗口被批准之前发送 XHR(使确认弹出窗口毫无意义)。我本来希望 Rails 能够处理这种选项组合,但我必须编写自己的 javascript 来执行确认弹出窗口并挂钩“ajax:beforeSend”事件以防止请求过早触发。
这是我没有工作的(请求立即触发,没有等待确认对话框返回 true):
= link_to 'Ajax Button', app_path(resource), method: :delete,
remote: true, id: 'ajax_link', disable_with: 'Wait...',
confirm: 'Are you sure?'
这就是我现在所拥有的确实有效的东西:
= link_to 'Ajax Button', app_path(resource), method: :delete,
remote: true, id: 'ajax_link', disable_with: 'Wait...'
$ ->
$("a[data-remote]#ajax_link").on 'ajax:beforeSend', (xhr, settings) ->
return confirm("Are you sure?")
只是我还是不应该 rails/jquery-ujs 处理上述选项组合并等待确认弹出窗口?在过去,Rails 3 和之前的版本中,远程链接/按钮在处理 AJAX 和远程请求时通常“正常工作”,但在 Rails 4 中,使用 AJAX 似乎需要更多以前需要的技巧。
有其他人在使用 Rails 4.1 时遇到过这种情况吗?还有其他解决方案吗?
【问题讨论】:
-
试试
= link_to 'Ajax Button', app_path(resource), method: :delete, remote: true, id: 'ajax_link', data: { disable_with: "Wait..." }, data: { confirm: 'Are you sure?' }。如果可行,我会将其作为答案发布。 -
谢谢@pavan,这绝对是一个想法。我使用的语法在技术上已被弃用,因此我将试一试您的解决方案,看看它是否能按我预期的方式工作。
标签: ruby-on-rails-4 ujs