Browse Source

feat: 小于6个请求时不打包

pull/1/head
Zhenfei.Li 4 years ago
parent
commit
671d8301cf
  1. 56
      src/main/resources/com/fr/plugin/pack/req.js

56
src/main/resources/com/fr/plugin/pack/req.js

@ -7,32 +7,41 @@
var widgets = {}; var widgets = {};
var handlers = {}; var handlers = {};
var count = 0;
// 防抖机制,0.3s无新的data请求,才向后端发送。否则会先打包并等待0.3s // 防抖机制,0.3s无新的data请求,才打包向后端发送。
var requestFunc = BI.debounce(function () { var requestFunc = BI.debounce(function (config) {
BI.asyncAjax({ if (count < 6) {
url: "widgets/data", // 发送请求时小于6则不打包,还跟之前的一样发
type: "POST", BI.each(handlers, function (wId, handler) {
data: { handler.next(config);
widgets: widgets });
}, } else {
success: function (res) { BI.asyncAjax({
BI.each(handlers, function (wId, handler) { url: "widgets/data",
handler.resolve({ type: "POST",
config: config, data: {
status: 200, widgets: widgets
statusText: "success", },
headers: {"content-type": "application/json;charset=UTF-8"}, success: function (res) {
response: BI.jsonEncode(res.data[wId]) 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,
complete: BI.emptyFn, opt: {},
opt: {}, version: "url"
version: "url" });
}); }
widgets = {}; widgets = {};
handlers = {}; handlers = {};
count = 0;
}, 300); }, 300);
// 不通过改写bi组件的方式去做,工作量太大且可维护性太差。使用巧妙的办法,直接通过拦截所有data请求,将其打包成一个请求发送到后端。 // 不通过改写bi组件的方式去做,工作量太大且可维护性太差。使用巧妙的办法,直接通过拦截所有data请求,将其打包成一个请求发送到后端。
@ -45,7 +54,8 @@
var wId = widget.wId; var wId = widget.wId;
widgets[wId] = widget; widgets[wId] = widget;
handlers[wId] = handler; handlers[wId] = handler;
requestFunc(); count++;
requestFunc(config);
} else { } else {
handler.next(config); handler.next(config);
} }

Loading…
Cancel
Save