From 532543395a63ebda0c189ee42b459fa4daeb1cc9 Mon Sep 17 00:00:00 2001 From: guy Date: Tue, 7 Dec 2021 19:22:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81BI.useContext?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/useContext.html | 94 ++++++++++++++++++++++++++++++++++++++++ src/core/4.widget.js | 36 +++++++++++++-- 2 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 examples/useContext.html diff --git a/examples/useContext.html b/examples/useContext.html new file mode 100644 index 000000000..8762ba34c --- /dev/null +++ b/examples/useContext.html @@ -0,0 +1,94 @@ + + + + + + + + + +
+ + + diff --git a/src/core/4.widget.js b/src/core/4.widget.js index c104a03c5..fcd71aab3 100644 --- a/src/core/4.widget.js +++ b/src/core/4.widget.js @@ -768,10 +768,40 @@ } }; - BI.watch = function (watch, handler) { + BI.useContext = function () { + return BI.Model.target; + }; + + BI.watch = function (vm, watch, handler, options) { if (BI.Widget.current) { - BI.Widget.current.$watchDelayCallbacks || (BI.Widget.current.$watchDelayCallbacks = []); - BI.Widget.current.$watchDelayCallbacks.push([watch, handler]); + options = options || {}; + if (vm instanceof BI.Model) { + BI.Widget.current._watchers || (BI.Widget.current._watchers = []); + if (BI.isKey(watch)) { + var k = watch; + watch = {}; + watch[k] = 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(Fix.watch(vm.model, key, handler, BI.extend(options, { + store: vm.store + }))); + } + } else { + BI.Widget.current._watchers.push(Fix.watch(vm.model, key, handler, BI.extend(options, { + store: vm.store + }))); + } + } + } else { + handler = watch; + watch = vm; + BI.Widget.current.$watchDelayCallbacks || (BI.Widget.current.$watchDelayCallbacks = []); + BI.Widget.current.$watchDelayCallbacks.push([watch, handler]); + } } };