From e6d9b5445014ff6af466e91b267cec3954cfa1a1 Mon Sep 17 00:00:00 2001 From: Nicolas Carlo Date: Mon, 22 Aug 2016 23:58:50 +0200 Subject: [PATCH] Explain how to dynamically change route in README --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 0d5a6ab..ea7d11b 100644 --- a/README.md +++ b/README.md @@ -66,3 +66,38 @@ const routes = { '/:id': id => sources => YourComponent({props$: Observable.of({id}), ...sources}) } ``` + +### Dynamically change route + +You can dynamically change route from code by emitting inputs handled by [the history driver](http://cycle.js.org/history/docs/#historyDriver). + +```js +function main(sources) { + // ... + const homePageClick$ = sources.DOM.select(".home").events("click"); + const previousPageClick$ = sources.DOM.select(".previous").events("click"); + const nextPageClick$ = sources.DOM.select(".next").events("click"); + const oldPageClick$ = sources.DOM.select(".old").events("click"); + const aboutPageClick$ = sources.DOM.select(".about").events("click"); + + return { + // ... + router: xs.merge( + // Go to page "/" + homePageClick$.mapTo("/"), + + // Go back to previous page + previousPageClick$.mapTo({ type: "goBack" }), + + // Go forward to next page + nextPageClick$.mapTo({ type: "goForward" }), + + // Go back from 5 pages + oldPageClick$.mapTo({ type: "go", value: -5 }), + + // Go to page "/about" with some state set to router's location + aboutPageClick$.mapTo({ pathname: "/about", state: { some: "state" } }) + ), + }; +} +```