Skip to content

Instantly share code, notes, and snippets.

@sadeqzadeh

sadeqzadeh/index.html

Forked from anonymous/index.html
Last active May 28, 2016
Embed
What would you like to do?
Functional Reactive Programming (FRP) with Bacon.js - example 2 - JS Bin source: http://jsbin.com/funaqocoso
<!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>
<style id="jsbin-css">
button, textarea {
display: block;
margin: 10px;
width: 200px
}
</style>
</head>
<body>
<button id="enable">Enable</button>
<button id="disable">Disable</button>
<textarea id="target"></textarea>
<script id="jsbin-javascript">
function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var enableClicks = $("#enable").asEventStream("click");
var disableClicks = $("#disable").asEventStream("click");
// Use .map to create a stream with more sensible values than jquery events
var enableStream = enableClicks.map(function(x) {return getRandomIntInclusive(1,10);});
var disableStream = disableClicks.map(function(x) {return getRandomIntInclusive(1000,10000);});
// Use .merge to join the two streams together
var combinedStream = enableStream.merge(disableStream);
// Use .onValue to enable or disable the
// textarea
combinedStream.onValue(function(x) {
console.log("number from result stream:", x);
if (x>10)
$("#target").prop( "disabled", true );
});
</script>
<script id="jsbin-source-css" type="text/css">button, textarea {
display: block;
margin: 10px;
width: 200px
}</script>
<script id="jsbin-source-javascript" type="text/javascript">function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var enableClicks = $("#enable").asEventStream("click");
var disableClicks = $("#disable").asEventStream("click");
// Use .map to create a stream with more sensible values than jquery events
var enableStream = enableClicks.map(function(x) {return getRandomIntInclusive(1,10);});
var disableStream = disableClicks.map(function(x) {return getRandomIntInclusive(1000,10000);});
// Use .merge to join the two streams together
var combinedStream = enableStream.merge(disableStream);
// Use .onValue to enable or disable the
// textarea
combinedStream.onValue(function(x) {
console.log("number from result stream:", x);
if (x>10)
$("#target").prop( "disabled", true );
});</script></body>
</html>
button, textarea {
display: block;
margin: 10px;
width: 200px
}
function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var enableClicks = $("#enable").asEventStream("click");
var disableClicks = $("#disable").asEventStream("click");
// Use .map to create a stream with more sensible values than jquery events
var enableStream = enableClicks.map(function(x) {return getRandomIntInclusive(1,10);});
var disableStream = disableClicks.map(function(x) {return getRandomIntInclusive(1000,10000);});
// Use .merge to join the two streams together
var combinedStream = enableStream.merge(disableStream);
// Use .onValue to enable or disable the
// textarea
combinedStream.onValue(function(x) {
console.log("number from result stream:", x);
if (x>10)
$("#target").prop( "disabled", true );
});
@sadeqzadeh

This comment has been minimized.

Copy link
Owner Author

@sadeqzadeh sadeqzadeh commented May 28, 2016

Functional Reactive Programming (FRP) with Bacon.js - example 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment