filterrific icon indicating copy to clipboard operation
filterrific copied to clipboard

Filterrific form loaded via AJAX isn't bound to event listeners

Open patrickquigley102 opened this issue 7 years ago • 2 comments

Hello.

This gem is lovely, so first off thank you for making it.

Rails 4.2.

As the title says, for my application at least, I found that if the filterrific form is loaded via AJAX then the event listeners do not bind to the newly inserted content. The following javascript fixes this for me.

// Initialize event observers on AJAX complete event // Gem only inits on turbolinks load or document ready by default. jQuery(document).ajaxComplete(function() { Filterrific.init(); });

This is my first time attempting to contribute to a github repository so apologies for, no doubt, doing it incorrectly. Thanks for your time. Patrick.

patrickquigley102 avatar May 26 '17 09:05 patrickquigley102

My mistake. Rookie mistake. This js snippet results in far too many instances of Filterrific.init, one per ajaxComplete event.

patrickquigley102 avatar May 26 '17 09:05 patrickquigley102

So I have found a solution, an ugly solution.

I have copied to submission javascript from the source, and bound it to the relevant element in my filterrific form.

`

$(document).on('change', '#filterrific_with_deleted_at', function(){

  var form = $(this).parents("form"),
      url = form.attr("action");
  // turn on spinner
  $('.filterrific_spinner').show();
  // Submit ajax request
  $.ajax({
    url: url,
    data: form.serialize(),
    type: 'GET',
    dataType: 'script'
  }).done(function( msg ) {
    $('.filterrific_spinner').hide();
  });

});`

patrickquigley102 avatar May 26 '17 11:05 patrickquigley102