Skip to navigation
Keep a javascript data-oject synced with a pouchdb database
17.09.18
function fetch_initial_docs() { return db.db.allDocs({ include_docs: true }).then(function (res) { var obj = {}; for (var x = 0; x < res.rows.length ;x++) { obj[res.rows[x].id] = res.rows[x]; } app.data['orders'] = obj; render_docs(); }); } function react_to_changes() { db.db.changes({live: true, since: 'now', include_docs: true}).on('change', function (change) { if (change.deleted) { console.log('delete: '+ change.id); delete app.data['orders'][change.id]; } else { console.log('update:' + change.id); app.data['orders'][change.id] = change.doc; } render_docs(); }).on('error', console.log.bind(console)); } function render_docs() { console.log('size: '+ Object.keys(app.data['orders']).length); } fetch_initial_docs().then(react_to_changes).catch(console.log.bind(console));
https://pouchdb.com/2015/02/28/efficiently-managing-ui-state-in-pouchdb.html
Reply
Anonymous
Information Epoch 1766936103
Save trees.
Home
Notebook
Contact us