hrsetyono
hrsetyono

Reputation: 4464

Rails 4 - Check if user click "No" when prompted using data-confirm

I have Ajax Delete button using Jquery UJS

<%= link_to "Delete Product", @product, method: :delete,
      data: { confirm: "You can't undo this. Are you sure?",
              remote: true } %>

When user clicked the link, it will add is-loading class in it

$("a[data-remote='true']").on("click", function(){
  $(this).addClass("is-loading");
});

When success, it will remove the products.

The problem is, when the user click No in the prompt (caused by data-confirm), the .is-loading class is already applied.

Is there any callback to check what user choose on data-confirm?

Thanks

[EDIT]

ANSWERS by ryaz

So ryaz's answer is correct, but too short. So for other's reference, here's my code after applying his answer:

$("a[data-remote='true']").on("ajax:beforeSend", function(){
  $(this).addClass("is-loading");
});

This will add is-loading class if AJAX event started, so if the user click No, it won't add the loading class.

Upvotes: 0

Views: 81

Answers (1)

ryaz
ryaz

Reputation: 336

You can use ajax:beforeSend and ajax:complete event hooks.

Upvotes: 1

Related Questions