mithril-router
v1.3.3
Published
Django style router for Mithril.js
Downloads
19
Maintainers
Readme
Mithril Router
Django style router for Mithril.js
Install
- Download the latest package
- NPM:
npm install mithril-router
Usage
Node.js / Browserify
// Include mithril
var m = require('mithril')
// Pass mithril to the router.
// Only required to overload once, subsequent overloads will
// return the same instance
require('mithril-router')(m)
Browser
<script src="path/to/mithril.js" type="text/javascript"></script>
<script src="path/to/mithril.router.js" type="text/javascript"></script>
Documentation
m.route()
Router allowing creation of Single-Page-Applications (SPA) with a DRY mechanism (identification classified as namespaces) to prevent hard-coded URLs.
m.route()
: returns current routem.route(element:DOMElement)
: bind elements while abstracting away route modem.route(namespace|route(, parameters:Object))
: programmatic redirect w/ argumentsm.route(namespace|route(, replaceHistory:Boolean))
: programmatic redirect w/ replacing history entrym.route(namespace|route(, parameters:Object, replaceHistory:Boolean))
: programmatic redirect w/ arguments and replacing history entrym.route(rootElement:DOMElement, routes:Object)
: configure app routingm.route(rootElement:DOMElement, rootRoute:String, routes:Object)
: configure app routing (mithril default router style)
Configure Routing
To define routing specify a host DOM element, and routes with a root route. Should no root route be specified, the first route is chosen.
New
m.route(document.body, {
"/": { controller: home, namespace: "index", root: true },
"/login": { controller: login, namespace: "login" },
"/dashboard": { controller: dashboard, namespace: "dashboard" }
})
Classic
m.route(document.body, "/", {
"/": { controller: home, namespace: "index" },
"/login": { controller: login, namespace: "login" },
"/dashboard": { controller: dashboard, namespace: "dashboard" }
})
m.route.mode
m.route.param()
m.redirect()
Redirect user to specified route, or route namespace with given arguments.
Sugar for m.route(namespace|path(, args))
m.reverse()
Generate path using specified identifier (route namespace) and path arguments.
Api
m.reverse(namespace(, options))
: takes specified route namespace and options and generates path.
Options
params
: Object Route parameters, named and non-named.query
: String | Object Querystringprefix
: String | Boolean Mode, whentrue
prepends the mode char to the route, when defined as a string the string is prepended instead.Useful for when you are not using
config: m.route
Examples
// user => /user/
m.reverse('user')
// user => /user/:id => /user/23
m.reverse('user', { params: { id: 23 }})
// user => /user/:id => /user/23?include=profile
m.reverse('user', { params: { id: 23 }, query: { include: 'profile' }})
// user => /user/:id => #/user/23?include=profile
m.route.mode = 'hash'
m.reverse('user', { prefix: true, params: { id: 23 }, query: { include: 'profile' }})
// user => /user/:id => /api/user/23?include=profile
m.reverse('user', { prefix: '/api', params: { id: 23 }, query: { include: 'profile' }})
License
Licensed under The MIT License.