cycle-route
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "cycle-route", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A Cycle.js helper for routing", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# Cycle Route | ||
A simple router factory that takes a static map of route => label and returns a function that maps a path to a route object of the form | ||
A simple router factory that takes a static map of route => label and returns a function that maps a path to a route object. | ||
```js | ||
{ | ||
path: '/', | ||
name: 'home', | ||
params: {} | ||
} | ||
``` | ||
It was created to be used with [```cycle-pushstate-driver```](https://github.com/secobarbital/cycle-pushstate-driver) but is a generic URL mapper that can be as a helper in any routing engine. | ||
@@ -18,5 +10,21 @@ | ||
```sh | ||
npm install @cycle-route | ||
npm install cycle-route | ||
``` | ||
## API | ||
###```makeRouter(routes)``` | ||
Takes as input a map from route definition to route name and returns a router function. ```cycle-route``` uses [```routington```](https://github.com/pillarjs/routington) so it accepts all route definitions that ```routington``` accepts and adds a ```*``` route to define the default route. | ||
## Format of route object | ||
```js | ||
{ | ||
path: '/', // this is the input path | ||
name: 'home', // this is the value from the routes map | ||
params: {} // this are the matched params | ||
} | ||
``` | ||
## Usage | ||
@@ -27,11 +35,11 @@ | ||
```js | ||
import makeRouter from ‘cycle-route’ | ||
import makeRouter from 'cycle-route' | ||
const router = makeRouter({ | ||
‘/’: ‘home’, | ||
‘/foo/:bar’: ‘foo’, | ||
‘*’: ‘notfound’ // default route | ||
'/': 'home', | ||
'/foo/:bar': 'foo', | ||
'*': 'notfound' // default route | ||
}) | ||
router(‘/’) | ||
router('/') | ||
``` | ||
@@ -42,5 +50,5 @@ | ||
```js | ||
import { makePushStateDriver } from ‘cycle-pushstate-driver’ | ||
import makeRouter from ‘cycle-route’ | ||
import routes from ‘routes’ | ||
import { makePushStateDriver } from 'cycle-pushstate-driver' | ||
import makeRouter from 'cycle-route' | ||
import routes from 'routes' | ||
@@ -60,3 +68,3 @@ const router = makeRouter(routes) | ||
const navigate$ = DOM.select(‘a’).events(‘click’) | ||
const navigate$ = DOM.select('a').events('click') | ||
.map(e => e.currentTarget.pathname) | ||
@@ -68,5 +76,5 @@ | ||
const pages = { | ||
‘home’: homePage, | ||
‘foo’: fooPage, | ||
‘bar’: barPage | ||
'home': homePage, | ||
'foo': fooPage, | ||
'bar': barPage | ||
} | ||
@@ -73,0 +81,0 @@ return pages[route.name] |
6208
87