forked from rails/pjax_rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pjax.rb
38 lines (32 loc) · 1.08 KB
/
pjax.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Pjax
extend ActiveSupport::Concern
included do
before_filter lambda {
if pjax_request?
self.instance_eval do
@old_layout = self.send("_layout")
end
self.class.send(:layout, false)
end
}
after_filter lambda {self.class.send(:layout, self.instance_variable_get(:@old_layout)) if pjax_request?}
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 {
$('[data-pjax-container]').html(#{render_to_string("#{action}.html.erb").to_json});
$(document).trigger('end.pjax');
var title = $.trim($('[data-pjax-container]').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