Skip to content

Instantly share code, notes, and snippets.

@b00gizm
Created June 13, 2014 08:35
Show Gist options
  • Save b00gizm/5d4019fe3ba58fbcf667 to your computer and use it in GitHub Desktop.
Save b00gizm/5d4019fe3ba58fbcf667 to your computer and use it in GitHub Desktop.
Just a very brief introduction to Bacon.js
// Bacon.js playground
// This is how event handling is done in jQuery
$('#btn').on('click', function(evt) {
alert("I was clicked!");
});
// This is how you do it in Bacon.js
var click = $('#btn').asEventStream('click');
click.onValue(function(evt) {
alert("I was clicked with Bacon!");
});
// LOL! THAT'S MUCH MORE CODE TO WRITE!!!
// Yeah, in this case you're right. But Bacon has more nice stuff
// How about mapping functions
var likeButton = $('#like-btn').asEventStream('click');
likeButton
.map(1)
.onValue(function(val) {
alert("You have " + val + " more like!");
})
;
// by the way: .map(1) is short for .map(function(inp) { ...; return 1; })
// Oh, you can have filters, too!
var password = $('#secret-password-input').asEventStream('blur');
password
.map(function(evt) {
return evt.target.value
})
.filter(function(val) {
return val == 's3cr3t';
})
.onValue(function() {
// This will only trigger, if filter function returns TRUE
alert("OMG! YOU DID IT.");
})
;
// You can merge streams!
var plusOne = $('#plus-btn').asEventStream('click').map(1);
var minusOne = $('#minus-btn').asEventStream('click').map(-1);
Bacon
.merge(plusOne, minusOne)
.onValue(function(val) {
alert("You clicked: " + val);
})
;
// Bacon.js also know "Properties". These are kind like event streams, but with a concept of a current value
var prop = Bacon.property(42);
// You can make an event stream a property with the .toProperty() method. Its argument is the property's initial value
var accept = $('#accept-toc-checkbox')
.asEventStream('click')
.map(function(evt) {
return !!evt.target.value;
})
.toProperty(false)
;
accept.onValue(function(val) {
alert('You did ' + (!val ? 'not ' : '') + 'accept the toc');
});
// This will promptly yield 'You did not accept the toc'
// We can do better! Use a property's value to enable or disable a button
accept
.not() // .not() reverses the value
.assign($('#signup-btn'), 'attr', 'disabled')
;
// Or how about combining streams?
var over18 = $('#over18-checkbox')
.asEventStream('click')
.map(function(evt) {
return !!evt.target.value;
})
.toProperty(false)
;
Bacon
.combineWith(function(v1, v2) { return v1&&v2; }, accept, over18)
.not()
.assign($('#signup-btn'), 'attr', 'disabled')
;
// This will only enable the signup button, if you accepted the toc and you're over 18
// There's a short variant, too
accept.and(over18).not().assign($('#signup-btn'), 'attr', 'disabled');
// Want a real word example?
// See: http://jsfiddle.net/b00gizm/nby9P/
var valueFromEvt = function(evt) {
return evt.target.value;
};
var username = $('input[name=username]')
.asEventStream('blur') // on blur
.map(valueFromEvt) // apply 'valueFromEvent'
.toProperty('') // set initial value to ''
;
var email = $('input[name=email]')
.asEventStream('blur')
.map(valueFromEvt)
.toProperty('')
;
var bothNotEmpty = function(v1, v2) {
return !!v1 && !!v2;
}
Bacon
.combineWith(bothNotEmpty, username, email)
.not()
.assign($('#signup-btn'), 'attr', 'disabled')
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment