Skip to content

Instantly share code, notes, and snippets.

@udzura
Created August 30, 2011 10:51
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 udzura/1180652 to your computer and use it in GitHub Desktop.
Save udzura/1180652 to your computer and use it in GitHub Desktop.
pjax sample. try: wget http://l.udr.me/qpuY4c -O pjax-sinatra.rb && ruby pjax-sinatra.rb
require 'sinatra'
require 'haml'
class Sinatra::Request
def pjax?
env['HTTP_X_PJAX'] || self["_pjax"]
end
end
get '/' do
haml "%p this is index...", :layout => !request.pjax?
end
get '/akubi' do
haml "%img{src: 'http://udzura.jp/images/150709359.jpg'}", :layout => !request.pjax?
end
get '/:name' do
haml <<"EOT", :layout => !request.pjax?
%p Hello, #{params[:name]}!
%p= Time.now.strftime("You loaded this part at %Y/%m/%d %H:%M:%S.")
EOT
end
__END__
@@layout
!!! 5
%html
%head
%title sample
%script{type: 'text/javascript', src: "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"}
%script{type: 'text/javascript', src: "http://pjax.heroku.com/jquery.pjax.js"}
%body
%div{style: "width: 20%; border: solid; padding: 1em; float:left;"}
%p= Time.now.strftime("You loaded a whole page at %Y/%m/%d %H:%M:%S.")
%ul
%li
%a{href: "/", class: "pjax"} Home
%li
%a{href: "/akubi", class: "pjax"} Akubi
%li
%a{href: "/Hoge", class: "pjax"} Hoge
%li
%a{href: "/Fuga", class: "pjax"} Fuga
%div{style: "width: 40%; border: solid; padding: 1em; float: left; margin-left: 1em;", id: "target"}
= yield
%script $("a.pjax").pjax("#target");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment