From a6eff43452d3a2bf61d6c019827206e32b4f23bf Mon Sep 17 00:00:00 2001 From: nakaji-dayo Date: Sun, 27 Mar 2016 22:06:04 +0900 Subject: [PATCH 1/2] Add example using cycle/http --- example/contacts.json | 10 ++++++++++ example/package.json | 1 + example/src/app.js | 9 ++++++--- example/src/components/contact.js | 23 +++++++++++++++++++++++ example/src/main.js | 2 ++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 example/contacts.json create mode 100644 example/src/components/contact.js diff --git a/example/contacts.json b/example/contacts.json new file mode 100644 index 0000000..9aeaa12 --- /dev/null +++ b/example/contacts.json @@ -0,0 +1,10 @@ +[ + { + "name": "foo", + "email": "foo@example.com" + }, + { + "name": "bar", + "email": "bar@example.com" + } +] diff --git a/example/package.json b/example/package.json index 86be8dc..8205b9f 100644 --- a/example/package.json +++ b/example/package.json @@ -13,6 +13,7 @@ "license": "MIT", "dependencies": { "@cycle/core": "^6.0.3", + "@cycle/http": "^8.2.2", "cycle-snabbdom": "^1.2.0", "history": "^2.0.1", "rx": "^4.1.0" diff --git a/example/src/app.js b/example/src/app.js index 8fab89e..e5d39b2 100644 --- a/example/src/app.js +++ b/example/src/app.js @@ -4,12 +4,14 @@ import Sidebar from './components/sidebar' import Home from './components/home' import Inbox from './components/inbox' import Compose from './components/compose' +import Contact from './components/contact' import NotFound from './components/notfound' const routes = { '/': Home, '/inbox': Inbox, '/compose': Compose, + '/contact': Contact, '*': NotFound, } @@ -53,12 +55,13 @@ function App(sources) { const {router} = sources const match$ = router.define(routes) const sidebar = Sidebar(sources, match$.pluck('path')) - const childrenDOM$ = match$.map( - ({path, value}) => value({...sources, router: router.path(path)}).DOM + const children$ = match$.map( + ({path, value}) => value({...sources, router: router.path(path)}) ) return { - DOM: sidebar.DOM.combineLatest(childrenDOM$, view), + DOM: sidebar.DOM.combineLatest(children$.map(x => x.DOM), view), + HTTP: children$.map(x => x.HTTP).filter(x => !!x).mergeAll(), } } diff --git a/example/src/components/contact.js b/example/src/components/contact.js new file mode 100644 index 0000000..967481c --- /dev/null +++ b/example/src/components/contact.js @@ -0,0 +1,23 @@ +import {Observable} from 'rx' +import {div, h2, ul, li} from 'cycle-snabbdom' + +function Contact(sources) { + const getContacts$ = Observable.just({}) + .map(() => ({ + url: '/contacts.json', + method: 'GET', + })) + const contacts$ = sources.HTTP + .mergeAll() + .map(x => x.body) + .startWith([]) + return {DOM: contacts$.map(xs => div([ + h2({style: {padding: '1em'}}, 'Contact Stuffs'), + ul({}, + xs.map(x => li({}, `${x.name}: ${x.email}`)) + ), + ])), + HTTP: getContacts$} +} + +export default Contact diff --git a/example/src/main.js b/example/src/main.js index 6a40643..3d82686 100644 --- a/example/src/main.js +++ b/example/src/main.js @@ -1,5 +1,6 @@ import {run} from '@cycle/core' import {makeDOMDriver} from 'cycle-snabbdom' +import {makeHTTPDriver} from '@cycle/http' import {makeRouterDriver} from '../../lib' import {createHashHistory} from 'history' @@ -10,4 +11,5 @@ const history = createHashHistory({queryKey: false}) run(app, { DOM: makeDOMDriver('#app'), router: makeRouterDriver(history), + HTTP: makeHTTPDriver(), }) From 2036b009e684f11257763a08765920d4facbdf9e Mon Sep 17 00:00:00 2001 From: nakaji-dayo Date: Mon, 28 Mar 2016 22:29:12 +0900 Subject: [PATCH 2/2] add link for 404 example --- example/src/components/sidebar.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/example/src/components/sidebar.js b/example/src/components/sidebar.js index dc452e6..b3085e1 100644 --- a/example/src/components/sidebar.js +++ b/example/src/components/sidebar.js @@ -28,6 +28,7 @@ function Sidebar(sources, path$) { const inboxHref = createHref('/inbox') const composeHref = createHref('/compose') const contactHref = createHref('/contact') + const newHref = createHref('/new') const view$ = path$.map(() => { return ul({style: ulStyle},[ @@ -40,6 +41,9 @@ function Sidebar(sources, path$) { li({style: liStyle}, [ a({style: linkStyle, props: {href: contactHref}}, 'Contacts'), ]), + li({style: liStyle}, [ + a({style: linkStyle, props: {href: newHref}}, 'New'), + ]), ]) })