-
-
Save jdeebee/a0526df892df33ec197d1a345c8f70c9 to your computer and use it in GitHub Desktop.
Functional Reactive Programming (FRP) with Bacon.js - example 5 - JS Bin source: http://jsbin.com/feqote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://fb.me/react-0.14.7.min.js"></script> | |
<script src="https://fb.me/react-dom-0.14.7.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> | |
<script src="https://rawgit.com/baconjs/bacon.js/master/dist/Bacon.js"></script> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Search helper | |
const doSearch = (str) => { | |
return Bacon.fromPromise($.get("https://openclipart.org/search/json/?query="+str)) | |
} | |
// BaconJS (store) | |
const searchBus = new Bacon.Bus() | |
const fullData = Bacon.combineTemplate({ | |
search: searchBus.toProperty(""), | |
results: [] | |
}) | |
fullData.log() | |
// create a stream of search results | |
const resultStream = fullData.flatMapLatest(doSearch).toProperty(); | |
// Event handlers (actions) | |
const searchChange = (e) => { | |
searchBus.push(e.target.value) | |
} | |
// Function component (view) | |
const renderResults = (results) => | |
React.createElement("div", null, "Sorry, results not implemented for you") | |
const HelloWorld = (props) => | |
(React.createElement("div", null, | |
React.createElement("h1", null, "Search test"), | |
React.createElement("input", {type: "text", onChange: searchChange, value: props.search}), | |
React.createElement("ul", null, | |
renderResults(props.results) | |
) | |
)); | |
// Tie everything in a bow | |
fullData.onValue((data) => { | |
ReactDOM.render(React.createElement(HelloWorld, React.__spread({}, data)), document.body); | |
}) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Search helper | |
const doSearch = (str) => { | |
return Bacon.fromPromise($.get("https://openclipart.org/search/json/?query="+str)) | |
} | |
// BaconJS (store) | |
const searchBus = new Bacon.Bus() | |
const fullData = Bacon.combineTemplate({ | |
search: searchBus.toProperty(""), | |
results: [] | |
}) | |
fullData.log() | |
// create a stream of search results | |
const resultStream = fullData.flatMapLatest(doSearch).toProperty(); | |
// Event handlers (actions) | |
const searchChange = (e) => { | |
searchBus.push(e.target.value) | |
} | |
// Function component (view) | |
const renderResults = (results) => | |
<div>Sorry, results not implemented for you</div> | |
const HelloWorld = (props) => | |
(<div> | |
<h1>Search test</h1> | |
<input type="text" onChange={searchChange} value={props.search} /> | |
<ul> | |
{renderResults(props.results)} | |
</ul> | |
</div>); | |
// Tie everything in a bow | |
fullData.onValue((data) => { | |
ReactDOM.render(<HelloWorld {...data} />, document.body); | |
}) | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Search helper | |
const doSearch = (str) => { | |
return Bacon.fromPromise($.get("https://openclipart.org/search/json/?query="+str)) | |
} | |
// BaconJS (store) | |
const searchBus = new Bacon.Bus() | |
const fullData = Bacon.combineTemplate({ | |
search: searchBus.toProperty(""), | |
results: [] | |
}) | |
fullData.log() | |
// create a stream of search results | |
const resultStream = fullData.flatMapLatest(doSearch).toProperty(); | |
// Event handlers (actions) | |
const searchChange = (e) => { | |
searchBus.push(e.target.value) | |
} | |
// Function component (view) | |
const renderResults = (results) => | |
React.createElement("div", null, "Sorry, results not implemented for you") | |
const HelloWorld = (props) => | |
(React.createElement("div", null, | |
React.createElement("h1", null, "Search test"), | |
React.createElement("input", {type: "text", onChange: searchChange, value: props.search}), | |
React.createElement("ul", null, | |
renderResults(props.results) | |
) | |
)); | |
// Tie everything in a bow | |
fullData.onValue((data) => { | |
ReactDOM.render(React.createElement(HelloWorld, React.__spread({}, data)), document.body); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Functional Reactive Programming (FRP) with Bacon.js - example 4