0

Say I have two Bacon.js properties. One is child object - maybe just his name. The other property object with the outfit he or she is wearing - pants color and shirt color.

In my case I am using Angular-Bacon to create properties from AngularJS scope variables.

How do I get a property that merges the two together? I'd expect I could do something like the following (assuming I had Lodash available):

var outfit = $scope.$watchCollectionAsProperty('outfit'); // {shirt: 'blue', pants: 'green'}
var child = $scope.$watchCollectionAsProperty('child'); // {name: 'Jimmy'}

var clothedChild = outfit.map(function(it) {
    return _.merge(it, child.value());
});

This doesn't work, though.

I'm super new to RFP and am trying to use Bacon in an Angular app, but I'm having a hard time wrapping my head around it.

Any tips would be appreciated.

Michael Oryl
  • 20,856
  • 14
  • 77
  • 117

2 Answers2

1

The answer was not as complicated as I thought - I just didn't know the term to look for, it seems.

var clothedChild = Bacon.combineWith(function(p1, p2) {
    return _.merge({}, p1, p2);
}, child, outfit);
Michael Oryl
  • 20,856
  • 14
  • 77
  • 117
  • 2
    Warning! That will modify the original child object. If any other code is listening on the child stream, they may or may not get the merged child object. Also, when the parent stream emits new values, those values will be merged with the already-merged child object. You can use `return _.merge({}, p1, p2);` to fix that. – Macil Jan 23 '15 at 05:20
  • `Bacon.combineWith(R.merge, child,outfit)` will do it without modifying anything – galileopy Feb 24 '17 at 00:35
1

You can also do

child.combine(outfit, function(child, outfit){
  return _.merge({}, child, outfit);
});

which is a bit shorter.

kristw
  • 26
  • 3