• Stars
    star
    532
  • Rank 80,044 (Top 2 %)
  • Language
    JavaScript
  • Created over 13 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RESTful degradable JavaScript routing using pushState

Davis.js

Description

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

Why

Using the history pustState and popstate events allows the links and forms in your app to have hrefs and actions that point to real end points on your server. This allows complex JavaScript apps to degrade gracefully when JavaScript is unavailable and combining this with a template system that can be used both client and server side allows for large amounts of code reuse.

Davis.js is heavily inspired by Sammy.js (hence the name), it is however intentionally much lighter than Sammy.js because I never use any of the template rendering etc that it includes. All Davis.js does is provide a simple routing layer, nothing more, nothing less.

Requirements

Davis.js requires jQuery 1.4.2+ as well as a modern browser that supports HTML5 history.pushState and the onpopstate event. At the moment that means FireFox 4+, Safari 5+, Chrome, iOS Safari 4+*, Android Browser 2.2+, Opera 11.50+.

In all other browsers Davis.js is currently unsupported, all links and forms will have their default behaviour. You can bind to the 'unsupported' event on an app to handle this situation in your code. Fallback to location.hash and onhashchange is a possibility in the future.

*Whilst pushState is supported in iOS it has several, fairly serious bugs. Davis will not fire the unsupported event though as technically iOS does support davis.

Installation

Download davis.min.js and include it on your page after jquery.

Example

A very simple example of a Davis.js app:

var app = Davis(function () {
  this.get('/welcome/:name', function (req) {
    $('body').append('<h1>Hello there, ' + req.params['name'] + '!</h1>')
  })
})

$(document).ready(function () {
  // append a link to trigger the route
  $('body').append('<a href="/welcome/bob">Greet</a>');
  
  app.start();
})

We create a new instance of a Davis.App using the Davis.js function, passing in a function that will draw the routes for the application. Inside this function this is the instance of our application.

We define a simple get route with a 'name' parameter and a callback that will append a message to the html body. Inside the route callback this is set to the request that matches the route, this request is also passed as a parameter to the callback.

Once the app is configure it needs to be started. You start a Davis.js app by calling the start method, this must be done once the document is ready. Now if you click on the link that we appended to the body our route should be called and a friendly greeting printed on the page.

To use Davis your html file must be loaded from a server rather than just opening the file in your browser.

More

API docs

Example using davis.js and a mustache templates shared between the client and the server, code

Contributing

Contributions are more than welcome, to make the process easier and quicker please follow these guidelines:

  • Open an issue detailing the bug fix or feature in your patch.
  • Add test cases for your code.
  • Don't change the version or build new versions as part of you patch.

Running examples

First you'll need node installed for the server. Then execute the following command and visit one of the examples: http://localhost:3000/examples/todo.html.

$ make test

Feedback

Any feedback or suggestions are welcome via issues.

More Repositories

1

lunr.js

A bit like Solr, but much smaller and not as bright
JavaScript
8,711
star
2

augment.js

Modern JavaScript in your IE
JavaScript
429
star
3

poirot

mustaches in your rails
Ruby
108
star
4

supplement.js

JavaScript
61
star
5

dox-template

simple templates for dox
JavaScript
29
star
6

moonwalkers

Demo site for Lunr2
JavaScript
26
star
7

canvas.fm

An audio visualisation using the SoundCloud API, Mozilla Audio Data API & Canvas
JavaScript
23
star
8

rejex

JavaScript
21
star
9

rackjson

Ruby
15
star
10

notepad

Example app for using davis.js with mustache templates on both server and client
JavaScript
15
star
11

weather

A D3 visualisation of the UK temperature using data from the Met Office.
JavaScript
13
star
12

tlux

Luxurious tmux configuration
Ruby
11
star
13

lunr-index-builder

A simple command line too for building lunr indexes
JavaScript
7
star
14

flipper

jquery plugin for 3d flip animation
JavaScript
6
star
15

lunr-schema

Schema for serialised Lunr indexes
Ruby
5
star
16

lunr.ru.js

A Russian language extension to lunr.js
JavaScript
5
star
17

curry.js

JavaScript
5
star
18

emberjs-requirejs-example

Example using Ember.js 1.0 (pre) with Require.js / AMD.
JavaScript
4
star
19

dotfiles

my dotfiles
Shell
4
star
20

lunr.rs

Lunr backend implemented in Rust
Rust
4
star
21

jem

Gems with a J
Ruby
3
star
22

cdn

image resizing cdn
JavaScript
3
star
23

greyhound

Fast fuzzy filepath finder
Go
3
star
24

flatshare

Ruby
3
star
25

ff

Fast fuzzy file finder
Rust
3
star
26

lunr.fr.js

French language extension for lunr.js
JavaScript
3
star
27

ShiftSubtitle

Ruby
3
star
28

builder

javascript proxy experiment
JavaScript
2
star
29

led_board

A ruby wrapper for displaying messages on an LED display board
Ruby
2
star
30

json_tree

Print JSON structure
Ruby
2
star
31

ColourPicker

JavaScript
2
star
32

ghostwriter

A pre-push hook to prevent using the wrong email when pushing to a specific remote.
Rust
2
star
33

pi_wire

A ruby wrapper around wiringPi
Ruby
2
star
34

iportfolio

A Rails application for hosting portfolios online
JavaScript
2
star
35

Jam

JavaScript
2
star
36

bitsy

Simple Bitmask in Ruby
Ruby
2
star
37

downcount

Social event count down application
Ruby
2
star
38

hx-perf-spike

Ruby
1
star
39

PhoneBook

Ruby
1
star
40

MinimalPixel

JavaScript
1
star
41

rpb

Remote pbcopy & pbpaste
Go
1
star
42

mongorack

Ruby
1
star
43

f1_calendar

F1 Calendar
Ruby
1
star
44

rails_training

Ruby
1
star
45

cdn-test

sample rails app client for cdn
Ruby
1
star
46

goliath_mysql2_errors

This project demonstrates the mysql problems we are having with goliath
Ruby
1
star
47

cranfield

Parsers for the Cranfield Collection test corpus
Ruby
1
star
48

tracker

Simple time tracking tool for batman
Ruby
1
star
49

bump

A simple tool for managing project version files.
Rust
1
star
50

BBC

Simple ruby library to access the BBC Apis
Ruby
1
star
51

bamboo_gif

bamboo bot gif end point
Ruby
1
star
52

blockbuster

A Visualisation of Hollywood Blockbuster Films
JavaScript
1
star
53

rack_push

Ruby
1
star