From 1f30a1956606258aa7651dac59989ce658aa6552 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 14 Apr 2015 10:59:08 +0200 Subject: [PATCH] Improved view example. --- packages/xo-collection/src/view.example.js | 32 ++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/xo-collection/src/view.example.js b/packages/xo-collection/src/view.example.js index 29768d51d..136e0c70b 100644 --- a/packages/xo-collection/src/view.example.js +++ b/packages/xo-collection/src/view.example.js @@ -1,32 +1,48 @@ +import forEach from 'lodash.foreach' + import Collection from './collection' import View from './view' +// =================================================================== + +// Create the collection. const users = new Collection() users.getId = (user) => user.name -const activeUsers = new View(users, 'active') -activeUsers.on('add', console.log) -activeUsers.on('update', console.log) -activeUsers.on('remove', console.log) - +// Inserts some data. users.add({ name: 'bob' }) - users.add({ name: 'clara', active: true }) - users.add({ name: 'ophelia' }) - users.add({ name: 'Steve', active: true }) +// ------------------------------------------------------------------- + +// Create the view. +const activeUsers = new View(users, 'active') + +// Register some event listeners to see the changes. +activeUsers.on('add', users => { + forEach(users, (_, id) => { + console.log('+ active user:', id) + }) +}) +activeUsers.on('remove', users => { + forEach(users, (_, id) => { + console.log('- active user:', id) + }) +}) + +// Make some changes in the future. setTimeout(function () { console.log('-----')