[#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) {
// deep copy
this._datasetAsData = _.extend({}, dataset);
this._datasetAsData = $.extend(true, {}, dataset);
_.bindAll(this, 'sync');
},
getDataset: function() {
@ -118,6 +118,15 @@ my.BackendMemory = Backbone.Model.extend({
dfd.resolve(model);
}
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 {
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');
this.model.currentDocuments.bind('add', this.render);
this.model.currentDocuments.bind('reset', this.render);
this.model.currentDocuments.bind('remove', this.render);
this.state = {};
// 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) {
@ -161,21 +162,20 @@ my.DataTable = Backbone.View.extend({
if (confirm(msg)) costco.deleteColumn(self.state.currentColumn);
},
deleteRow: function() {
// TODO:
alert('This function needs to be re-implemented');
return;
var doc = _.find(app.cache, function(doc) { return doc._id === app.currentRow });
doc._deleted = true;
costco.uploadDocs([doc]).then(
function(updatedDocs) {
var doc = _.find(self.model.currentDocuments.models, function(doc) {
// important this is == as the currentRow will be string (as comes
// from DOM) while id may be int
return doc.id == self.state.currentRow
});
doc.destroy().then(function() {
self.model.currentDocuments.remove(doc);
util.notify("Row deleted successfully");
recline.initializeTable(app.offset);
},
function(err) { util.notify("Errorz! " + err) }
)
})
.fail(function(err) {
util.notify("Errorz! " + err)
})
}
}
util.hide('menu');
actions[$(e.target).attr('data-action')]();
},

View File

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