From b9b518df7d7c56c046ca608ec47ef19c4d91b68e Mon Sep 17 00:00:00 2001 From: guy Date: Sat, 10 Oct 2020 14:06:16 +0800 Subject: [PATCH] Composition API --- dist/fix/fix.compact.ie.js | 21 ++++++++++++++++++++- dist/fix/fix.compact.js | 23 +++++++++++++++++++++-- src/core/widget.js | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/dist/fix/fix.compact.ie.js b/dist/fix/fix.compact.ie.js index 5891bfdd6..f0de3da58 100644 --- a/dist/fix/fix.compact.ie.js +++ b/dist/fix/fix.compact.ie.js @@ -101,6 +101,26 @@ return result; }; + BI.watch = function (watch, handler) { + if (BI.Widget.current) { + if (BI.isKey(watch)) { + var key = watch; + watch = {}; + watch[key] = handler; + } + for (var key in watch) { + var handler = watch[key]; + if (BI.isArray(handler)) { + for (var i = 0; i < handler.length; i++) { + BI.Widget.current._watchers.push(createWatcher(BI.Widget.current, key, handler[i])); + } + } else { + BI.Widget.current._watchers.push(createWatcher(BI.Widget.current, key, handler)); + } + } + } + }; + _.each(["populate", "addItems", "prependItems"], function (name) { var old = BI.Loader.prototype[name]; BI.Loader.prototype[name] = function () { @@ -324,5 +344,4 @@ return _.cloneDeep(obj); } } - BI.watch = Fix.watch; }()); diff --git a/dist/fix/fix.compact.js b/dist/fix/fix.compact.js index 6025117c0..97c209e22 100644 --- a/dist/fix/fix.compact.js +++ b/dist/fix/fix.compact.js @@ -13,7 +13,7 @@ } } - function createWatcher(vm, keyOrFn, cb, options) { + function createWatcher (vm, keyOrFn, cb, options) { if (BI.isPlainObject(cb)) { options = cb; cb = cb.handler; @@ -110,6 +110,26 @@ return result; }; + BI.watch = function (watch, handler) { + if (BI.Widget.current) { + if (BI.isKey(watch)) { + var key = watch; + watch = {}; + watch[key] = handler; + } + for (var key in watch) { + var handler = watch[key]; + if (BI.isArray(handler)) { + for (var i = 0; i < handler.length; i++) { + BI.Widget.current._watchers.push(createWatcher(BI.Widget.current, key, handler[i])); + } + } else { + BI.Widget.current._watchers.push(createWatcher(BI.Widget.current, key, handler)); + } + } + } + }; + _.each(["populate", "addItems", "prependItems"], function (name) { var old = BI.Loader.prototype[name]; BI.Loader.prototype[name] = function () { @@ -280,5 +300,4 @@ return Fix.toJSON(ob); }; } - BI.watch = Fix.watch; }()); diff --git a/src/core/widget.js b/src/core/widget.js index 78d5e7646..12556ff13 100644 --- a/src/core/widget.js +++ b/src/core/widget.js @@ -164,6 +164,11 @@ _initElement: function () { var self = this; var els = this.render && this.render(); + if (!els) { + pushTarget(this); + els = this.setup && this.setup(); + popTarget(); + } if (BI.isPlainObject(els)) { els = [els]; } @@ -492,6 +497,39 @@ this.purgeListeners(); } }); + var context = null; + var contextStack = []; + + function pushTarget (_context) { + if (context) contextStack.push(context); + BI.Widget.current = context = _context; + } + + function popTarget () { + BI.Widget.current = context = contextStack.pop(); + } + + BI.onBeforeMount = function (beforeMount) { + if (context) { + context.beforeMount = beforeMount; + } + }; + BI.onMounted = function (mounted) { + if (context) { + context.mounted = mounted; + } + }; + BI.onBeforeUnmount = function (beforeDestroy) { + if (context) { + context.beforeDestroy = beforeDestroy; + } + }; + BI.onUnmounted = function (destroyed) { + if (context) { + context.destroyed = destroyed; + } + }; + BI.Widget.registerRenderEngine = function (engine) { BI.Widget._renderEngine = engine; };