Created
August 30, 2011 10:51
-
-
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
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
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