From 671d8301cfe9123a22cff9339208aec7153338a7 Mon Sep 17 00:00:00 2001 From: "Zhenfei.Li" Date: Mon, 19 Oct 2020 17:00:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=8F=E4=BA=8E6=E4=B8=AA=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=97=B6=E4=B8=8D=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/com/fr/plugin/pack/req.js | 56 ++++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/main/resources/com/fr/plugin/pack/req.js b/src/main/resources/com/fr/plugin/pack/req.js index 4c0377e..e1c81c1 100644 --- a/src/main/resources/com/fr/plugin/pack/req.js +++ b/src/main/resources/com/fr/plugin/pack/req.js @@ -7,32 +7,41 @@ var widgets = {}; var handlers = {}; + var count = 0; - // 防抖机制,0.3s无新的data请求,才向后端发送。否则会先打包并等待0.3s - var requestFunc = BI.debounce(function () { - BI.asyncAjax({ - url: "widgets/data", - type: "POST", - data: { - widgets: widgets - }, - success: function (res) { - BI.each(handlers, function (wId, handler) { - handler.resolve({ - config: config, - status: 200, - statusText: "success", - headers: {"content-type": "application/json;charset=UTF-8"}, - response: BI.jsonEncode(res.data[wId]) + // 防抖机制,0.3s无新的data请求,才打包向后端发送。 + var requestFunc = BI.debounce(function (config) { + if (count < 6) { + // 发送请求时小于6则不打包,还跟之前的一样发 + BI.each(handlers, function (wId, handler) { + handler.next(config); + }); + } else { + BI.asyncAjax({ + url: "widgets/data", + type: "POST", + data: { + widgets: widgets + }, + success: function (res) { + BI.each(handlers, function (wId, handler) { + handler.resolve({ + config: config, + status: 200, + statusText: "success", + headers: {"content-type": "application/json;charset=UTF-8"}, + response: BI.jsonEncode(res.data[wId]) + }); }); - }); - }, - complete: BI.emptyFn, - opt: {}, - version: "url" - }); + }, + complete: BI.emptyFn, + opt: {}, + version: "url" + }); + } widgets = {}; handlers = {}; + count = 0; }, 300); // 不通过改写bi组件的方式去做,工作量太大且可维护性太差。使用巧妙的办法,直接通过拦截所有data请求,将其打包成一个请求发送到后端。 @@ -45,7 +54,8 @@ var wId = widget.wId; widgets[wId] = widget; handlers[wId] = handler; - requestFunc(); + count++; + requestFunc(config); } else { handler.next(config); }