Browse Source

Pull request #1824: 无JIRA任务 优化一下context的传递

Merge in VISUAL/fineui from ~GUY/fineui:master to master

* commit '1dd49715d4039eabc66e8a86167761ec3100f1ae':
  优化一下context的传递
  优化一下context的传递
  Revert "优化一下context的传递"
  优化一下context的传递
  优化一下context的传递
  优化一下context的传递
es6
guy 4 years ago
parent
commit
d0bc2a2873
  1. 19
      dist/fix/fix.compact.ie.js
  2. 24
      dist/fix/fix.compact.js
  3. 2
      examples/dev.html
  4. 118
      examples/tab-context.html
  5. 4
      src/core/base.js
  6. 5
      src/core/shortcut.js
  7. 7
      src/core/widget.js

19
dist/fix/fix.compact.ie.js vendored

@ -82,7 +82,7 @@
if (p instanceof Fix.Model || p.store || p.__cacheStore) { if (p instanceof Fix.Model || p.store || p.__cacheStore) {
break; break;
} }
p = p._parent || (p.options && p.options.element); p = p._context || p._parent || (p.options && p.options.element);
} }
if (p) { if (p) {
if (p instanceof Fix.Model) { if (p instanceof Fix.Model) {
@ -93,21 +93,6 @@
} }
} }
// 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;
// };
_.each(["populate", "addItems", "prependItems"], function (name) { _.each(["populate", "addItems", "prependItems"], function (name) {
var old = BI.Loader.prototype[name]; var old = BI.Loader.prototype[name];
BI.Loader.prototype[name] = function () { BI.Loader.prototype[name] = function () {
@ -121,7 +106,7 @@
function createStore () { function createStore () {
var needPop = false; var needPop = false;
if (_global.Fix && this._store) { if (_global.Fix && this._store) {
var store = findStore(this.options.context || this._parent || this.options.element); var store = findStore(this.options.context || this._context || this._parent || this.options.element);
if (store) { if (store) {
pushTarget(store); pushTarget(store);
needPop = true; needPop = true;

24
dist/fix/fix.compact.js vendored

@ -86,7 +86,7 @@
if (p instanceof Fix.Model || p.store || p.__cacheStore) { if (p instanceof Fix.Model || p.store || p.__cacheStore) {
break; break;
} }
p = p._parent || (p.options && p.options.element); p = p._context || p._parent || (p.options && p.options.element);
} }
if (p) { if (p) {
if (p instanceof Fix.Model) { if (p instanceof Fix.Model) {
@ -97,26 +97,6 @@
} }
} }
// 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;
// };
_.each(["populate", "addItems", "prependItems"], function (name) { _.each(["populate", "addItems", "prependItems"], function (name) {
var old = BI.Loader.prototype[name]; var old = BI.Loader.prototype[name];
BI.Loader.prototype[name] = function () { BI.Loader.prototype[name] = function () {
@ -134,7 +114,7 @@
function createStore () { function createStore () {
var needPop = false; var needPop = false;
if (_global.Fix && this._store) { if (_global.Fix && this._store) {
var store = findStore(this.options.context || this._parent || this.options.element); var store = findStore(this.options.context || this._context || this._parent || this.options.element);
if (store) { if (store) {
pushTarget(store); pushTarget(store);
needPop = true; needPop = true;

2
examples/dev.html

@ -73,7 +73,7 @@
store.toggle(); store.toggle();
}, 1000); }, 1000);
BI.watch("text", function () { BI.watch("text", function () {
child.populate(); child.reset();
}); });
return function () { return function () {
return { return {

118
examples/tab-context.html

@ -0,0 +1,118 @@
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="../dist/2.0/fineui.css"/>
<script src="../dist/2.0/fineui.js"></script>
</head>
<body>
<div id="wrapper"></div>
<script>
// tab上下文环境测试
var Model = BI.inherit(Fix.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 ChildModel = BI.inherit(Fix.Model, {
context: ["text"]
});
BI.model("demo.child_model", ChildModel);
var Child = BI.inherit(BI.Widget, {
setup: function () {
var store = BI.useStore(function () {
return BI.Models.getModel("demo.child_model");
});
return {
render: function () {
return {
type: "bi.label",
text: store.model.text
};
}
};
}
});
BI.shortcut("demo.child", Child);
var Widget = BI.inherit(BI.Widget, {
props: {
updateMode: "auto"
},
setup: function () {
var child;
var store = BI.useStore(function () {
return BI.Models.getModel("demo.model");
});
setInterval(function () {
store.toggle();
}, 1000);
BI.watch("text", function () {
// child.reset();
});
return function () {
return {
type: "bi.vertical",
vgap: 20,
items: [{
type: "demo.child",
ref: function (_ref) {
child = _ref;
}
}, {
type: "bi.combo",
el: {
type: "bi.button",
text: "点击"
},
popup: {
el: {
type: "bi.tab",
height: 100,
showIndex: 0,
cardCreator: function () {
return {
type: "demo.child"
};
}
}
}
}]
};
};
}
});
BI.shortcut("demo.parent", Widget);
BI.createWidget({
type: "bi.absolute",
items: [{
el: {
type: "demo.parent"
},
top: 100,
left: 100
}],
element: "#wrapper"
});
</script>
</body>
</html>

4
src/core/base.js

@ -70,7 +70,7 @@ if (!_global.BI) {
}, },
isWidget: function (widget) { isWidget: function (widget) {
return widget instanceof BI.Widget || (BI.View && widget instanceof BI.View); return widget instanceof BI.Widget;
}, },
createWidgets: function (items, options, context) { createWidgets: function (items, options, context) {
@ -107,7 +107,7 @@ if (!_global.BI) {
el: innerAttr.shift() el: innerAttr.shift()
}); });
} }
if (item.el instanceof BI.Widget || (BI.View && item.el instanceof BI.View)) { if (item.el instanceof BI.Widget) {
innerAttr.shift(); innerAttr.shift();
return BI.extend({}, outerAttr.shift(), { type: null }, item); return BI.extend({}, outerAttr.shift(), { type: null }, item);
} }

5
src/core/shortcut.js

@ -18,11 +18,12 @@
throw new Error("组件" + config.type + "未定义"); throw new Error("组件" + config.type + "未定义");
} }
var pushed = false; var pushed = false;
if (context) { var widget = new cls();
widget._context = BI.Widget.context || context;
if (!BI.Widget.context && context) {
pushed = true; pushed = true;
BI.Widget.pushContext(context); BI.Widget.pushContext(context);
} }
var widget = new cls();
widget._initProps(config); widget._initProps(config);
widget._constructed(); widget._constructed();
widget._initRoot(); widget._initRoot();

7
src/core/widget.js

@ -152,16 +152,9 @@
this._children = {}; this._children = {};
if (BI.isWidget(o.element)) { if (BI.isWidget(o.element)) {
this.element = this.options.element.element; this.element = this.options.element.element;
if (o.element instanceof BI.Widget) {
this._parent = o.element; this._parent = o.element;
this._parent.addWidget(this.widgetName, this); this._parent.addWidget(this.widgetName, this);
} else {
this._isRoot = true;
}
} else if (o.element) { } else if (o.element) {
// if (o.root !== true) {
// throw new Error("root is a required property");
// }
this.element = BI.Widget._renderEngine.createElement(this); this.element = BI.Widget._renderEngine.createElement(this);
this._isRoot = true; this._isRoot = true;
} else { } else {

Loading…
Cancel
Save