cycle-pushstate-driver
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "cycle-pushstate-driver", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A Cycle.js driver for working with the current URL", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# Cycle PushState Driver | ||
A [Cycle.js](http://cycle.js.org) [driver](http://cycle.js.org/drivers.html) for the history PushState API. | ||
A [Cycle.js](http://cycle.js.org) [driver](http://cycle.js.org/drivers.html) for the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API). | ||
## Design Choices | ||
This is a very minimal Cycle.js driver that simply isolates ```history.pushState``` calls and ```popstate``` events. It makes no assumption about how you want to filter and transform click/touch event streams to come up with the paths to push. Nor will it do ```e.preventDefault()``` for you. | ||
In the absence of ``pushState`` support it lets the links work as normal links. This means that your ```preventDefault``` driver needs to know not to capture these click/touch events. | ||
Finally, it does not currently support ```history.replaceState``` or the state argument of ```pushState``` | ||
If you prefer a driver that covers all these cases, you may want to consider the [```TylorS/cycle-history```](https://github.com/TylorS/cycle-history) driver which is a batteries-included approach to the same problem. | ||
## API | ||
@@ -10,3 +20,3 @@ | ||
Returns a navigation driver that calls ```history.pushState``` on the input paths and outputs paths sent to ```pushState``` as well as received with ```popstate``` events, starting with the current path. If pushState is not supported, this function returns a driver that simply emits the current path. | ||
Returns a driver that calls ```history.pushState``` on the input paths and outputs paths sent to ```pushState``` as well as received with ```popstate``` events, starting with the current path. If ```pushState``` is not supported, this function returns a driver that simply emits the current path. | ||
@@ -17,2 +27,3 @@ ## Install | ||
npm install cycle-pushstate-driver | ||
``` | ||
@@ -41,3 +52,3 @@ ## Usage | ||
```js | ||
function main(responses) { | ||
function main({ DOM, Path }) { | ||
let localLinkClick$ = DOM.select('a').events('click') | ||
@@ -49,3 +60,3 @@ .filter(e => e.currentTarget.host === location.host) | ||
let vtree$ = responses.Path | ||
let vtree$ = Path | ||
.map(url => { | ||
@@ -73,3 +84,3 @@ switch(url) { | ||
Routing use case: | ||
Routing use case with ```switch-path```: | ||
@@ -84,10 +95,10 @@ ```js | ||
function main(responses) { | ||
let localLinkClick$ = DOM.select('a').events('click') | ||
function main({ DOM, Path }) { | ||
const localLinkClick$ = DOM.select('a').events('click') | ||
.filter(e => e.currentTarget.host === location.host) | ||
let navigate$ = localLinkClick$ | ||
const navigate$ = localLinkClick$ | ||
.map(e => e.currentTarget.href) | ||
let vtree$ = responses.Path | ||
const vtree$ = Path | ||
.map(resolve) | ||
@@ -103,1 +114,52 @@ .map(({ value }) => value) | ||
``` | ||
Routing use case with ```wayfarer```: | ||
```js | ||
import wayfarer from 'wayfarer' | ||
function route (path$) { | ||
const route$ = new Rx.ReplaySubject(1) | ||
const r = name => params => route$.onNext({ name, params }) | ||
const router = wayfarer('/notfound') | ||
router.on('/', r('owers')) | ||
router.on('/owers/:ower', r('owees')) | ||
router.on('/notfound', r('notfound')) | ||
path$ | ||
.subscribe( | ||
path => router(path), | ||
route$.onError.bind(route$), | ||
route$.onCompleted.bind(route$) | ||
) | ||
return route$ | ||
} | ||
function main({ DOM, Path }) { | ||
const Route = route(Path) | ||
const localLinkClick$ = DOM.select('a').events('click') | ||
.filter(e => e.currentTarget.host === location.host) | ||
const navigate$ = localLinkClick$ | ||
.map(e => e.currentTarget.href) | ||
const vtree$ = Rx.Observable.combineLatest( | ||
Route, owersVtree$, oweesVtree$, notfoundVtree$, | ||
(route, owersVtree, oweesVtree, notfoundVtree) => { | ||
const vtrees = { | ||
'owers': owersVtree, | ||
'owees': oweesVtree, | ||
'notfound': notfoundVtree | ||
} | ||
return vtrees[route.name] | ||
} | ||
) | ||
return { | ||
DOM: vtree$, | ||
Path: navigate$, | ||
preventDefault: localLinkClick$ | ||
}; | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8312
159