Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Forked from moro/gist:1024620
Created December 13, 2011 12:29
Show Gist options
  • Select an option

  • Save seyhunak/1471973 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
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
Copy Markdown

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

@cserb

cserb commented May 13, 2013

Copy link
Copy Markdown

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

@renevall

Copy link
Copy Markdown

For some reason it only works the first time

@renevall

Copy link
Copy Markdown

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