From bb4496fbe1ea212bf55199e4a7c3bc23a5ec9882 Mon Sep 17 00:00:00 2001 From: Denys Rul <drul@magento.com> Date: Thu, 17 Nov 2016 14:57:49 +0200 Subject: [PATCH] MAGETWO-59322: Incorrect scope filter caching in UI grids - Drop cache when certain request parameters change --- .../ui_component/product_listing.xml | 3 ++ .../Ui/view/base/web/js/grid/data-storage.js | 39 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/view/adminhtml/ui_component/product_listing.xml b/app/code/Magento/Catalog/view/adminhtml/ui_component/product_listing.xml index ec88952af50..caca8151ec8 100644 --- a/app/code/Magento/Catalog/view/adminhtml/ui_component/product_listing.xml +++ b/app/code/Magento/Catalog/view/adminhtml/ui_component/product_listing.xml @@ -23,6 +23,9 @@ <item name="config" xsi:type="array"> <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item> <item name="update_url" xsi:type="url" path="mui/index/render"/> + <item name="storageConfig" xsi:type="array"> + <item name="dataNamespace" xsi:type="string">filters.store_id</item> + </item> </item> </argument> </argument> diff --git a/app/code/Magento/Ui/view/base/web/js/grid/data-storage.js b/app/code/Magento/Ui/view/base/web/js/grid/data-storage.js index dca12f832cd..3d0828b3b1c 100644 --- a/app/code/Magento/Ui/view/base/web/js/grid/data-storage.js +++ b/app/code/Magento/Ui/view/base/web/js/grid/data-storage.js @@ -20,6 +20,7 @@ define([ method: 'GET', dataType: 'json' }, + dataNamespace: '', data: {} }, @@ -29,8 +30,16 @@ define([ * @returns {DataStorage} Chainable. */ initConfig: function () { + var namespace; + this._super(); + namespace = this.dataNamespace; + + if (typeof namespace === 'string') { + this.dataNamespace = namespace ? [namespace] : []; + } + this._requests = []; return this; @@ -77,10 +86,12 @@ define([ * @returns {jQueryPromise} */ getData: function (params, options) { - var cachedRequest = this.getRequest(params); + var cachedRequest; - if (params && params.filters && params.filters['store_id']) { - cachedRequest = false; + if (this.hasNamespaceChanged(params)) { + this.clearRequests(); + } else { + cachedRequest = this.getRequest(params); } options = options || {}; @@ -90,6 +101,28 @@ define([ this.requestData(params); }, + /** + * Tells whether one of the parameters defined in the "dataNamespace" has + * changed since the last request. + * + * @param {Object} params - Request parameters. + * @returns {Boolean} + */ + hasNamespaceChanged: function (params) { + var lastRequest = _.last(this._requests), + paths, + diff; + + if (!lastRequest) { + return false; + } + + diff = utils.compare(lastRequest.params, params); + paths = _.pluck(diff.changes, 'path').concat(_.keys(diff.containers)); + + return _.intersection(this.dataNamespace, paths).length > 0; + }, + /** * Extends records of current data object * with the provided records collection. -- GitLab