Skip to content

Instantly share code, notes, and snippets.

@dtao
Created May 5, 2014 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtao/2a837c9e785c90c9d19b to your computer and use it in GitHub Desktop.
Save dtao/2a837c9e785c90c9d19b to your computer and use it in GitHub Desktop.
Lazy.js jQuery Event Stream
window.addEventListener('load', function() {
// This 'createWrapper' method is super weird and awkward and I'm not proud of
// it at all. But right now it's the only option for defining custom sequence
// types that wrap arbitrary sources. In a future version I will come up with
// something much better, swear to God.
var jqueryWrapper = Lazy.createWrapper(function(selector, eventName) {
var source = this;
$(selector).on(eventName, function(e) {
source.emit(e);
});
});
jqueryWrapper('#click-source', 'click')
.map(function(e) { return [e.clientX, e.clientY]; })
.each(function(pos) {
$('#click-position').text('Clicked at ' + pos.join(', '));
});
});
<html>
<head>
<title>Lazy.js FRP Example</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="https://rawgit.com/dtao/lazy.js/master/lazy.js"></script>
<script src="https://rawgit.com/dtao/lazy.js/master/lazy.browser.js"></script>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="frp.example.js"></script>
</head>
<body>
<div id="click-source">
<p>Click anywhere in here.</p>
<div id="click-position"></div>
</div>
</body>
</html>
html, body { height: 100% ;}
#click-source {
position: relative;
top: 50%;
height: 400px;
margin-top: -200px;
background-color: #eee;
cursor: pointer;
}
#click-source p {
position: relative;
top: 50%;
margin-top: -12px;
line-height: 24px;
font-size: 20px;
text-align: center;
}
#click-position {
position: absolute;
bottom: 10px;
right: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment