Created
September 3, 2013 16:09
-
-
Save omockler/6425976 to your computer and use it in GitHub Desktop.
Sinatra + pjax + requirejs + knockout
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 :index, :layout => !request.pjax? | |
end | |
get '/next' do | |
haml :next, :layout => !request.pjax? | |
end | |
__END__ | |
@@layout | |
!!! 5 | |
%html | |
%head | |
%script{src: "//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.8/require.min.js", data: {main: "js/main.js"}} | |
%body | |
%h1 Hello pjax | |
#pjax-container.container | |
= yield | |
@@index | |
Go to | |
%a{href: '/next'} next page. | |
@@next | |
%h1 WELCOME TO THE NEXT LEVEL | |
.module-test{data: {bind: "module: 'testModule'"}} | |
%span{data:{bind:"text: points"}} | |
%button{data:{bind:"click: addPoint"}} Add Points | |
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.config({ | |
paths: { | |
'jquery': 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min', | |
'knockout': 'http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.0', | |
'knockout-amd': 'https://raw.github.com/rniemeyer/knockout-amd-helpers/master/build/knockout-amd-helpers.min', | |
'pjax': 'http://pjax.heroku.com/jquery.pjax', | |
}, | |
shim: { | |
'pjax': ['jquery'] | |
} | |
}); | |
require([ | |
'knockout', | |
'jquery', | |
'knockout-amd', | |
'pjax' | |
], function(ko, $) { | |
ko.applyBindings({}); | |
$(document).pjax('a', '#pjax-container'); | |
$(document).on('pjax:end', function () { | |
ko.applyBindings({}, document.getElementById('pjax-container')); | |
}); | |
}); |
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
define(['knockout'], function (ko) { | |
return function TestViewModel () { | |
var self = this; | |
self.points = ko.observable(5); | |
self.addPoint = function () { | |
self.points(self.points() + 1); | |
}; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure why it is insisting on using tabs.
If you try to run this, the files should be structured as
/app
--app.rb
--public/js
----main.js
----testModule.js