[#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:
11
src/model.js
11
src/model.js
@@ -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);
|
||||
}
|
||||
|
||||
24
src/view.js
24
src/view.js
@@ -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')]();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user