mithril-router

1.3.3 • Public • Published

Mithril Router

Django style router for Mithril.js

version License Build Status Downloads Code Climate Coverage Status Dependencies

Install

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 route
  • m.route(element:DOMElement): bind elements while abstracting away route mode
  • m.route(namespace|route(, parameters:Object)): programmatic redirect w/ arguments
  • m.route(namespace|route(, replaceHistory:Boolean)): programmatic redirect w/ replacing history entry
  • m.route(namespace|route(, parameters:Object, replaceHistory:Boolean)): programmatic redirect w/ arguments and replacing history entry
  • m.route(rootElement:DOMElement, routes:Object): configure app routing
  • m.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

See Mithril.route.html#mode


m.route.param()

See Mithril.route.html#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 Querystring

  • prefix: String | Boolean Mode, when true 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.

Package Sidebar

Install

npm i mithril-router

Weekly Downloads

2

Version

1.3.3

License

MIT

Last publish

Collaborators

  • nijikokun