From 86f9bba5d2b7182e694a08cc928643175728dd25 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Mon, 20 Nov 2017 15:57:27 +0530 Subject: [PATCH] - Fix Datamanager.init - Revert oldValue when setValue promise fails --- lib/frappe-datatable.js | 18 ++++++++++++------ src/cellmanager.js | 15 +++++++++------ src/datamanager.js | 5 ++++- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/frappe-datatable.js b/lib/frappe-datatable.js index aa8143e..dd05adc 100644 --- a/lib/frappe-datatable.js +++ b/lib/frappe-datatable.js @@ -962,14 +962,17 @@ var CellManager = function () { if (editing) { var value = editing.getValue(); var done = editing.setValue(value); + var oldValue = this.getCell(colIndex, rowIndex).content; + + // update cell immediately + this.updateCell(rowIndex, colIndex, value); if (done && done.then) { - // wait for promise then update internal state - done.then(function () { - return _this7.updateCell(rowIndex, colIndex, value); + // revert to oldValue if promise fails + done.catch(function (e) { + console.log(e); + _this7.updateCell(rowIndex, colIndex, oldValue); }); - } else { - this.updateCell(rowIndex, colIndex, value); } } } @@ -1732,7 +1735,6 @@ var DataManager = function () { _classCallCheck(this, DataManager); this.options = options; - this.rowCount = 0; this.currentSort = { sortBy: -1, // colIndex sortOrder: 'none' // asc, desc, none @@ -1746,6 +1748,10 @@ var DataManager = function () { rows = data.rows; + this.rowCount = 0; + this.columns = []; + this.rows = []; + this.columns = this.prepareColumns(columns); this.rows = this.prepareRows(rows); diff --git a/src/cellmanager.js b/src/cellmanager.js index cb123b6..f8590e9 100644 --- a/src/cellmanager.js +++ b/src/cellmanager.js @@ -412,14 +412,17 @@ export default class CellManager { if (editing) { const value = editing.getValue(); const done = editing.setValue(value); + const oldValue = this.getCell(colIndex, rowIndex).content; + + // update cell immediately + this.updateCell(rowIndex, colIndex, value); if (done && done.then) { - // wait for promise then update internal state - done.then( - () => this.updateCell(rowIndex, colIndex, value) - ); - } else { - this.updateCell(rowIndex, colIndex, value); + // revert to oldValue if promise fails + done.catch((e) => { + console.log(e); + this.updateCell(rowIndex, colIndex, oldValue); + }); } } } diff --git a/src/datamanager.js b/src/datamanager.js index 0b4c726..ec48a7f 100644 --- a/src/datamanager.js +++ b/src/datamanager.js @@ -3,7 +3,6 @@ import { isNumeric } from './utils'; export default class DataManager { constructor(options) { this.options = options; - this.rowCount = 0; this.currentSort = { sortBy: -1, // colIndex sortOrder: 'none' // asc, desc, none @@ -13,6 +12,10 @@ export default class DataManager { init(data) { let { columns, rows } = data; + this.rowCount = 0; + this.columns = []; + this.rows = []; + this.columns = this.prepareColumns(columns); this.rows = this.prepareRows(rows);