starting to refactor document state logic... wish i had pouchdb right about now

This commit is contained in:
Max Ogden 2011-07-26 13:09:44 -07:00
parent 3312498a14
commit 4837c63d6a
3 changed files with 42 additions and 21 deletions

View File

@ -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
};
}();

View File

@ -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
};
}();

View File

@ -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) {