Related with bug 25811: Prevent multiple requests refreshing with selected row
authorAugusto Mauch <augusto.mauch@openbravo.com>
Tue, 11 Nov 2014 18:46:35 +0100
changeset 25270 e6f0909cf6a4
parent 25269 c51dce7e9fd3
child 25271 c3491ae284fe
Related with bug 25811: Prevent multiple requests refreshing with selected row

Now when a grid is refreshed by pushing the refresh toolbar button while having a record selected, there will be no unneded requests. Also, instead of using targetRecordId (which results in bad performance), provide a range that will most probably contain the record after refreshing.

Technical aspects:
- While the grid is being refreshed with a selected record, do not allow to refresh its body. The redrawal would try to obtain the record 0, would not find it (as the refresh needs to invalidate the cache), and make and extra request to obtain it.
- The range starRow, endRow is calculated by obtaining the record index and adding and substracting half of the data page size.
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js	Tue Nov 11 18:33:42 2014 +0100
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-grid.js	Tue Nov 11 18:46:35 2014 +0100
@@ -1376,6 +1376,11 @@
 isc.OBViewGridBody.addProperties({
   redraw: function () {
     var newDrawArea, grid, drawArea, firstRecord, loading;
+    // Dont redraw while the grid is being refreshed with a selected record to 
+    // prevent unneeded requests. See issue https://issues.openbravo.com/view.php?id=25811
+    if (this.grid.refreshingWithSelectedRecord) {
+      return;
+    }
 
     this.Super('redraw', arguments);
 
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Tue Nov 11 18:33:42 2014 +0100
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/grid/ob-view-grid.js	Tue Nov 11 18:46:35 2014 +0100
@@ -206,10 +206,19 @@
       // clone to prevent side effects
       var requestProperties = isc.clone(this.context);
       this.context.params = this.grid.getFetchRequestParams(requestProperties.params);
-
+      if (this.grid.refreshingWithSelectedRecord) {
+        // if the grid was refreshed with a record selected, use the range that contained that record 
+        //  instead of using targetRecordId to improve the performance
+        startRow = this.grid.selectedRecordInitInterval;
+        endRow = this.grid.selectedRecordendInterval;
+      }
       return this.Super('fetchRemoteData', arguments);
     },
 
+    sendDSRequest: function (dsRequest) {
+      this.Super('sendDSRequest', arguments);
+    },
+
     clearLoadingMarkers: function (start, end) {
       var j;
       if (this.localData) {
@@ -1627,7 +1636,7 @@
       // so just show grid mode
       // don't need to do anything here
       delete this.targetOpenGrid;
-    } else if (this.targetRecordId) {
+    } else if (this.targetRecordId || this.selectedRecordId) {
       // direct link from other tab to a specific record
       this.delayedHandleTargetRecord(startRow, endRow);
     } else if (this.view.shouldOpenDefaultEditMode()) {
@@ -1687,7 +1696,7 @@
 
   refreshGrid: function (callback, newRecordsToBeIncluded) {
     var originalCriteria, criteria = {},
-        newRecordsCriteria, newRecordsLength, i, index;
+        newRecordsCriteria, newRecordsLength, i, index, selectedRecordIndex;
 
     //check whether newRecordsToBeIncluded contains records not part of the current grid and remove them.
     if (newRecordsToBeIncluded && newRecordsToBeIncluded.length > 0 && this.data) {
@@ -1702,8 +1711,18 @@
     }
 
     if (this.getSelectedRecord()) {
-      this.targetRecordId = this.getSelectedRecord()[OB.Constants.ID];
-      // as the record is already selected it is already in the filter
+      // this property is used to prevent an unneeded request in OBViewGridBody.redraw
+      this.refreshingWithSelectedRecord = true;
+      // obtain a range that contains the selected record
+      selectedRecordIndex = this.getRecordIndex(this.getSelectedRecord());
+      if (selectedRecordIndex !== -1) {
+        this.selectedRecordId = this.getSelectedRecord()[OB.Constants.ID];
+        this.selectedRecordInitInterval = selectedRecordIndex - Math.round(this.data.resultSize / 2);
+        if (this.selectedRecordInitInterval < 0) {
+          this.selectedRecordInitInterval = 0;
+        }
+        this.selectedRecordendInterval = this.selectedRecordInitInterval + this.data.resultSize;
+      }
       this.notRemoveFilter = true;
     }
     this.actionAfterDataArrived = callback;
@@ -1755,7 +1774,7 @@
   // with a delay to handle the target record when the body has been drawn
   delayedHandleTargetRecord: function (startRow, endRow) {
     var rowTop, recordIndex, i, data = this.data,
-        tmpTargetRecordId = this.targetRecordId;
+        tmpTargetRecordId = this.targetRecordId || this.selectedRecordId;
     if (!this.targetRecordId) {
       delete this.isOpenDirectModeLeaf;
       return;
@@ -1816,7 +1835,7 @@
 
   filterData: function (criteria, callback, requestProperties) {
     var theView = this.view,
-        newCallBack;
+        newCallBack, me = this;
 
     if (!requestProperties) {
       requestProperties = {};
@@ -1826,6 +1845,8 @@
 
     newCallBack = function () {
       theView.recordSelected();
+      delete me.refreshingWithSelectedRecord;
+      me.markForRedraw();
       if (typeof callback === 'function') {
         callback();
       }