暂时禁用 Rails 远程链接

Gra*_*eme 2 jquery ruby-on-rails

我有一个切换开关,当服务器将真/假数据发送到数据库时,我需要禁用远程链接。然后我将重新启用链接ajax:success

e.preventDefault()我无法在 Rails 远程链接上使用

我希望能够做这样的事情:

$('body').on('click', '.toggle_link', function(){
  //disable remote link
})

$('body').on('ajax:success', '.toggle_link', function(){
  //enable remote link
})
Run Code Online (Sandbox Code Playgroud)

是否有实现此目标的最佳实践?

Joh*_*gle 5

您还可以连接到ajax:beforeSendujs 中的事件。

  $(document).on('ajax:beforeSend', 'a[data-remote=true]', function(event, xhr, status, error) {
    // stash the link
    // remove the href
  });
Run Code Online (Sandbox Code Playgroud)

我使用了它的变体来防止数据远程链接在 CSS 类为“禁用”时运行

  $(document).on('ajax:beforeSend', 'a.disabled[data-remote=true]', function(event, xhr, status, error) {
    event.preventDefault();
    event.stopImmediatePropagation();
    return false;
  });
Run Code Online (Sandbox Code Playgroud)