-
-
Save jdeebee/d3c2cf45a093d0c9556e557408cd42cb to your computer and use it in GitHub Desktop.
Functional Reactive Programming (FRP) with Bacon.js - example 3 - JS Bin source: http://jsbin.com/vujowamiyo
This file contains 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> | |
<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> | |
<label>A: <input id="a"></input></label> | |
<label>B: <input id="b"></input></label> | |
<div>Sum: <span id="sum"></span></div> | |
<div>Product: <span id="product"></span></div> | |
<script id="jsbin-javascript"> | |
var a = valueStream($("#a")); | |
var b = valueStream($("#b")); | |
a.log("A:"); | |
b.log("B:"); | |
// map the streams to more sensible number values | |
a.map(str2Number); | |
b.map(str2Number); | |
// combine the streams to create sum | |
var sum = a.combine(b.plus); | |
// use onValue to show sum | |
sum.onValue(showSum); | |
// combine the streams to create product and show | |
// it in the correct span | |
function valueStream(element) { | |
return element | |
.asEventStream("keyup") | |
.map(".currentTarget.value"); | |
} | |
function str2Number(str) { | |
return Number(str); | |
} | |
function showSum(result) { | |
$("#sum").text(result); | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var a = valueStream($("#a")); | |
var b = valueStream($("#b")); | |
a.log("A:"); | |
b.log("B:"); | |
// map the streams to more sensible number values | |
a.map(str2Number); | |
b.map(str2Number); | |
// combine the streams to create sum | |
var sum = a.combine(b.plus); | |
// use onValue to show sum | |
sum.onValue(showSum); | |
// combine the streams to create product and show | |
// it in the correct span | |
function valueStream(element) { | |
return element | |
.asEventStream("keyup") | |
.map(".currentTarget.value"); | |
} | |
function str2Number(str) { | |
return Number(str); | |
} | |
function showSum(result) { | |
$("#sum").text(result); | |
}</script></body> | |
</html> |
This file contains 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
var a = valueStream($("#a")); | |
var b = valueStream($("#b")); | |
a.log("A:"); | |
b.log("B:"); | |
// map the streams to more sensible number values | |
a.map(str2Number); | |
b.map(str2Number); | |
// combine the streams to create sum | |
var sum = a.combine(b.plus); | |
// use onValue to show sum | |
sum.onValue(showSum); | |
// combine the streams to create product and show | |
// it in the correct span | |
function valueStream(element) { | |
return element | |
.asEventStream("keyup") | |
.map(".currentTarget.value"); | |
} | |
function str2Number(str) { | |
return Number(str); | |
} | |
function showSum(result) { | |
$("#sum").text(result); | |
} |
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 2