From 6704e10365ad458a926a222881524c22a0cf1f82 Mon Sep 17 00:00:00 2001 From: guy Date: Thu, 19 Aug 2021 16:40:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/wrapper/layout.js | 46 +++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/core/wrapper/layout.js b/src/core/wrapper/layout.js index b69c6a652..ac421a7d0 100644 --- a/src/core/wrapper/layout.js +++ b/src/core/wrapper/layout.js @@ -198,6 +198,21 @@ BI.Layout = BI.inherit(BI.Widget, { return this.element; }, + // 不依赖于this.options.items进行更新 + _updateItemAt: function (index, item) { + var del = this._children[this._getChildName(index)]; + delete this._children[this._getChildName(index)]; + var w = this._addElement(index, item); + this._children[this._getChildName(index)] = w; + if (index > 0) { + this._children[this._getChildName(index - 1)].element.after(w.element); + } else { + w.element.prependTo(this._getWrapper()); + } + del._destroy(); + w._mount(); + }, + _addItemAt: function (index, item) { for (var i = this.options.items.length; i > index; i--) { this._children[this._getChildName(i)] = this._children[this._getChildName(i - 1)]; @@ -286,35 +301,13 @@ BI.Layout = BI.inherit(BI.Widget, { }, shouldUpdateItem: function (index, item) { - if (index < 0 || index > this.options.items.length - 1) { - return false; - } var child = this._children[this._getChildName(index)]; - if (!child.shouldUpdate) { + if (!child || !child.shouldUpdate) { return null; } return child.shouldUpdate(this._getOptions(item)); }, - updateItemAt: function (index, item) { - if (index < 0 || index > this.options.items.length - 1) { - return; - } - var del = this._children[this._getChildName(index)]; - delete this._children[this._getChildName(index)]; - this.options.items.splice(index, 1); - var w = this._addElement(index, item); - this.options.items.splice(index, 0, item); - this._children[this._getChildName(index)] = w; - if (index > 0) { - this._children[this._getChildName(index - 1)].element.after(w.element); - } else { - w.element.prependTo(this._getWrapper()); - } - del._destroy(); - w._mount(); - }, - addItems: function (items, context) { var self = this, o = this.options; var fragment = BI.Widget._renderEngine.createFragment(); @@ -394,8 +387,9 @@ BI.Layout = BI.inherit(BI.Widget, { // if (child.update) { // return child.update(this._getOptions(vnode)); // } - return this.updateItemAt(index, vnode); + return this._updateItemAt(index, vnode); } + }, updateChildren: function (oldCh, newCh) { @@ -564,9 +558,9 @@ BI.Layout = BI.inherit(BI.Widget, { update: function (opt) { var o = this.options; var items = opt.items || []; - var updated = this.updateChildren(o.items, items); + var oldItems = o.items; this.options.items = items; - return updated; + return this.updateChildren(oldItems, items); }, stroke: function (items) {