我正在使用ngGrid顯示源自數據庫的數據。使用ngGrid文檔中描述的方法,我可以在網格中將更改寫回數據庫。但是,如果db響應寫入錯誤,我一直無法找到恢復到原始值的方法。如果數據庫寫入失敗,則恢復爲ngGrid中的原始單元格值
下面是我使用的電池模板和更新功能:
var cellEditableTemplate = "<input ng-class=\"'colt' + col.index\" ng-input=\"COL_FIELD\" ng-model=\"COL_FIELD\" ng-blur=\"updateEntity(col, row, COL_FIELD)\" />";
$scope.updateEntity = function(column, row, cellValue){
console.log(row.entity);
console.log(column.field);
console.log('Cell Value prior: ' + row.entity[column.field]);
console.log('Cell Value after: ' + cellValue);
//Trying to cache original value, i know this is wrong.
var fail = row.entity[column.field];
//row.entity[column.field] = cellValue;
var map = {};
map[column.field] = cellValue;
//Code to prevent multiple ngBlur events
//http://technpol.wordpress.com/2013/12/06/editable-nggrid-with-both-dropdowns-and-selects/
if(!$scope.save) {
$scope.save = {promise: null, pending: false, data: null};
}
$scope.save.data = map;
if(!$scope.save.pending){
$scope.save.pending = true;
$scope.save.promise = $timeout(function(){
//DB Write Function
$scope.update(row.entity.id, $scope.save.data).then(
function (result){
//DB Write Success
//Ensure new value is written to scope
$scope.log("Table Updated", result);
row.entity[column.field] = cellValue;
}, function (error){
//DB Write Failure
//Need to revert to original value
$scope.log("Error updating value", error);
row.entity[column.field] = fail; //I know this doesn't work!
});
$scope.save.pending = false;
}, 500);
}
};
這段代碼的審查清楚地表明,設置NG-模型在模板中COL_FIELD意味着每次更新單元格的值立即應用於示波器。所以我需要的是在任何編輯發生之前緩存原始值的方法。
對此提出建議?