Skip to content
Christian Alfoni edited this page Mar 5, 2015 · 5 revisions

Chaining

If one update depends on an other update you can chain those updates together.

var Baobab = require('baobab');

var tree = new Baobab({
  nextId: 0
});

var updateNextId = function(id) {
  return id + 1;
};

var nextId = tree.select('nextId');

// On update event this will produce 1
nextId.apply(updateNextId);
nextId.apply(updateNextId);

// On update event this will produce 2
nextId.chain(updateNextId).chain(updateNextId);

Update

If you ever need to specify complex updates without resetting the whole subtree you are acting on, for readability or performance reasons, you remain free to use Baobab's internal update specifications.

These are widely inspired by React's immutable helpers, themselves inspired by MongoDB's ones.

The available commands are the following and are basically the same as the cursor's updating methods:

  • $set
  • $apply
  • $chain
  • $push
  • $unshift
  • $merge
var tree = new Baobab({
  users: {
    john: {
      firstname: 'John',
      lastname: 'Silver'
    },
    jack: {
      firstname: 'Jack',
      lastname: 'Gold'
    }
  }
});

tree.update({
  users: {
    john: {
      firstname: {
        $set: 'John the 3rd'
      }
    },
    jack: {
      firstname: {
        $apply: function(firstname) {
          return firstname + ' the 2nd';
        }
      }
    }
  }
});

tree.select('users', 'john').update({
  firstname: {
    $set: 'Little Johnsie'
  }
});