ajax:success and ajax:complete callback doesn't work when using UJS in Rails

后端 未结 4 613
醉梦人生
醉梦人生 2021-01-13 15:43

I have a link that when i click on it will trigger an ajax call then replace this link by another link, says for example the original link is \"add friend\", when i click th

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 16:20

    I had the same issue, and the problem is not with ajax:success or ajax:complete callback. The problem is that when displaying the loading gif image, you replace the content of the container, including the link itself. So the element on which you apply the success, complete callback is not part of the DOM anymore, that's why the callback is not fired.

    One solution would be to hide the element and append the loading image, like following:

    $('#my_link').on "ajax:send", (event, xhr, settings) ->
            $('#my_link').hide()
            $("#my_link_container").append("")
    

    Then the success and complete callback will be fired. Hope it helps somebody :)

提交回复
热议问题