From 4837c63d6a2bbe270df521f82b5ba04283920bec Mon Sep 17 00:00:00 2001 From: Max Ogden Date: Tue, 26 Jul 2011 13:09:44 -0700 Subject: [PATCH] starting to refactor document state logic... wish i had pouchdb right about now --- attachments/script/costco.js | 12 +++++++--- attachments/script/recline.js | 45 +++++++++++++++++++++++------------ attachments/script/site.js | 6 ++--- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/attachments/script/costco.js b/attachments/script/costco.js index 9453bc76..75ddd88f 100755 --- a/attachments/script/costco.js +++ b/attachments/script/costco.js @@ -68,7 +68,8 @@ var costco = function() { costco.uploadDocs(toUpdate).then( function(updatedDocs) { util.notify(updatedDocs.length + " documents updated successfully"); - recline.fetchRows(false, app.offset); + recline.initializeTable(app.offset); + recline.updateDocCount(); dfd.resolve(updatedDocs); }, function(err) { @@ -85,12 +86,16 @@ var costco = function() { if(!docs.length) dfd.resolve("Failed: No docs specified"); couch.request({url: app.baseURL + "api/_bulk_docs", type: "POST", data: JSON.stringify({docs: docs})}) .then( - dfd.resolve, + function(resp) {ensureCommit().then(function() { dfd.resolve(resp) })}, function(err) { dfd.reject(err.responseText) } ); return dfd.promise(); } + function ensureCommit() { + return couch.request({url: app.baseURL + "api/_ensure_full_commit", type:'POST', data: "''"}); + } + function deleteColumn(name) { var deleteFunc = function(doc) { delete doc[name]; @@ -105,6 +110,7 @@ var costco = function() { mapDocs: mapDocs, updateDocs: updateDocs, uploadDocs: uploadDocs, - deleteColumn: deleteColumn + deleteColumn: deleteColumn, + ensureCommit: ensureCommit }; }(); \ No newline at end of file diff --git a/attachments/script/recline.js b/attachments/script/recline.js index 1f8ff9cf..c542e148 100755 --- a/attachments/script/recline.js +++ b/attachments/script/recline.js @@ -40,7 +40,11 @@ var recline = function() { function renderRows(response) { var rows = response.rows; - if (rows.length < 1) return; + if (rows.length < 1) { + util.render('dataTable', 'data-table-container'); + updateDocCount(); + return; + }; var tableRows = []; @@ -134,6 +138,16 @@ var recline = function() { } + function updateDocCount() { + return couch.request({url: app.baseURL + 'api/_all_docs?' + $.param({startkey: '"_design/"', endkey: '"_design0"'})}).then( + function ( data ) { + var ddocCount = data.rows.length; + $('#docCount').text(app.dbInfo.doc_count - ddocCount + " documents"); + } + ) + } + + function bootstrap() { util.registerEmitter(); util.listenFor(['esc', 'return']); @@ -153,12 +167,7 @@ var recline = function() { util.render('title', 'project-title', app.dbInfo); util.render( 'generating', 'project-actions' ); - couch.request({url: app.baseURL + 'api/_all_docs?' + $.param({startkey: '"_design/"', endkey: '"_design0"'})}).then( - function ( data ) { - var ddocCount = data.rows.length; - $('#docCount').text(app.dbInfo.doc_count - ddocCount + " documents"); - } - ) + updateDocCount(); couch.session().then(function(session) { if ( session.userCtx.name ) { @@ -169,13 +178,17 @@ var recline = function() { util.render('controls', 'project-controls', {text: text}); }) - couch.request({url: app.baseURL + 'api/headers'}).then(function ( headers ) { - app.headers = headers; - app.csvUrl = app.baseURL + 'api/csv?headers=' + escape(JSON.stringify(headers)); - - util.render( 'actions', 'project-actions', $.extend({}, app.dbInfo, {url: app.csvUrl}) ); - fetchRows(); - }) + initializeTable(); + }) + } + + function initializeTable(offset) { + couch.request({url: app.baseURL + 'api/headers'}).then(function ( headers ) { + app.headers = headers; + app.csvUrl = app.baseURL + 'api/csv?headers=' + escape(JSON.stringify(headers)); + + util.render( 'actions', 'project-actions', $.extend({}, app.dbInfo, {url: app.csvUrl}) ); + fetchRows(false, offset); }) } @@ -183,10 +196,12 @@ var recline = function() { formatDiskSize: formatDiskSize, handleMenuClick: handleMenuClick, showDialog: showDialog, + updateDocCount: updateDocCount, bootstrap: bootstrap, fetchRows: fetchRows, activateControls: activateControls, getPageSize: getPageSize, - renderRows: renderRows + renderRows: renderRows, + initializeTable: initializeTable }; }(); \ No newline at end of file diff --git a/attachments/script/site.js b/attachments/script/site.js index f4e65e27..60b865ba 100755 --- a/attachments/script/site.js +++ b/attachments/script/site.js @@ -55,7 +55,7 @@ app.after = { util.notify("Updating row...", {persist: true, loader: true}); couch.request({type: "PUT", url: app.baseURL + "api/" + doc._id, data: JSON.stringify(doc)}).then(function(response) { util.notify("Row updated successfully"); - recline.fetchRows(false, app.offset); + recline.initializeTable(); }) }) $('.data-table-cell-editor .cancelButton').click(function(e) { @@ -193,7 +193,7 @@ app.after = { util.notify("Saving documents...", {persist: true, loader: true}); costco.uploadDocs(util.lookupPath(util.selectedTreePath())).then(function(msg) { util.notify("Docs saved successfully!"); - recline.fetchRows(false, app.offset); + recline.initializeTable(app.offset); }); }) }, @@ -210,7 +210,7 @@ app.after = { costco.uploadDocs(docs).then( function(docs) { util.notify("Data uploaded successfully!"); - recline.fetchRows(false, app.offset); + recline.initializeTable(app.offset); util.hide('dialog'); }, function (err) {