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
912 stars 125 forks source link

Add History.pushstate() to update the url #79

Open FrankHald opened 8 years ago

FrankHald commented 8 years ago

So that its possible to bookmark the url or copy-paste it to recreate the same search criteria

FrankHald commented 8 years ago

Addind the following in the application.js does the job:

var ready = function(){
    $("#filterrific_filter").click(function(){
      $.get($("#filterrific_filter").attr("action"), $("#filterrific_filter").serialize(), null, "script");
      history.pushState(null, document.title, $("#filterrific_filter").attr("action") + "?" + $("#filterrific_filter").serialize());
      e.preventDefault();
    });
    $(window).bind("popstate", function() {
      $.getScript(location.href);
    });
};

$(document).ready(ready);
$(document).on('page:load', ready);
maaris commented 7 years ago

@FrankHald tnx for sharing this snippet!

apanzerj commented 7 years ago

Just a few questions:

  1. What is the purpose of this line:
$.get($("#filterrific_filter").attr("action"), $("#filterrific_filter").serialize(), null, "script");

You are making a request and discarding the response as far as I can tell?

  1. Why do you need to prevent default here?