Already found questions- how should I keep a running total of several values with bacon.js?
I've already found the above question, which deals with a static set of properties. However, I'm interested in a variation, wherein the set of properties is dynamic.
My specific case is that I have a set of things which have editable costs, forming an observable Property. From these, I combine the properties with combineWith
to get the total cost for ALL events. (Code at end)
Right now, I've got the properties hooked up properly for a static set of events using jQuery to get the right values, but the idea is that the user could add (or delete) an event arbitrarily and the calculated total will be updated accordingly.
I have a vague inkling that the tool for the job is combineAsArray
and some clever usage of event listening at the common parent for all of my streams, or possibly some sort of Bacon.Bus
(dynamically plugging streams into it?), but I'm really spinning my wheels now...
The (Pseudo)Code
<table>
<tr>
<td><input id="thing1" value="40"></input></td>
</tr>
<tr>
<td><input id="thing2" value="40"></input></td>
</tr>
</table>
$(function() {
stream1 = $("#input1").asEventStream("change blur keyup").toProperty();
stream2 = $("#input2").asEventStream("change blur keyup").toProperty();
totalMonthlyCost = Bacon.combineWith(
sumAll,
[stream1, stream2]
);
});