Skip to content

JAForbes/mithril-map-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

mithril-map-router

Map over route changes like you map over arrays!

WARNING: This library only works with mithril's 0.2.x API. If you are using 1.0 take a look at the authentication section of the docs

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.

About

Map over route changes like you map over arrays!

Resources

Stars

Watchers

Forks

Packages