-
-
Notifications
You must be signed in to change notification settings - Fork 115
Register listeners
Christian Alfoni edited this page Mar 5, 2015
·
3 revisions
var Baobab = require('baobab');
var tree = new Baobab({
todos: []
});
// Register to updates
tree.select('todos').on('update', function () {
// I triggered
});
tree.select('todos').push('foo');
// Register to state being removed from storage
tree.select('todos').on('irrelevant', function () {
// I triggered
});
tree.edit({});
// Register to state being added to storage
tree.select('todos', 0).on('relevant', function () {
// I triggered
});
tree.select('todos').push('foo');
At times, you might want to listen to updates concerning a logical combination of cursors. For instance, you might want to know when two cursors both updated or when either one or the other did.
var Baobab = require('baobab');
var tree = new Baobab({
todos: [],
emails: [],
notifications: []
});
var todos = tree.select('todos');
var notifications = tree.select('notifications');
var emails = tree.select('emails');
// Simple "or" combination
var todosOrNotifications = todos.or(notifications);
// Simple "and" combination
var todosAndNotifications = todos.and(notifications);
// Complex combination
var todosOrEmailsAndNotifications = todos.or(emails).and(notifications);
// Listening to events
todosOrEmailsAndNotifications.on('update', handler);
// Releasing a combination to avoid leaks
todosOrEmailsAndNotifications.release();