[#14,#6,backend,view][m]: delete row in data table now working via Backbone with backend memory.

* #6: BackendMemory now supports delete for Documents.
This commit is contained in:
Rufus Pollock
2012-01-05 12:21:16 +00:00
parent d5793c4044
commit e65bc74a88
3 changed files with 33 additions and 16 deletions

View File

@@ -79,7 +79,7 @@ my.BackendMemory = Backbone.Model.extend({
// }; // };
initialize: function(dataset) { initialize: function(dataset) {
// deep copy // deep copy
this._datasetAsData = _.extend({}, dataset); this._datasetAsData = $.extend(true, {}, dataset);
_.bindAll(this, 'sync'); _.bindAll(this, 'sync');
}, },
getDataset: function() { getDataset: function() {
@@ -118,6 +118,15 @@ my.BackendMemory = Backbone.Model.extend({
dfd.resolve(model); dfd.resolve(model);
} }
return dfd.promise(); return dfd.promise();
} else if (method === 'delete') {
var dfd = $.Deferred();
if (model.__type__ == 'Document') {
this._datasetAsData.data.rows = _.reject(this._datasetAsData.data.rows, function(row) {
return (row.id === model.id);
});
dfd.resolve(model);
}
return dfd.promise();
} else { } else {
alert('Not supported: sync on BackendMemory with method ' + method + ' and model ' + model); alert('Not supported: sync on BackendMemory with method ' + method + ' and model ' + model);
} }

View File

@@ -101,6 +101,7 @@ my.DataTable = Backbone.View.extend({
_.bindAll(this, 'render'); _.bindAll(this, 'render');
this.model.currentDocuments.bind('add', this.render); this.model.currentDocuments.bind('add', this.render);
this.model.currentDocuments.bind('reset', this.render); this.model.currentDocuments.bind('reset', this.render);
this.model.currentDocuments.bind('remove', this.render);
this.state = {}; this.state = {};
// this is nasty. Due to fact that .menu element is not inside this view but is elsewhere in DOM // this is nasty. Due to fact that .menu element is not inside this view but is elsewhere in DOM
$('.menu li a').live('click', function(e) { $('.menu li a').live('click', function(e) {
@@ -161,21 +162,20 @@ my.DataTable = Backbone.View.extend({
if (confirm(msg)) costco.deleteColumn(self.state.currentColumn); if (confirm(msg)) costco.deleteColumn(self.state.currentColumn);
}, },
deleteRow: function() { deleteRow: function() {
// TODO: var doc = _.find(self.model.currentDocuments.models, function(doc) {
alert('This function needs to be re-implemented'); // important this is == as the currentRow will be string (as comes
return; // from DOM) while id may be int
var doc = _.find(app.cache, function(doc) { return doc._id === app.currentRow }); return doc.id == self.state.currentRow
doc._deleted = true; });
costco.uploadDocs([doc]).then( doc.destroy().then(function() {
function(updatedDocs) { self.model.currentDocuments.remove(doc);
util.notify("Row deleted successfully"); util.notify("Row deleted successfully");
recline.initializeTable(app.offset); })
}, .fail(function(err) {
function(err) { util.notify("Errorz! " + err) } util.notify("Errorz! " + err)
) })
} }
} }
util.hide('menu'); util.hide('menu');
actions[$(e.target).attr('data-action')](); actions[$(e.target).attr('data-action')]();
}, },

View File

@@ -27,23 +27,32 @@ test('new Dataset', function () {
}); });
recline.Model.setBackend(backend); recline.Model.setBackend(backend);
var dataset = backend.getDataset(datasetId); var dataset = backend.getDataset(datasetId);
expect(7); expect(9);
dataset.fetch().then(function(dataset) { dataset.fetch().then(function(dataset) {
equal(dataset.get('name'), metadata.name); equal(dataset.get('name'), metadata.name);
equal(dataset.get('headers'), indata.headers); deepEqual(dataset.get('headers'), indata.headers);
equal(dataset.getLength(), 6); equal(dataset.getLength(), 6);
dataset.getRows(4, 2).then(function(documentList) { dataset.getRows(4, 2).then(function(documentList) {
deepEqual(indata.rows[2], documentList.models[0].toJSON()); deepEqual(indata.rows[2], documentList.models[0].toJSON());
}); });
dataset.getRows().then(function(docList) { dataset.getRows().then(function(docList) {
// Test getRows
equal(docList.length, Math.min(10, indata.rows.length)); equal(docList.length, Math.min(10, indata.rows.length));
var doc1 = docList.models[0]; var doc1 = docList.models[0];
deepEqual(doc1.toJSON(), indata.rows[0]); deepEqual(doc1.toJSON(), indata.rows[0]);
// Test UPDATA
var newVal = 10; var newVal = 10;
doc1.set({x: newVal}); doc1.set({x: newVal});
doc1.save().then(function() { doc1.save().then(function() {
equal(backend._datasetAsData.data.rows[0].x, newVal); equal(backend._datasetAsData.data.rows[0].x, newVal);
}) })
// Test Delete
doc1.destroy().then(function() {
equal(backend._datasetAsData.data.rows.length, 5);
equal(backend._datasetAsData.data.rows[0].x, indata.rows[1].x);
});
}); });
}); });
}); });
@@ -139,7 +148,6 @@ test('Webstore Backend', function() {
} }
} }
} else { } else {
console.log('here');
return { return {
then: function(callback) { then: function(callback) {
callback(webstoreData); callback(webstoreData);