简体   繁体   中英

Using multiple event stream values with Bacon.js

I have a form with currently 2 inputs (amount will increase), where users add/edit numbers. After a change in any of the fields, I want to make a calculation with both numbers.

With basic jQuery, it would be a breeze:

var f1 = $(".field-1"), f2 = $(".field-2"), n1, n2, result;
$(".field-1, .field-2").on("keyup", function(){
    n1 = f1.val();
    n2 = f2.val();
    result = n1 / n2;
});

What's the best way to make the same thing with Bacon.js?

With Bacon, this is even simpler:

function keyupValueStream(el) {
    return $(el).asEventStream("keyup").map(".target.value");
}
Bacon.combineWith(function(n1, n2) { return n1/n2; },
                  keyupValueStream(".field-1"),
                  keyupValueStream(".field-2")
).onValue(function(result) {
     // …
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM