Transit is a simple router, inspired by page.js
, but made to my preference. It also has hash-based fallbacks because most of us still need to support IE 9.
Install with component(1)
$ component install bmcmahen/transit
Or Duo
var transit = require('bmcmahen/transit');
Begin listening for URL changes. Typically you will call this after specifying all of your routes.
Active click delegation. If the path is specified in your router it will push and execute the URL and its functions.
transit.listen();
transit('/bacon', getBacon);
<a href='/bacon'>Get Bacon</a>
When Get Bacon is clicked, the getBacon function will run.
Update the URL and add it to the history without executing the specified callbacks.
transit.push('/bacon');
Execute the specified callbacks for the provided URL without manipulating the URL or adding it to the history.
transit.exec('/bacon');
Call both #push
and #exec
for the specified url.
transit.go('/bacon');
Alternative, use the shorthand:
transit('/bacon');
var route = require('transit');
function allRoutes(ctx, next){
console.log('I run on all routes');
ctx.bacon = 'tasty';
next();
}
function myname(ctx, next){
ctx.name = ctx.params.first || 'foo';
console.log('bacon is', ctx.bacon || 'not tasty');
next();
}
function lastname(ctx, next){
var lastname = ctx.params.last || 'bar';
console.log(ctx.name +' '+ lastname);
}
function after(ctx, next){
console.log('I run when the route is left');
}
route('*', allRoutes);
route('/name', myname, lastname);
route('/name/:first', myname, lastname);
route('/name/:first/:last', myname, lastname).out(after);
route.listen();
route.start();
- TESTS
$ npm install duo-test -g
$ make test-browser
MIT