Skip to content

gschambers/router-rx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

router-rx Build Status Dependencies Status npm version

A simple application router built around reactive principles.

Note: router-rx currently only implements browser-based routing. Node.js routing to follow.

Installation

npm install router-rx

Usage

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();

API

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.

TODO

  • Nested routers
  • Node.js routing

License

MIT

About

A simple application router with Rx

Resources

Stars

Watchers

Forks

Packages

No packages published