jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 124 forks source link

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

Open patrickquigley102 opened 7 years ago

patrickquigley102 commented 7 years ago

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 commented 7 years ago

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

patrickquigley102 commented 7 years ago

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();
  });

});`