base-router
A simple and portable router for the client and server.
example
var createRouter = // Create a router that will resolve datavar router = // React to transition eventsrouter // Manually trigger transitioning to routesrouter
Specifying Routes
This library uses the module routington to specify and match routes. Please see their docs for route matching.
Some common examples are:
/bears
will match/bears
/bears/:type
will match/bears/grizzly
/bears/:type?
will match/bears
or/bears/grizzly
Resolving Data
You can resolve data using return
, callback
or promise
:
var router =
A loading
event is emitted indicating the route is transitioning but has not
yet finished resolving. Only when the route has resolved will transition
be
called.
virtual-dom
Example withvar createRouter = var h = var xhr = var router = router
api
var router = new BaseRouter([routes, options])
Creates a new instance of base-router
.
routes
- An object literal of routes to create.options
- An object literal to configure:location
- Whether to manage thewindow.location
. Ifwindow.history.pushState
is available it will use that otherwise it will usewindow.location.hash
. Set tofalse
to disable,hash
to force using hashes, andhistory
to force using push state.
router.route(name, model)
Adds a new route. name
is the pathname to our route and model
is a function
that resolves the data for the route.
router
router.transitionTo(name[, params, callback])
Transitions to the given route name
.
Optionally you can supply params
to override the params given to a route.
Optionally you can supply a callback
which will be called instead of using the
transition
and error
events.
router.currentRoute
The last resolved route we are currently on.
Events
.on('transition', function (name, data) {})
When a transition has resolved. Gives the name
of the route and the data
that has been resolved by the model.
.on('loading', function (name, abort) {})
Indicates the desire to transition into a route with name
but model has not
yet resolved.
Call abort()
to abort the transition.
.on('error', function (name, err) {})
When a transition has errored. Gives the name
of the route and the err
that has been either thrown, first argument of callback or rejected by the
promise.
license
(c) 2015 Kyle Robinson Young. MIT License