Skip to content

Commit adcdc8b

Browse files
committed
refactor(README): Update README for V5
1 parent 7e66bc4 commit adcdc8b

File tree

2 files changed

+148
-20
lines changed

2 files changed

+148
-20
lines changed

README.md

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
# cyclic-router
2-
cyclic-router is a Router Driver built for Cycle.js
2+
cyclic-router V5 is a routing library built for Cycle.js. Unlike previous versions, cyclic-router V5 is not a driver. It is a `main` function-wrapper which relies on @cycle/history for driver source/sink interactions
33

4-
**Disclaimer** Use v4 for Cycle Unified. v2.x.x is for Cycle and v3 is for Cycle Diversity.
5-
If you are still using @cycle/core please continue to use v1.x.x
4+
For older versions of cyclic-router, V4 and earlier please go to the old [README](https://github.com/cyclejs-community/cyclic-router/README_V4.md)
65

76
## Installation
87

98
Using [npm](https://www.npmjs.com/):
109

1110
$ npm install --save cyclic-router
1211

13-
Versions 3 and 4 requires users to inject the route matcher. We'll use `switch-path` for our examples but other
14-
matching libraries could be adapted to be used here:
12+
Cyclic router requires you to inject the route matcher. We'll use `switch-path` for our examples but other matching libraries could be adapted to be used here:
1513

1614
$ npm install --save switch-path
17-
18-
Note: Version 2 and below use `switch-path` for the route matcher always and the above library install is not necesssary/done implicitly.
19-
20-
Then with a module bundler like [browserify](http://browserify.org/), use as you would anything else:
21-
2215
```js
2316
// using an ES6 transpiler, like babel
24-
import {makeRouterDriver} from 'cyclic-router'
17+
import {routerify} from 'cyclic-router'
2518

2619
// not using an ES6 transpiler
27-
var makeRouterDriver = require('cyclic-router').makeRouterDriver
20+
var routerify = require('cyclic-router').routerify
2821
```
2922

3023
## API
@@ -37,9 +30,9 @@ For API documentation please visit this link [here](http://cyclejs-community.git
3730
import xs from 'xstream';
3831
import Cycle from '@cycle/xstream-run';
3932
import {makeDOMDriver} from '@cycle/dom';
40-
import {makeRouterDriver} from 'cyclic-router';
41-
import {createBrowserHistory} from 'history';
42-
import switchPath from 'switch-path'; // Required in v3, not required in v2 or below
33+
import {routerify} from 'cyclic-router';
34+
import {makeHistoryDriver} from '@cycle/history';
35+
import switchPath from 'switch-path';
4336

4437
function main(sources) {
4538
const match$ = sources.router.define({
@@ -49,22 +42,45 @@ function main(sources) {
4942

5043
const page$ = match$.map(({path, value}) => {
5144
return value(Object.assign({}, sources, {
52-
router: sources.router.path(path)
45+
router: sources.router.path(path) // notice use of 'router' source name,
46+
// which proxies raw 'history' source with
47+
// additional functionality
5348
}));
5449
});
5550

5651
return {
5752
DOM: page$.map(c => c.DOM).flatten(),
58-
router: xs.of('/other'),
53+
router: xs.of('/other') // Notice use of 'router' sink name,
54+
// which proxies the original 'history' sink
5955
};
6056
}
6157

62-
Cycle.run(main, {
58+
const mainWithRouting = routerify(main, switchPath)
59+
60+
Cycle.run(mainWithRouting, {
6361
DOM: makeDOMDriver('#app'),
64-
router: makeRouterDriver(createBrowserHistory(), switchPath) // v3
65-
// router: makeRouterDriver(createHistory()) // <= v2
62+
history: makeHistoryDriver() // create history driver as usual,
63+
// but it gets proxied by routerify
6664
});
6765
```
66+
### Routerify Options
67+
68+
Routerify accepts an options object like so: `routerify(main, options)`
69+
70+
The `options` object conforms to the interface:
71+
```
72+
interface RouterOptions {
73+
basename?: string;
74+
historyName?: string;
75+
routerName?: string;
76+
omitHistory?: boolean;
77+
}
78+
```
79+
80+
- `basename` - The root router path, defaults to `/`
81+
- `historyName` - The source/sink name associated with the raw history river, defaults to `history`
82+
- `routerName` - The source/sink name you want exposed to your app which caputures the functionality/streams associated with routerify. Defaults to `router`
83+
- `omitHistory` - Routerify hides the source/sink name associated with the raw history driver (given in the `historyName` option) from your app and only exposes the source/sink name associated with the router (given in the `routerName` option). Defaults to `true`, If you would like your app to have access to both the raw `history` source/sink and the `router` source/sink (injected by routerify), set this option to `false`
6884

6985
### Route Parameters
7086

README_V4.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# cyclic-router
2+
cyclic-router is a Router Driver built for Cycle.js
3+
4+
**Disclaimer** Use v4 for Cycle Unified. v2.x.x is for Cycle and v3 is for Cycle Diversity.
5+
If you are still using @cycle/core please continue to use v1.x.x
6+
7+
## Installation
8+
9+
Using [npm](https://www.npmjs.com/):
10+
11+
$ npm install --save cyclic-router
12+
13+
Versions 3 and 4 requires users to inject the route matcher. We'll use `switch-path` for our examples but other
14+
matching libraries could be adapted to be used here:
15+
16+
$ npm install --save switch-path
17+
18+
Note: Version 2 and below use `switch-path` for the route matcher always and the above library install is not necesssary/done implicitly.
19+
20+
Then with a module bundler like [browserify](http://browserify.org/), use as you would anything else:
21+
22+
```js
23+
// using an ES6 transpiler, like babel
24+
import {makeRouterDriver} from 'cyclic-router'
25+
26+
// not using an ES6 transpiler
27+
var makeRouterDriver = require('cyclic-router').makeRouterDriver
28+
```
29+
30+
## API
31+
32+
For API documentation please visit this link [here](http://cyclejs-community.github.io/cyclic-router/docs/)
33+
34+
## Basic Usage
35+
36+
```js
37+
import xs from 'xstream';
38+
import Cycle from '@cycle/xstream-run';
39+
import {makeDOMDriver} from '@cycle/dom';
40+
import {makeRouterDriver} from 'cyclic-router';
41+
import {createBrowserHistory} from 'history';
42+
import switchPath from 'switch-path'; // Required in v3, not required in v2 or below
43+
44+
function main(sources) {
45+
const match$ = sources.router.define({
46+
'/': HomeComponent,
47+
'/other': OtherComponent
48+
});
49+
50+
const page$ = match$.map(({path, value}) => {
51+
return value(Object.assign({}, sources, {
52+
router: sources.router.path(path)
53+
}));
54+
});
55+
56+
return {
57+
DOM: page$.map(c => c.DOM).flatten(),
58+
router: xs.of('/other'),
59+
};
60+
}
61+
62+
Cycle.run(main, {
63+
DOM: makeDOMDriver('#app'),
64+
router: makeRouterDriver(createBrowserHistory(), switchPath) // v3
65+
// router: makeRouterDriver(createHistory()) // <= v2
66+
});
67+
```
68+
69+
### Route Parameters
70+
71+
This behavior changes based on the injected route matcher. In the case of `switch-path`, you can pass route parameters to your component by adding them to the component sources.
72+
73+
```js
74+
const routes = {
75+
'/:id': id => sources => YourComponent({props$: Observable.of({id}), ...sources})
76+
}
77+
```
78+
79+
### Dynamically change route
80+
81+
You can dynamically change route from code by emitting inputs handled by [the history driver](http://cycle.js.org/history/docs/#historyDriver).
82+
83+
```js
84+
function main(sources) {
85+
// ...
86+
const homePageClick$ = sources.DOM.select(".home").events("click");
87+
const previousPageClick$ = sources.DOM.select(".previous").events("click");
88+
const nextPageClick$ = sources.DOM.select(".next").events("click");
89+
const oldPageClick$ = sources.DOM.select(".old").events("click");
90+
const aboutPageClick$ = sources.DOM.select(".about").events("click");
91+
92+
return {
93+
// ...
94+
router: xs.merge(
95+
// Go to page "/"
96+
homePageClick$.mapTo("/"),
97+
98+
// Go back to previous page
99+
previousPageClick$.mapTo({ type: "goBack" }),
100+
101+
// Go forward to next page
102+
nextPageClick$.mapTo({ type: "goForward" }),
103+
104+
// Go back from 5 pages
105+
oldPageClick$.mapTo({ type: "go", value: -5 }),
106+
107+
// Go to page "/about" with some state set to router's location
108+
aboutPageClick$.mapTo({ pathname: "/about", state: { some: "state" } })
109+
),
110+
};
111+
}
112+
```

0 commit comments

Comments
 (0)