mithril-map-router

Map route changes like you map arrays!

Usage no npm install needed!

<script type="module">
  import mithrilMapRouter from 'https://cdn.skypack.dev/mithril-map-router';
</script>

README

mithril-map-router

Map over route changes like you map over arrays!

Getting start

npm install mithril-map-router

Now let's give every route access to a data object for shared context. Easy with ramda's R.always.

var R = require('ramda');
var m = require('mithril')
var mapRoutes = require('mithril-map-router')(m)

/*
  Automatically parameterize every component
  with the data object.
  
*/
mapRoutes(R.always(data), container, '/' ,{
    '/': Home,
    '/access/login': Login, 
    '/access/verify': Verify, 
    '/access/invite': Invite, 
    '/access/forgot': Forgot, 
    '/access/reset': Reset, 
    '/access/signup': SignUp, 
})

Or you could do something more advanced like check if the user is logged in on route change.

var authRedirect = function(routeName){
  var token = localStorage.getItem('auth_token')
  if(!token){
    m.route('/login')
  }
}

mapRoutes(authRedirect, container, '/' ,{
    '/': Home,
    '/login': Login
})

Documentation

Initialization

You must pass in your mithril m object to initialize the router module. You'll then be given access to the mapRoutes function.

mapRoutes

Arguments: ( visitor: function, container: HTMLElement, initialRoute: string, routes: object )

Calls the visitor function every time mithril triggers a route change.
Your controller will receive the result of the visitor function as its first argument.

Works with parameterized components just fine.

The other arguments work just like m.route because this function just proxies to it.