From cae15031747af4bb7be9ad657eb1f7cc8d891068 Mon Sep 17 00:00:00 2001 From: guy Date: Thu, 27 Oct 2022 15:30:42 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-13158:=20=E5=8C=85=E8=A3=B9BI.Widget.pus?= =?UTF-8?q?hContext()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/combination/group.button.js | 12 +++++++----- src/base/combination/navigation.js | 19 +++++++++---------- src/base/combination/tab.js | 17 +++++++++-------- src/core/4.widget.js | 11 +++++++++++ 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/base/combination/group.button.js b/src/base/combination/group.button.js index 184a6d146..02b044b17 100644 --- a/src/base/combination/group.button.js +++ b/src/base/combination/group.button.js @@ -42,11 +42,13 @@ BI.ButtonGroup = BI.inherit(BI.Widget, { }, _createBtns: function (items) { - BI.Widget.pushContext(this); - var btns = BI.createWidgets(BI.createItems(items, { - type: "bi.text_button", - })); - BI.Widget.popContext(); + var btns; + BI.Widget.execWithContext(this, function () { + btns = BI.createWidgets(BI.createItems(items, { + type: "bi.text_button", + })); + }); + return btns; }, diff --git a/src/base/combination/navigation.js b/src/base/combination/navigation.js index adc3e5ad4..1b347298e 100644 --- a/src/base/combination/navigation.js +++ b/src/base/combination/navigation.js @@ -43,11 +43,10 @@ BI.Navigation = BI.inherit(BI.Widget, { return self.showIndex + v; }, cardCreator: function (v) { - BI.Widget.pushContext(self); - var card = o.cardCreator(v); - BI.Widget.popContext(); - self.cardMap[v] = card; - return card; + BI.Widget.execWithContext(self, function () { + self.cardMap[v] = o.cardCreator(v); + }); + return self.cardMap[v]; }, afterCardCreated: BI.bind(this.afterCardCreated, this), afterCardShow: BI.bind(this.afterCardShow, this), @@ -104,12 +103,12 @@ BI.Navigation = BI.inherit(BI.Widget, { }, _assertCard: function (v) { + var self = this, o = this.options; if (!this.layout.isCardExisted(v)) { - BI.Widget.pushContext(this); - var card = this.options.cardCreator(v); - BI.Widget.popContext(); - this.cardMap[v] = card; - this.layout.addCardByName(v, card); + BI.Widget.execWithContext(self, function () { + self.cardMap[v] = o.cardCreator(v); + }); + this.layout.addCardByName(v, this.cardMap[v]); this.afterCardCreated(v); } }, diff --git a/src/base/combination/tab.js b/src/base/combination/tab.js index 85fca3483..9fe862884 100644 --- a/src/base/combination/tab.js +++ b/src/base/combination/tab.js @@ -43,10 +43,11 @@ BI.Tab = BI.inherit(BI.Widget, { eventObj: this.tab, cardLayout: this.layout, cardCreator: function (v) { - var card = o.cardCreator.apply(self, arguments); - self.cardMap[v] = card; + BI.Widget.execWithContext(self, function () { + self.cardMap[v] = o.cardCreator.apply(self, arguments); + }); - return card; + return self.cardMap[v]; }, afterCardShow: function (v) { self._deleteOtherCards(v); @@ -71,12 +72,12 @@ BI.Tab = BI.inherit(BI.Widget, { }, _assertCard: function (v) { + var self = this, o = this.options; if (!this.layout.isCardExisted(v)) { - BI.Widget.pushContext(this); - var card = this.options.cardCreator(v); - BI.Widget.popContext(); - this.cardMap[v] = card; - this.layout.addCardByName(v, card); + BI.Widget.execWithContext(this, function () { + self.cardMap[v] = o.cardCreator.apply(self, arguments); + }); + this.layout.addCardByName(v, this.cardMap[v]); } }, diff --git a/src/core/4.widget.js b/src/core/4.widget.js index ab85900ff..ce3747adc 100644 --- a/src/core/4.widget.js +++ b/src/core/4.widget.js @@ -857,6 +857,17 @@ BI.Widget.context = context = contextStack.pop(); }; + BI.Widget.execWithContext = function (context, execFunc) { + BI.Widget.pushContext(context); + try { + execFunc(); + } catch (e) { + throw e; + } finally { + BI.Widget.popContext(); + } + }; + function pushTarget(_current) { if (current) currentStack.push(current); BI.Widget.current = current = _current;