Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jaforbes/mithril-map-router
Map over route changes like you map over arrays!
https://github.com/jaforbes/mithril-map-router
Last synced: about 1 month ago
JSON representation
Map over route changes like you map over arrays!
- Host: GitHub
- URL: https://github.com/jaforbes/mithril-map-router
- Owner: JAForbes
- Created: 2015-11-22T05:26:19.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-27T17:02:01.000Z (over 7 years ago)
- Last Synced: 2024-10-11T06:12:53.318Z (about 1 month ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://mithril.js.org/route.html#authentication)
#### 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`.```js
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.
```js
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`](https://lhorie.github.io/mithril/mithril.route.html)
because this function just proxies to it.