Skip to content

Commit 41ea641

Browse files
committed
Merge pull request #9 from nakaji-dayo/master
Add example using cycle/http
2 parents ccd87bf + 2036b00 commit 41ea641

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

example/contacts.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"name": "foo",
4+
"email": "[email protected]"
5+
},
6+
{
7+
"name": "bar",
8+
"email": "[email protected]"
9+
}
10+
]

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"license": "MIT",
1414
"dependencies": {
1515
"@cycle/core": "^6.0.3",
16+
"@cycle/http": "^8.2.2",
1617
"cycle-snabbdom": "^1.2.0",
1718
"history": "^2.0.1",
1819
"rx": "^4.1.0"

example/src/app.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import Sidebar from './components/sidebar'
44
import Home from './components/home'
55
import Inbox from './components/inbox'
66
import Compose from './components/compose'
7+
import Contact from './components/contact'
78
import NotFound from './components/notfound'
89

910
const routes = {
1011
'/': Home,
1112
'/inbox': Inbox,
1213
'/compose': Compose,
14+
'/contact': Contact,
1315
'*': NotFound,
1416
}
1517

@@ -53,12 +55,13 @@ function App(sources) {
5355
const {router} = sources
5456
const match$ = router.define(routes)
5557
const sidebar = Sidebar(sources, match$.pluck('path'))
56-
const childrenDOM$ = match$.map(
57-
({path, value}) => value({...sources, router: router.path(path)}).DOM
58+
const children$ = match$.map(
59+
({path, value}) => value({...sources, router: router.path(path)})
5860
)
5961

6062
return {
61-
DOM: sidebar.DOM.combineLatest(childrenDOM$, view),
63+
DOM: sidebar.DOM.combineLatest(children$.map(x => x.DOM), view),
64+
HTTP: children$.map(x => x.HTTP).filter(x => !!x).mergeAll(),
6265
}
6366
}
6467

example/src/components/contact.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {Observable} from 'rx'
2+
import {div, h2, ul, li} from 'cycle-snabbdom'
3+
4+
function Contact(sources) {
5+
const getContacts$ = Observable.just({})
6+
.map(() => ({
7+
url: '/contacts.json',
8+
method: 'GET',
9+
}))
10+
const contacts$ = sources.HTTP
11+
.mergeAll()
12+
.map(x => x.body)
13+
.startWith([])
14+
return {DOM: contacts$.map(xs => div([
15+
h2({style: {padding: '1em'}}, 'Contact Stuffs'),
16+
ul({},
17+
xs.map(x => li({}, `${x.name}: ${x.email}`))
18+
),
19+
])),
20+
HTTP: getContacts$}
21+
}
22+
23+
export default Contact

example/src/components/sidebar.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function Sidebar(sources, path$) {
2828
const inboxHref = createHref('/inbox')
2929
const composeHref = createHref('/compose')
3030
const contactHref = createHref('/contact')
31+
const newHref = createHref('/new')
3132

3233
const view$ = path$.map(() => {
3334
return ul({style: ulStyle},[
@@ -40,6 +41,9 @@ function Sidebar(sources, path$) {
4041
li({style: liStyle}, [
4142
a({style: linkStyle, props: {href: contactHref}}, 'Contacts'),
4243
]),
44+
li({style: liStyle}, [
45+
a({style: linkStyle, props: {href: newHref}}, 'New'),
46+
]),
4347
])
4448
})
4549

example/src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {run} from '@cycle/core'
22
import {makeDOMDriver} from 'cycle-snabbdom'
3+
import {makeHTTPDriver} from '@cycle/http'
34
import {makeRouterDriver} from '../../lib'
45
import {createHashHistory} from 'history'
56

@@ -10,4 +11,5 @@ const history = createHashHistory({queryKey: false})
1011
run(app, {
1112
DOM: makeDOMDriver('#app'),
1213
router: makeRouterDriver(history),
14+
HTTP: makeHTTPDriver(),
1315
})

0 commit comments

Comments
 (0)