A simple application router built around reactive principles.
Note: router-rx currently only implements browser-based routing. Node.js routing to follow.
npm install router-rx
import { createRouter, redirect } from "router-rx";
import { Disposable } from "rx";
const handler = function(id) {
// Do something
// Optional. Disposed on route change or tear down
return Disposable.create(function() {
// ...
});
};
const router = createRouter({
"/": redirect("/foo"),
"/foo": handler,
"/foo/:id": handler
});
// Tear down when finished
router.dispose();Disposable createRouter( Object<String, Function> );
Create a new router, mapping path strings to handler functions. Handler functions can optionally return an instance of Rx.Disposable, which will be automatically disposed when the route is changed or the containing router is disposed.
Function redirect( String path, Boolean invoke );
Create a handler function that redirects to a specified path. If invoke is true, the handler is called immediately.
void useHistory( Boolean value );
Enable/disable use of the HTML5 History API. router-rx uses hashChange by default and will only attempt to use the History API if it is supported.
- Nested routers
- Node.js routing
MIT