From 5a17741692fc3a468483576844f73ecb21f01cb1 Mon Sep 17 00:00:00 2001 From: guy Date: Sun, 11 Oct 2020 22:40:07 +0800 Subject: [PATCH] =?UTF-8?q?context=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/fix/fix.compact.ie.js | 44 +++++++++++-------------------- dist/fix/fix.compact.js | 54 +++++++++++++++----------------------- src/core/shortcut.js | 3 ++- src/core/widget.js | 35 +++++++++++++++--------- 4 files changed, 61 insertions(+), 75 deletions(-) diff --git a/dist/fix/fix.compact.ie.js b/dist/fix/fix.compact.ie.js index f0de3da58..eafa4ab5d 100644 --- a/dist/fix/fix.compact.ie.js +++ b/dist/fix/fix.compact.ie.js @@ -36,18 +36,6 @@ Fix.Model.target = target = targetStack.pop(); } - var context = null; - var contextStack = []; - - function pushContext(_context) { - if (context) contextStack.push(context); - Fix.Model.context = context = _context; - } - - function popContext() { - Fix.Model.context = context = contextStack.pop(); - } - var oldWatch = Fix.watch; Fix.watch = function (model, expOrFn, cb, options) { if (BI.isPlainObject(cb)) { @@ -86,20 +74,20 @@ } } - var _create = BI.createWidget; - BI.createWidget = function (item, options, context) { - var pushed = false; - if (BI.isWidget(options)) { - pushContext(options); - pushed = true; - } else if (context != null) { - pushContext(context); - pushed = true; - } - var result = _create.apply(this, arguments); - pushed && popContext(); - return result; - }; + // var _create = BI.createWidget; + // BI.createWidget = function (item, options, context) { + // var pushed = false; + // if (BI.isWidget(options)) { + // pushContext(options); + // pushed = true; + // } else if (context != null) { + // pushContext(context); + // pushed = true; + // } + // var result = _create.apply(this, arguments); + // pushed && popContext(); + // return result; + // }; BI.watch = function (watch, handler) { if (BI.Widget.current) { @@ -124,9 +112,9 @@ _.each(["populate", "addItems", "prependItems"], function (name) { var old = BI.Loader.prototype[name]; BI.Loader.prototype[name] = function () { - pushContext(this); + BI.Widget.pushContext(this); var result = old.apply(this, arguments); - popContext(); + BI.Widget.popContext(); return result; }; }); diff --git a/dist/fix/fix.compact.js b/dist/fix/fix.compact.js index 97c209e22..2b91afd57 100644 --- a/dist/fix/fix.compact.js +++ b/dist/fix/fix.compact.js @@ -36,18 +36,6 @@ Fix.Model.target = target = targetStack.pop(); } - var context = null; - var contextStack = []; - - function pushContext (_context) { - if (context) contextStack.push(context); - Fix.Model.context = context = _context; - } - - function popContext () { - Fix.Model.context = context = contextStack.pop(); - } - var oldWatch = Fix.watch; Fix.watch = function (model, expOrFn, cb, options) { if (BI.isPlainObject(cb)) { @@ -90,25 +78,25 @@ } } - var _create = BI.createWidget; - BI.createWidget = function (item, options, context) { - var pushed = false; - if (BI.isWidget(options)) { - pushContext(options); - pushed = true; - } else if (context != null) { - pushContext(context); - pushed = true; - } - var result = _create.apply(this, arguments); - // try { - // var result = _create.apply(this, arguments); - // } catch (e) { - // console.error(e); - // } - pushed && popContext(); - return result; - }; + // var _create = BI.createWidget; + // BI.createWidget = function (item, options, context) { + // var pushed = false; + // if (BI.isWidget(options)) { + // pushContext(options); + // pushed = true; + // } else if (context != null) { + // pushContext(context); + // pushed = true; + // } + // var result = _create.apply(this, arguments); + // // try { + // // var result = _create.apply(this, arguments); + // // } catch (e) { + // // console.error(e); + // // } + // pushed && popContext(); + // return result; + // }; BI.watch = function (watch, handler) { if (BI.Widget.current) { @@ -133,13 +121,13 @@ _.each(["populate", "addItems", "prependItems"], function (name) { var old = BI.Loader.prototype[name]; BI.Loader.prototype[name] = function () { - pushContext(this); + BI.Widget.pushContext(this); try { var result = old.apply(this, arguments); } catch (e) { console.error(e); } - popContext(); + BI.Widget.popContext(); return result; }; }); diff --git a/src/core/shortcut.js b/src/core/shortcut.js index ae100f5d7..b377bac9f 100644 --- a/src/core/shortcut.js +++ b/src/core/shortcut.js @@ -19,12 +19,13 @@ } var widget = new cls(); - + BI.Widget.pushContext(widget); widget._initProps(config); widget._initRoot(); if (!lazy || config.element || config.root) { widget._lazyConstructor(); } + BI.Widget.popContext(); return widget; }; diff --git a/src/core/widget.js b/src/core/widget.js index 25c7259f0..fe8f60abb 100644 --- a/src/core/widget.js +++ b/src/core/widget.js @@ -506,36 +506,45 @@ this.purgeListeners(); } }); - var context = null; - var contextStack = []; + var context = null, current = null; + var contextStack = [], currentStack = []; - function pushTarget (_context) { + BI.Widget.pushContext = function (_context) { if (context) contextStack.push(context); - BI.Widget.current = context = _context; + BI.Widget.context = context = _context; + }; + + BI.Widget.popContext = function () { + BI.Widget.context = context = contextStack.pop(); + }; + + function pushTarget (_current) { + if (current) currentStack.push(current); + BI.Widget.current = current = _current; } function popTarget () { - BI.Widget.current = context = contextStack.pop(); + BI.Widget.current = current = currentStack.pop(); } BI.onBeforeMount = function (beforeMount) { - if (context) { - context.beforeMount = beforeMount; + if (current) { + current.beforeMount = beforeMount; } }; BI.onMounted = function (mounted) { - if (context) { - context.mounted = mounted; + if (current) { + current.mounted = mounted; } }; BI.onBeforeUnmount = function (beforeDestroy) { - if (context) { - context.beforeDestroy = beforeDestroy; + if (current) { + current.beforeDestroy = beforeDestroy; } }; BI.onUnmounted = function (destroyed) { - if (context) { - context.destroyed = destroyed; + if (current) { + current.destroyed = destroyed; } };