Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

temporarily disabling rails remote link

I have a toggle where I need to disable the remote link while the server sends the true/false data to the database. I will then re-enable the link on ajax:success.

I cant use e.preventDefault() on a rails remote link

I want to be able to do something like this:

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

$('body').on('ajax:success', '.toggle_link', function(){
  //enable remote link
})

Is there a best practice for accomplishing this?

like image 396
Graeme Avatar asked Jan 22 '26 10:01

Graeme


1 Answers

You can also wire into the ajax:beforeSend event in ujs.

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

I used a varient of this to prevent data-remote links from running when they had a css class of 'disabled'

  $(document).on('ajax:beforeSend', 'a.disabled[data-remote=true]', function(event, xhr, status, error) {
    event.preventDefault();
    event.stopImmediatePropagation();
    return false;
  });
like image 190
John Naegle Avatar answered Jan 24 '26 02:01

John Naegle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!