Created
May 19, 2011 07:27
-
-
Save edison/980351 to your computer and use it in GitHub Desktop.
Pjax with ruby 1.8.7
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 lambda {|c| 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