From 5a2434172b4b4fd1ca308d20586f1ed7bdba0978 Mon Sep 17 00:00:00 2001 From: guy Date: Sat, 10 Oct 2020 16:39:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=9C=A8=E7=BB=84=E4=BB=B6=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E7=9A=84=E6=97=B6=E5=80=99=E5=8D=B3=E7=A1=AE=E5=AE=9A?= =?UTF-8?q?=E7=88=B6=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); }); From 64ee148a669a885b4796226d704f8081f5c3bcfc Mon Sep 17 00:00:00 2001 From: guy Date: Sat, 10 Oct 2020 17:20:39 +0800 Subject: [PATCH 2/2] Composition API --- src/base/single/iframe/iframe.js | 12 ++++++------ src/base/single/img/img.js | 10 ++-------- src/base/single/link/link.js | 6 +----- src/base/tree/ztree/parttree.js | 6 +----- src/case/layer/layer.multipopup.js | 6 +----- src/case/layer/layer.panel.js | 6 +----- src/case/tree/ztree/tree.display.js | 9 +-------- src/core/action/action.show.js | 6 +----- src/core/controller.js | 9 +-------- src/core/logic/logic.layout.js | 18 +----------------- src/core/shortcut.js | 6 +++--- 11 files changed, 19 insertions(+), 75 deletions(-) diff --git a/src/base/single/iframe/iframe.js b/src/base/single/iframe/iframe.js index 81269bdad..2bb4fc1b2 100644 --- a/src/base/single/iframe/iframe.js +++ b/src/base/single/iframe/iframe.js @@ -5,24 +5,24 @@ * Created by GameJian on 2016/3/2. */ BI.Iframe = BI.inherit(BI.Single, { - _defaultConfig: function () { + _defaultConfig: function (config) { var conf = BI.Iframe.superclass._defaultConfig.apply(this, arguments); return BI.extend(conf, { tagName: "iframe", baseCls: (conf.baseCls || "") + " bi-iframe", src: "", name: "", - attributes: {}, + attributes: { + frameborder: 0, + src: config.src, + name: config.name + }, width: "100%", height: "100%" }); }, _init: function () { - var self = this, o = this.options; - o.attributes.frameborder = "0"; - o.attributes.src = o.src; - o.attributes.name = o.name; BI.Iframe.superclass._init.apply(this, arguments); this.element.on("load", function () { self.fireEvent("EVENT_LOADED"); diff --git a/src/base/single/img/img.js b/src/base/single/img/img.js index 414679c37..e54306b11 100644 --- a/src/base/single/img/img.js +++ b/src/base/single/img/img.js @@ -7,24 +7,18 @@ * @abstract */ BI.Img = BI.inherit(BI.Single, { - _defaultConfig: function () { + _defaultConfig: function (config) { var conf = BI.Img.superclass._defaultConfig.apply(this, arguments); return BI.extend(conf, { tagName: "img", baseCls: (conf.baseCls || "") + " bi-img display-block", src: "", - attributes: {}, + attributes: config.src ? {src: config.src} : {}, width: "100%", height: "100%" }); }, - _init: function () { - var o = this.options; - o.attributes.src = o.src; - BI.Img.superclass._init.apply(this, arguments); - }, - setSrc: function (src) { this.options.src = src; this.element.attr("src", src); diff --git a/src/base/single/link/link.js b/src/base/single/link/link.js index ede3fd523..680893537 100644 --- a/src/base/single/link/link.js +++ b/src/base/single/link/link.js @@ -28,11 +28,7 @@ BI.Link = BI.inherit(BI.Label, { href: o.href, target: o.target }; - }, - - _init: function () { - BI.Link.superclass._init.apply(this, arguments); } }); -BI.shortcut("bi.link", BI.Link); \ No newline at end of file +BI.shortcut("bi.link", BI.Link); diff --git a/src/base/tree/ztree/parttree.js b/src/base/tree/ztree/parttree.js index 771217108..974196b33 100644 --- a/src/base/tree/ztree/parttree.js +++ b/src/base/tree/ztree/parttree.js @@ -9,10 +9,6 @@ BI.PartTree = BI.inherit(BI.AsyncTree, { return BI.extend(BI.PartTree.superclass._defaultConfig.apply(this, arguments), {}); }, - _init: function () { - BI.PartTree.superclass._init.apply(this, arguments); - }, - _loadMore: function () { var self = this, o = this.options; var op = BI.extend({}, o.paras, { @@ -201,4 +197,4 @@ BI.PartTree = BI.inherit(BI.AsyncTree, { } }); -BI.shortcut("bi.part_tree", BI.PartTree); \ No newline at end of file +BI.shortcut("bi.part_tree", BI.PartTree); diff --git a/src/case/layer/layer.multipopup.js b/src/case/layer/layer.multipopup.js index 80d9fee93..ec937a172 100644 --- a/src/case/layer/layer.multipopup.js +++ b/src/case/layer/layer.multipopup.js @@ -14,10 +14,6 @@ BI.MultiPopupView = BI.inherit(BI.PopupView, { }); }, - _init: function () { - BI.MultiPopupView.superclass._init.apply(this, arguments); - }, - _createToolBar: function () { var o = this.options, self = this; if (o.buttons.length === 0) { @@ -61,4 +57,4 @@ BI.MultiPopupView = BI.inherit(BI.PopupView, { BI.MultiPopupView.EVENT_CHANGE = "EVENT_CHANGE"; BI.MultiPopupView.EVENT_CLICK_TOOLBAR_BUTTON = "EVENT_CLICK_TOOLBAR_BUTTON"; -BI.shortcut("bi.multi_popup_view", BI.MultiPopupView); \ No newline at end of file +BI.shortcut("bi.multi_popup_view", BI.MultiPopupView); diff --git a/src/case/layer/layer.panel.js b/src/case/layer/layer.panel.js index ba4a20677..c136cc0e9 100644 --- a/src/case/layer/layer.panel.js +++ b/src/case/layer/layer.panel.js @@ -14,10 +14,6 @@ BI.PopupPanel = BI.inherit(BI.MultiPopupView, { }); }, - _init: function () { - BI.PopupPanel.superclass._init.apply(this, arguments); - }, - _createTool: function () { var self = this, o = this.options; var close = BI.createWidget({ @@ -54,4 +50,4 @@ BI.PopupPanel.EVENT_CHANGE = "EVENT_CHANGE"; BI.PopupPanel.EVENT_CLOSE = "EVENT_CLOSE"; BI.PopupPanel.EVENT_CLICK_TOOLBAR_BUTTON = "EVENT_CLICK_TOOLBAR_BUTTON"; -BI.shortcut("bi.popup_panel", BI.PopupPanel); \ No newline at end of file +BI.shortcut("bi.popup_panel", BI.PopupPanel); diff --git a/src/case/tree/ztree/tree.display.js b/src/case/tree/ztree/tree.display.js index 5d3c7d340..4c0135675 100644 --- a/src/case/tree/ztree/tree.display.js +++ b/src/case/tree/ztree/tree.display.js @@ -10,9 +10,6 @@ BI.DisplayTree = BI.inherit(BI.TreeView, { extraCls: "bi-display-tree" }); }, - _init: function () { - BI.DisplayTree.superclass._init.apply(this, arguments); - }, // 配置属性 _configSetting: function () { @@ -62,12 +59,8 @@ BI.DisplayTree = BI.inherit(BI.TreeView, { initTree: function (nodes, setting) { var setting = setting || this._configSetting(); this.nodes = BI.$.fn.zTree.init(this.tree.element, setting, nodes); - }, - - destroy: function () { - BI.DisplayTree.superclass.destroy.apply(this, arguments); } }); BI.DisplayTree.EVENT_CHANGE = "EVENT_CHANGE"; -BI.shortcut("bi.display_tree", BI.DisplayTree); \ No newline at end of file +BI.shortcut("bi.display_tree", BI.DisplayTree); diff --git a/src/core/action/action.show.js b/src/core/action/action.show.js index 1f24c9a0e..4fd6c3fae 100644 --- a/src/core/action/action.show.js +++ b/src/core/action/action.show.js @@ -9,10 +9,6 @@ BI.ShowAction = BI.inherit(BI.Action, { return BI.extend(BI.ShowAction.superclass._defaultConfig.apply(this, arguments), {}); }, - _init: function () { - BI.ShowAction.superclass._init.apply(this, arguments); - }, - actionPerformed: function (src, tar, callback) { tar = tar || this.options.tar; tar.setVisible(true); @@ -24,4 +20,4 @@ BI.ShowAction = BI.inherit(BI.Action, { tar.setVisible(false); callback && callback(); } -}); \ No newline at end of file +}); diff --git a/src/core/controller.js b/src/core/controller.js index 2aee3d6ac..ac55e834b 100644 --- a/src/core/controller.js +++ b/src/core/controller.js @@ -11,13 +11,6 @@ BI.Controller = BI.inherit(BI.OB, { return BI.extend(BI.Controller.superclass._defaultConfig.apply(this, arguments), { }); - }, - _init: function () { - BI.Controller.superclass._init.apply(this, arguments); - }, - - destroy: function () { - } }); -BI.Controller.EVENT_CHANGE = "__EVENT_CHANGE__"; \ No newline at end of file +BI.Controller.EVENT_CHANGE = "__EVENT_CHANGE__"; diff --git a/src/core/logic/logic.layout.js b/src/core/logic/logic.layout.js index c6c33bdc6..a3a630b3a 100644 --- a/src/core/logic/logic.layout.js +++ b/src/core/logic/logic.layout.js @@ -43,10 +43,6 @@ BI.VerticalLayoutLogic = BI.inherit(BI.Logic, { bgap: o.bgap, items: o.items }; - }, - - _init: function () { - BI.VerticalLayoutLogic.superclass._init.apply(this, arguments); } }); @@ -96,10 +92,6 @@ BI.HorizontalLayoutLogic = BI.inherit(BI.Logic, { bgap: o.bgap, items: o.items }; - }, - - _init: function () { - BI.HorizontalLayoutLogic.superclass._init.apply(this, arguments); } }); @@ -148,10 +140,6 @@ BI.TableLayoutLogic = BI.inherit(BI.Logic, { vgap: o.vgap, items: o.items }; - }, - - _init: function () { - BI.TableLayoutLogic.superclass._init.apply(this, arguments); } }); @@ -204,9 +192,5 @@ BI.HorizontalFillLayoutLogic = BI.inherit(BI.Logic, { bgap: o.bgap, items: o.items }; - }, - - _init: function () { - BI.HorizontalFillLayoutLogic.superclass._init.apply(this, arguments); } -}); \ No newline at end of file +}); diff --git a/src/core/shortcut.js b/src/core/shortcut.js index 8384acdef..9bdddf158 100644 --- a/src/core/shortcut.js +++ b/src/core/shortcut.js @@ -23,9 +23,9 @@ widget._initProps(config); widget._initRoot(); widget._initRef(); - if (config.element || config.root) { - widget._lazyConstructor(); - } + // if (config.element || config.root) { + widget._lazyConstructor(); + // } return widget; };