Академический Документы
Профессиональный Документы
Культура Документы
BASIC EXAMPLES
Cycle.js apps will always include at least
three important components: main(),
drivers, and run(). In main(), we
listen to drivers (the input to main), and
we speak to drivers (the output of
main). In Haskell 1.0 jargon, main()
takes driver response Observables and
output driver request Observables.
Cycle.run()ties main()and
drivers together, as we saw in the last
chapter.
function main(driverResponses) {
let driverRequests = {
DOM: // transform driverResponses.DOM
1/24
7/19/2015
Toggle a checkbox
Lets start from the assumption we have
an index.htmlfile with an element to
contain our app.
<!DOCTYPE html>
<html>
<head>
<script src="./checkbox-app.js"
<meta charset="utf-8">
<title>Cycle.js checkbox</title>
</head>
<body>
2/24
7/19/2015
http://cycle.js.org/basic-examples.html
3/24
7/19/2015
4/24
7/19/2015
http://cycle.js.org/basic-examples.html
5/24
7/19/2015
Cycle.run(main, {
DOM: makeDOMDriver('#app')
});
6/24
7/19/2015
7/24
7/19/2015
8/24
7/19/2015
9/24
7/19/2015
function main(responses) {
// ...
let user$ = responses.HTTP
.filter(res$ => res$.request.url
.mergeAll()
.map(res => res.body);
// ...
}
responses.HTTPis an Observable of
all the network responses this app is
observing. Because it could potentially
include responses unrelated to user
details, we need to filter()it. And
we also mergeAll(), to flatten the
Observable of Observables. This might
feel like magic right now, so read the
HTTP Driver docs if youre curious of the
details. We map each response resto
res.bodyin order to get the JSON
data from the response and ignore other
fields like HTTP status.
We still havent defined the rendering in
our app. We should display on the DOM
whatever data we have from the current
user in user$. So the VTree Observable
vtree$should depend directly on
user$, like this:
function main(responses) {
// ...
http://cycle.js.org/basic-examples.html
10/24
7/19/2015
http://cycle.js.org/basic-examples.html
11/24
7/19/2015
12/24
7/19/2015
Increment and
http://cycle.js.org/basic-examples.html
13/24
7/19/2015
decrement a counter
We saw how to use the dialogue pattern
of building user interfaces, but our
examples didnt have state: the label just
reacted to the checkbox event, and the
user details view just showed what came
from the HTTP response. Normally
applications have state in memory, so
lets see how to build a Cycle.js app for
that case.
If we have a counter Observable (emitting
events to tell the current counter value),
displaying the counter is as simple as this:
count$.map(count =>
h('div', [
h('button.increment', 'Increment'
h('button.decrement', 'Decrement'
h('p', 'Counter: ' + count)
])
)
14/24
7/19/2015
15/24
7/19/2015
http://cycle.js.org/basic-examples.html
16/24
7/19/2015
0 1
+1+1+1
scan((x,y)=>x+y)
0 1
0 12
h('button.decrement', 'Decrement'
h('button.increment', 'Increment'
h('p', 'Counter: ' + count
])
)
};
}
Cycle.run(main, {
DOM: makeDOMDriver('#app')
});
http://cycle.js.org/basic-examples.html
17/24
7/19/2015
http://cycle.js.org/basic-examples.html
18/24
7/19/2015
19/24
7/19/2015
20/24
7/19/2015
21/24
7/19/2015
22/24
7/19/2015
]),
h('h2', 'BMI is ' + bmi)
])
)
};
}
Cycle.run(main, {
DOM: makeDOMDriver('#app')
});
23/24
7/19/2015
http://cycle.js.org/basic-examples.html
MIT License
support of Futurice
2015
24/24