From 5a2434172b4b4fd1ca308d20586f1ed7bdba0978 Mon Sep 17 00:00:00 2001 From: guy Date: Sat, 10 Oct 2020 16:39:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E7=BB=84=E4=BB=B6=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=8D=B3=E7=A1=AE=E5=AE=9A=E7=88=B6?= =?UTF-8?q?=E5=AD=90=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/ob.js | 2 +- src/core/shortcut.js | 10 ++++++---- src/core/widget.js | 14 +++++++++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/core/ob.js b/src/core/ob.js index 9dc8b9959..c4aacf624 100644 --- a/src/core/ob.js +++ b/src/core/ob.js @@ -1,5 +1,5 @@ !(function () { - function extend() { + function extend () { var target = arguments[0] || {}, length = arguments.length, i = 1, options, name, src, copy; for (; i < length; i++) { // Only deal with non-null/undefined values diff --git a/src/core/shortcut.js b/src/core/shortcut.js index a287091cb..8384acdef 100644 --- a/src/core/shortcut.js +++ b/src/core/shortcut.js @@ -14,16 +14,18 @@ var createWidget = function (config) { var cls = kv[config.type]; - if(!cls){ - throw new Error("组件"+config.type +"未定义"); + if (!cls) { + throw new Error("组件" + config.type + "未定义"); } var widget = new cls(); widget._initProps(config); - widget._init(); + widget._initRoot(); widget._initRef(); - + if (config.element || config.root) { + widget._lazyConstructor(); + } return widget; }; diff --git a/src/core/widget.js b/src/core/widget.js index 12556ff13..c56d5fd04 100644 --- a/src/core/widget.js +++ b/src/core/widget.js @@ -28,6 +28,14 @@ // 覆盖父类的_constructor方法,widget不走ob的生命周期 _constructor: function () { + // do nothing + }, + + _lazyConstructor: function () { + if (!this._constructed) { + this._constructed = true; + this._init(); + } }, beforeInit: null, @@ -54,7 +62,6 @@ _init: function () { BI.Widget.superclass._init.apply(this, arguments); - this._initRoot(); this._initElementWidth(); this._initElementHeight(); this._initVisual(); @@ -90,14 +97,15 @@ var o = this.options; this.widgetName = o.widgetName || BI.uniqueId("widget"); this._isRoot = o.root; + this._children = {}; if (BI.isWidget(o.element)) { + this.element = this.options.element.element; if (o.element instanceof BI.Widget) { this._parent = o.element; this._parent.addWidget(this.widgetName, this); } else { this._isRoot = true; } - this.element = this.options.element.element; } else if (o.element) { // if (o.root !== true) { // throw new Error("root is a required property"); @@ -120,7 +128,6 @@ if (o.css) { this.element.css(o.css); } - this._children = {}; }, _initElementWidth: function () { @@ -330,6 +337,7 @@ throw new Error("name has already been existed"); } widget._setParent && widget._setParent(this); + widget._lazyConstructor(); widget.on(BI.Events.DESTROY, function () { BI.remove(self._children, this); });