Browse Source

支持BI.useContext

es6
guy 3 years ago
parent
commit
532543395a
  1. 94
      examples/useContext.html
  2. 32
      src/core/4.widget.js

94
examples/useContext.html

@ -0,0 +1,94 @@
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- <link rel="stylesheet" type="text/css" href="../dist/2.0/fineui.min.css"/>
<script src="../dist/2.0/fineui.js"></script> -->
<link rel="stylesheet" type="text/css" href="http://fanruan.design/fineui/2.0/fineui.min.css"/>
<script src="http://fanruan.design/fineui/2.0/fineui.min.js"></script>
</head>
<body>
<div id="wrapper"></div>
<script>
// tab上下文环境测试
var Model = BI.inherit(BI.Model, {
state: function () {
return {
expand: false
};
},
childContext: ["text"],
computed: {
text: function () {
return this.model.expand ? "text-yes" : "text-not";
}
},
actions: {
toggle: function () {
this.model.expand = !this.model.expand;
}
}
});
BI.model("demo.model", Model);
var Child = BI.inherit(BI.Widget, {
render: function () {
var label;
var context = BI.useContext();
setInterval(function () {
context.toggle();
}, 1000);
BI.watch(context, "expand", function () {
label.setText(context.model.text);
});
return {
type: "bi.label",
ref: function (_ref) {
label = _ref;
},
text: function () {
return context.model.text;
}
};
}
});
BI.shortcut("demo.child", Child);
var Widget = BI.inherit(BI.Widget, {
_store: function () {
return BI.Models.getModel("demo.model");
},
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"
});
</script>
</body>
</html>

32
src/core/4.widget.js

@ -768,11 +768,41 @@
}
};
BI.watch = function (watch, handler) {
BI.useContext = function () {
return BI.Model.target;
};
BI.watch = function (vm, watch, handler, options) {
if (BI.Widget.current) {
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]);
}
}
};
BI.onBeforeMount = function (beforeMount) {

Loading…
Cancel
Save