Skip to content

Instantly share code, notes, and snippets.

@omockler
Created September 3, 2013 16:09
Show Gist options
  • Save omockler/6425976 to your computer and use it in GitHub Desktop.
Save omockler/6425976 to your computer and use it in GitHub Desktop.
Sinatra + pjax + requirejs + knockout
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
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'));
});
});
define(['knockout'], function (ko) {
return function TestViewModel () {
var self = this;
self.points = ko.observable(5);
self.addPoint = function () {
self.points(self.points() + 1);
};
};
});
@omockler
Copy link
Author

omockler commented Sep 3, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment