Davis.js is a small JavaScript library using HTML5 history.pushState that allows simple Sinatra style routing for your JavaScript apps.
The benefit from using HTML5 pushState is that all of your links and forms can point to real end points on your server, so that if JavaScript is unavailable, or it breaks, your server can still respond to these links and forms. When JavaScript and pushState are available your users will get the full experience.
-
Define your app:
var app = Davis(function () {
this.get('/welcome/:name', function (req) {
alert("Hello " + req.params['name'])
})
})
app.start()
-
Set up some links:
<a href="/welcome/oliver">greet oliver</a>
<a href="/welcome/bob">greet bob</a>
The above will set up an app that will greet people by name. There are two links for greeting Bob and Oliver. Clicking on these links would run the route in our app and greet both Bob and Oliver by name.
Davis.js has a dependency on jQuery. It will work perfectly in all modern browsers that support pushState: currently that means FireFox 4+, Safari 5+, Chrome, iOS Safari 4+, Android Browser 2.2+. In older browsers Davis.js will let you know that it is unsupported and then get out of your way, allowing all links and forms to have their default behaviour.
Alternatively you can clone the whole project from github using git:
$ git clone git://github.com/olivernn/davis.js