diff --git a/changelog.md b/changelog.md
index 38b9c7fcd..3c1fc91be 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,6 @@
# 更新日志
2.0(2021-12)
+- 支持BI.useContext获取上下文环境
- BI.Msg.alert支持message传json格式
- 支持BI.config(function(){})进行系统配置
diff --git a/examples/dev.html b/examples/dev.html
index 17bfd951b..167c18ac5 100644
--- a/examples/dev.html
+++ b/examples/dev.html
@@ -1,419 +1,97 @@
-
-
-
-
- PullRequest | Code Review as a Service
-
-
-
-
+
+
+
+
+
+
+
+
+
-
+ setup: function () {
+ var child;
+ var store = BI.useStore();
+ return function () {
+ return {
+ type: "bi.vertical",
+ vgap: 20,
+ items: [{
+ type: "demo.child",
+ ref: function (_ref) {
+ child = _ref;
+ }
+ }]
+ };
+ };
+ }
+ });
+ BI.shortcut("demo.parent", Widget);
+ BI.createWidget({
+ type: "bi.absolute",
+ items: [{
+ el: {
+ type: "demo.parent"
+ },
+ top: 100,
+ left: 100
+ }],
+ element: "#wrapper"
+ });
+
+
diff --git a/examples/useContext.html b/examples/useContext.html
new file mode 100644
index 000000000..56c78b2ce
--- /dev/null
+++ b/examples/useContext.html
@@ -0,0 +1,97 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/core/4.widget.js b/src/core/4.widget.js
index c104a03c5..f8fe45929 100644
--- a/src/core/4.widget.js
+++ b/src/core/4.widget.js
@@ -768,8 +768,40 @@
}
};
- BI.watch = function (watch, handler) {
+ BI.useContext = function () {
+ return BI.Model.target;
+ };
+
+ BI.watch = function (vm, watch, handler) {
+ if (vm instanceof BI.Model) {
+ var watchers = [];
+ if (BI.isKey(watch)) {
+ var k = watch;
+ watch = {};
+ watch[k] = handler;
+ }
+ for (var key in watch) {
+ var innerHandler = watch[key];
+ if (BI.isArray(handler)) {
+ for (var i = 0; i < handler.length; i++) {
+ watchers.push(Fix.watch(vm.model, key, innerHandler, {
+ store: vm
+ }));
+ }
+ } else {
+ watchers.push(Fix.watch(vm.model, key, innerHandler, {
+ store: vm
+ }));
+ }
+ }
+ // vm中一定有_widget
+ vm._widget._watchers || (vm._widget._watchers = []);
+ vm._widget._watchers = vm._widget._watchers.concat(watchers);
+ return;
+ }
if (BI.Widget.current) {
+ handler = watch;
+ watch = vm;
BI.Widget.current.$watchDelayCallbacks || (BI.Widget.current.$watchDelayCallbacks = []);
BI.Widget.current.$watchDelayCallbacks.push([watch, handler]);
}