Browse Source

生命周期mount,先把子dom节点创建完再整个一起append到父节点上去

es6
guy 4 years ago
parent
commit
fb32c9ef14
  1. 23
      dev.html
  2. 20
      src/core/widget.js

23
dev.html

@ -70,24 +70,26 @@
var store = BI.useStore(function () { var store = BI.useStore(function () {
return BI.Models.getModel("demo.model"); return BI.Models.getModel("demo.model");
}); });
setInterval(function () { // setInterval(function () {
store.toggle(); // store.toggle();
}, 1000); // }, 1000);
return function () { return function () {
return { return {
type: "bi.vertical", type: "bi.vertical",
vgap: 20, vgap: 20,
items: [{ items: BI.makeArray(10000).map(function (i) {
type: "demo.child" return {
}, { type: "bi.label",
type: "demo.child" text: i
}] };
})
}; };
}; };
} }
}); });
BI.shortcut("demo.parent", Widget); BI.shortcut("demo.parent", Widget);
BI.createWidget({ var time = performance.now();
var widget = BI.createWidget({
type: "bi.absolute", type: "bi.absolute",
items: [{ items: [{
el: { el: {
@ -97,7 +99,10 @@
left: 100 left: 100
}], }],
element: "#wrapper" element: "#wrapper"
// root: true
}); });
// widget.element.appendTo("#wrapper");
console.log(performance.now() - time);
</script> </script>
</body> </body>
</html> </html>

20
src/core/widget.js

@ -227,22 +227,36 @@
* @private * @private
*/ */
_mount: function (force, deep, lifeHook, predicate) { _mount: function (force, deep, lifeHook, predicate) {
if (this.__beforeMount(force, deep, lifeHook, predicate)) {
this.__afterMount(lifeHook, predicate);
return true;
}
return false;
},
__beforeMount: function (force, deep, lifeHook, predicate) {
var self = this; var self = this;
if (!force && (this._isMounted || !this.isVisible() || this.__asking === true || !(this._isRoot === true || (this._parent && this._parent._isMounted === true)))) { if (!force && (this._isMounted || !this.isVisible() || this.__asking === true || !(this._isRoot === true || (this._parent && this._parent._isMounted === true)))) {
return false; return false;
} }
lifeHook !== false && callLifeHook(this, "beforeMount"); lifeHook !== false && callLifeHook(this, "beforeMount");
this._isMounted = true; this._isMounted = true;
this._mountChildren && this._mountChildren();
BI.each(this._children, function (i, widget) { BI.each(this._children, function (i, widget) {
!self.isEnabled() && widget._setEnable(false); !self.isEnabled() && widget._setEnable(false);
!self.isValid() && widget._setValid(false); !self.isValid() && widget._setValid(false);
widget._mount && widget._mount(deep ? force : false, deep, lifeHook, predicate); widget.__beforeMount && widget.__beforeMount(deep ? force : false, deep, lifeHook, predicate);
});
this._mountChildren && this._mountChildren();
return true;
},
__afterMount: function (lifeHook, predicate) {
BI.each(this._children, function (i, widget) {
widget.__afterMount && widget.__afterMount(lifeHook, predicate);
}); });
lifeHook !== false && callLifeHook(this, "mounted"); lifeHook !== false && callLifeHook(this, "mounted");
this.fireEvent(BI.Events.MOUNT); this.fireEvent(BI.Events.MOUNT);
predicate && predicate(this); predicate && predicate(this);
return true;
}, },
_mountChildren: null, _mountChildren: null,

Loading…
Cancel
Save