Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Forked from moro/gist:1024620
Created December 13, 2011 12:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seyhunak/1471973 to your computer and use it in GitHub Desktop.
Save seyhunak/1471973 to your computer and use it in GitHub Desktop.
Kaminari and pushState
(function($){
$('.pagination a[data-remote=true]').live('ajax:success', function(e){ window.history.pushState('', '', $(e.target).attr('href')) })
$(window).bind('popstate', function(){ $.ajax({url:window.location, dataType:'script'}) ; return true });
})(jQuery);
@seyhunak
Copy link
Author

Coffeescript version;

(($) ->
  $(".pagination a[data-remote=true]").live "ajax:success", (e) ->
    window.history.pushState "", "", $(e.target).attr("href")

  $(window).bind "popstate", ->
    $.ajax
      url: window.location
      dataType: "script"

    true
) jQuery

@nishantmodak
Copy link

Why does this generate random numbers in query strings. How to avoid that?

@cserb
Copy link

cserb commented May 13, 2013

This doesn't work for me. "ajax:success" ist not triggered. So I changed it to the "click" event for now

@renevall
Copy link

For some reason it only works the first time

@renevall
Copy link

Ok I finally have a code up to date on this code

jQuery ->
  $(document).on "click",'.pagination a[data-remote=true]', (e) ->
    history.pushState {}, '', $(@).attr('href')

.live is deprecated, .click stops working after first click, since according to kaminari example, they use .html() function which to keep it simple, destroy the event handler. By using on() like this, you recreate the bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment