From 9cc418cd3dbd30faa99a3fb887e72c95d96d2d87 Mon Sep 17 00:00:00 2001 From: iapyang Date: Wed, 17 Jul 2019 14:16:34 +0800 Subject: [PATCH 1/3] =?UTF-8?q?KERNEL-859=20refactor:=20=E6=94=B9widget?= =?UTF-8?q?=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/ob.js | 19 ++++++++++++------- src/core/shortcut.js | 12 +++++++----- src/core/widget.js | 3 +++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/core/ob.js b/src/core/ob.js index 7c7d68ddf1..4c617631f0 100644 --- a/src/core/ob.js +++ b/src/core/ob.js @@ -29,24 +29,29 @@ * @abstract */ BI.OB = function (config) { - var props = this.props; - if (BI.isFunction(this.props)) { - props = this.props(config); - } - this.options = extend(this._defaultConfig(config), props, config); - this._init(); - this._initRef(); + this.__config = config; + this._constructor(); }; _.extend(BI.OB.prototype, { props: {}, init: null, destroyed: null, + _constructor: function () { + this._init(); + this._initRef(); + }, + _defaultConfig: function (config) { return {}; }, _init: function () { + var props = this.props; + if (BI.isFunction(this.props)) { + props = this.props(this.__config); + } + this.options = extend(this._defaultConfig(this.__config), props, this.__config); this._initListeners(); this.init && this.init(); }, diff --git a/src/core/shortcut.js b/src/core/shortcut.js index 53d6ae165e..b40cac907b 100644 --- a/src/core/shortcut.js +++ b/src/core/shortcut.js @@ -9,12 +9,14 @@ // 根据配置属性生成widget var createWidget = function (config) { - if (config["classType"]) { - return new (new Function("return " + config["classType"] + ";")())(config); - } - var cls = kv[config.type]; - return new cls(config); + + var widget = new cls(config); + + widget._init(); + widget._initRef(); + + return widget; }; BI.createWidget = function (item, options, context) { diff --git a/src/core/widget.js b/src/core/widget.js index f6add3ff06..eae1276e17 100644 --- a/src/core/widget.js +++ b/src/core/widget.js @@ -25,6 +25,9 @@ }); }, + // 覆盖父类的_constructor方法,widget不走ob的生命周期 + _constructor: function () {}, + beforeInit: null, // 生命周期函数 From 727552929a2bd752b5faa8312142d55d841164b0 Mon Sep 17 00:00:00 2001 From: iapyang Date: Wed, 17 Jul 2019 14:42:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E5=85=BC=E5=AE=B9context=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/ob.js | 6 +++++- src/core/shortcut.js | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/ob.js b/src/core/ob.js index 4c617631f0..0f5112c7be 100644 --- a/src/core/ob.js +++ b/src/core/ob.js @@ -38,6 +38,7 @@ destroyed: null, _constructor: function () { + this._initProps(); this._init(); this._initRef(); }, @@ -46,12 +47,15 @@ return {}; }, - _init: function () { + _initProps: function () { var props = this.props; if (BI.isFunction(this.props)) { props = this.props(this.__config); } this.options = extend(this._defaultConfig(this.__config), props, this.__config); + }, + + _init: function () { this._initListeners(); this.init && this.init(); }, diff --git a/src/core/shortcut.js b/src/core/shortcut.js index b40cac907b..124343230b 100644 --- a/src/core/shortcut.js +++ b/src/core/shortcut.js @@ -13,6 +13,7 @@ var widget = new cls(config); + widget._initProps(); widget._init(); widget._initRef(); From 09fed9a3a39681ef02977a6b3aa2edd7d2bb83a3 Mon Sep 17 00:00:00 2001 From: iapyang Date: Wed, 17 Jul 2019 15:47:33 +0800 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20=E7=BE=8E=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/ob.js | 13 ++++++------- src/core/shortcut.js | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/core/ob.js b/src/core/ob.js index 0f5112c7be..4cf532c5a4 100644 --- a/src/core/ob.js +++ b/src/core/ob.js @@ -29,16 +29,15 @@ * @abstract */ BI.OB = function (config) { - this.__config = config; - this._constructor(); + this._constructor(config); }; _.extend(BI.OB.prototype, { props: {}, init: null, destroyed: null, - _constructor: function () { - this._initProps(); + _constructor: function (config) { + this._initProps(config); this._init(); this._initRef(); }, @@ -47,12 +46,12 @@ return {}; }, - _initProps: function () { + _initProps: function (config) { var props = this.props; if (BI.isFunction(this.props)) { - props = this.props(this.__config); + props = this.props(config); } - this.options = extend(this._defaultConfig(this.__config), props, this.__config); + this.options = extend(this._defaultConfig(config), props, config); }, _init: function () { diff --git a/src/core/shortcut.js b/src/core/shortcut.js index 124343230b..4e7df4ea7a 100644 --- a/src/core/shortcut.js +++ b/src/core/shortcut.js @@ -11,9 +11,9 @@ var createWidget = function (config) { var cls = kv[config.type]; - var widget = new cls(config); + var widget = new cls(); - widget._initProps(); + widget._initProps(config); widget._init(); widget._initRef();