jQuery.LazyJaxDavis

Davis.js

jQuery.LazyJaxDavis needs Davis.js to work.
If you want to use jQuery.LazyJaxDavis, you need to load Davis.js too.

What is Davis.js?

Davis.js's website says

Davis.js is a small JavaScript library using HTML5 history.pushState that allows simple Sinatra style routing for your JavaScript apps.

Generally, Davis.js was made for the web apps using history API. With this library, you can make URL routings for web apps easily. Backbone.js's Router provides similar API.

How to use Davis.js

With Davis.js, you can create URL routings like below.

$(function(){

  var app = new Davis(function () {
    this.get('/', function () {
      // do something when url was changed to '/'
    })
    this.get('/user/:name', function (req) {
      // do something when url was changed to '/user/*'
      var name = req.params['name']; // you can get the value of ':name' in url
    })
    this.get('/help', function (req) {
      // do something when url was changed to '/help'
    })
    this.post('/update', function (req) {
      // do something when url was changed to '/update'
    })
  })
  app.bind('routeNotFound', function(req){
    // if the url was not routed, this event handler will be fired.
    alert('this url was not routed!');
  });

});

This helps you to create url based JavaScript based web apps.
Learn more about Davis.js on the Davis.js's website.

Davis'js and jQuery.LazyJaxDavis

jQuery.LazyJaxDavis uses Davis.js in it.
In web apps, you almost need to assign what to do to URLs. For example, if the URL was '/help', show help page - or - if the URL was '/user/foobar', show the page of the user 'foobar' - or something like that.

This is very useful to develop web apps. But, imagine that you create your own blog. It is ok if you have a few posts on it. But, generally you will have many many posts on your blog. So it is almost impossible to handle all urls of the pages you created.

So, I focused on the routeNotFound event of Davis.js. This event is fired when the requested page was not routed. jQuery.LazyJaxDavis does ajax to the requested page even if it was unrouted.

Use Davis.js's routing with jQuery.LazyJaxDavis

JQuery.LazyJaxDavis has its own URL routing features as I wrote on "URL routing". But, if you want to use Davis.js's routing directly, you can use it like below.

var lazyJaxDavis = new $.LazyJaxDavis(function(router){

  ...

  router.routeDavis(function(){
    this.get('/', function () {
      // do something when url was changed to '/'
    })
    this.get('/user/:name', function (req) {
      // do something when url was changed to '/user/*'
      var name = req.params['name']; // you can get the value of ':name' in url
    })
    this.get('/help', function (req) {
      // do something when url was changed to '/help'
    })
    this.post('/update', function (req) {
      // do something when url was changed to '/update'
    })
  });

});

console.log(lazyJaxDavis.davis); // davis's instance here

You can pass the initializer to Davis with routeDavis method. To learn more, see the demo page below.

Fork me on GitHub