user664833
user664833

Reputation: 19505

How to execute Javascript after Rails' remote form `disable_with` functionality has completed?

I am using disable_with, and cannot get the button text change to persist (because it is being overwritten by the disable_with functionality). As soon as I remove , data: { disable_with: '...' the buttons are updated as expected.

Is there a way to perform actions after the disable_with functionality has completed?

form:

<%= form_for @user, html: {id: 'follow-form'}, remote: true do |f| %>
    <%= button_tag id: 'follow-btn', data: { disable_with: '...' } do %>
        Follow
    <% end %>
<% end %>

js:

$('#follow-form').on('ajax:success',function(data, status, xhr){
    if (status.action == 'follow') {
        $('#follow-btn').text('Unfollow');
    } else {
        $('#follow-btn').text('Follow');
    }
}

Upvotes: 0

Views: 627

Answers (1)

sites
sites

Reputation: 21795

Maybe without disable_with and:

$('#follow-form').on('ajax:send',function(data, status, xhr){
    $('#follow-btn').text('Please wait...').attr({disabled: true});
});

And when AJAX request succeeds:

$('#follow-form').on('ajax:success',function(data, status, xhr){
    if (status.action == 'follow') {
        $('#follow-btn').text('Unfollow').attr({disabled: false});
    } else {
        $('#follow-btn').text('Follow').attr({disabled: false});
    }
});

Upvotes: 1

Related Questions