Created
May 11, 2011 01:33
-
-
Save dhh/965746 to your computer and use it in GitHub Desktop.
Pjax helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pjax | |
extend ActiveSupport::Concern | |
included do | |
layout ->(c) { pjax_request? ? false : 'application' } | |
end | |
private | |
def redirect_pjax_to(action, url = nil) | |
new_url = url_for(url ? url : { action: action }) | |
render js: <<-EJS | |
if (!window.history || !window.history.pushState) { | |
window.location.href = '#{new_url}'; | |
} else { | |
$('div.pages').html(#{render_to_string("#{action}.html.erb").to_json}); | |
$(document).trigger('end.pjax'); | |
var title = $.trim($('div.pages').find('title').remove().text()); | |
if (title) document.title = title; | |
window.history.pushState({}, document.title, '#{new_url}'); | |
} | |
EJS | |
end | |
def pjax_request? | |
env['HTTP_X_PJAX'].present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment