0.1.5 • Published 10 years ago

tiny-hash-router v0.1.5

Weekly downloads
4
License
ISC
Repository
github
Last release
10 years ago

HashRouter.js

Super simple URL dispatcher for single page apps.

Why?

I wrote some URL dispatchers for server-side apps in the past. When I started writing single-page apps (with my API-server) I needed a simple way of registering URL routes (to sort-of preserve state in the application after refresh).

All the routes are attached in the hash (document.location.hash).

Installation

npm/webpack

npm install --save tiny-hash-router

Bower

bower install --save HashRouter

How?

var hRouter = new HashRouter.Router;
hRoute.route("foo/bar/:page", function(page) {
  console.error("foo/bar on page", page);
}).name("my_route").setDefault("page", 1);

hRoute.route("list/:table/:page", function(table, page) {
});

hRoute.addFilter('page', function(page) {
    return parseInt(page) >= 1;
});

hRoute.registerListener(); // do the initial route.

console.error(hRoute.url("my_page", 99));

Or you can use it directly with WebPack.

import Router from 'tiny-hash-router'

let hRoute = new Router;
hRoute.route("foo/bar/:page", function(page) {
  console.error("foo/bar on page", page);
}).name("my_route").setDefault("page", 1);

hRoute.route("list/:table/:page", function(table, page) {
});

hRoute.addFilter('page', function(page) {
    return parseInt(page) >= 1;
});

hRoute.registerListener(); // do the initial route.

console.error(hRoute.url("my_page", 99));

The library will listen to any change in the document.location.hash, and any change will trigger the newer action.

Disclaimer

  1. This is a work in progress, and things will be added
  2. Things to add in the near future:
  3. Catch-all routes
  4. Events
    1. Pre-routes
    2. Post-routes
    3. Clean up (after we "leave" a given URL).
  5. Better support for RegExp
0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago