From 87b40f75c73985e9e5220ce75fc0b55bfb886449 Mon Sep 17 00:00:00 2001 From: windy <1374721899@qq.com> Date: Fri, 3 Sep 2021 12:52:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0JIRA=E4=BB=BB=E5=8A=A1=20updateChildre?= =?UTF-8?q?n=E7=9A=84=E6=97=B6=E5=80=99=E4=B8=8Ddelete=20child?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/wrapper/layout.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/core/wrapper/layout.js b/src/core/wrapper/layout.js index be6785057..1f92a98d1 100644 --- a/src/core/wrapper/layout.js +++ b/src/core/wrapper/layout.js @@ -87,14 +87,17 @@ BI.Layout = BI.inherit(BI.Widget, { return "" + index; }, - _addElement: function (i, item, context) { + _addElement: function (i, item, context, widget) { var self = this, w; + if (widget) { + return widget; + } if (!this.hasWidget(this._getChildName(i))) { w = BI._lazyCreateWidget(item, context); w.on(BI.Events.DESTROY, function () { BI.each(self._children, function (name, child) { if (child === w) { - BI.remove(self._children, child); + delete self._children[name]; self.removeItemAt(name | 0); } }); @@ -106,6 +109,20 @@ BI.Layout = BI.inherit(BI.Widget, { return w; }, + _newElement: function (i, item, context) { + var self = this; + var w = BI._lazyCreateWidget(item, context); + w.on(BI.Events.DESTROY, function () { + BI.each(self._children, function (name, child) { + if (child === w) { + delete self._children[name]; + self.removeItemAt(name | 0); + } + }); + }); + return this._addElement(i, item, context, w); + }, + _getOptions: function (item) { if (item instanceof BI.Widget) { item = item.options; @@ -441,16 +458,12 @@ BI.Layout = BI.inherit(BI.Widget, { } else { var sameOldVnode = findOldVnode(oldCh, newStartVnode, oldStartIdx, oldEndIdx); if (BI.isNull(sameOldVnode[0])) { // 不存在就把新的放到左边 - delete self._children[self._getChildName(newStartIdx)]; var node = addNode(newStartVnode, newStartIdx); insertBefore(node, oldStartVnode); } else { // 如果新节点在旧节点区间中存在就复用一下 var sameOldIndex = sameOldVnode[1]; updated = self.patchItem(sameOldVnode[0], newStartVnode, sameOldIndex, newStartIdx) || updated; children[sameOldVnode[0].key == null ? newStartIdx : sameOldVnode[0].key] = self._children[self._getChildName(sameOldIndex)]; - if (newStartIdx !== sameOldIndex) { - delete self._children[self._getChildName(sameOldIndex)]; - } oldCh[sameOldIndex] = undefined; insertBefore(sameOldVnode[0], oldStartVnode); } @@ -468,6 +481,7 @@ BI.Layout = BI.inherit(BI.Widget, { BI.each(newCh, function (i, child) { var node = self._getOptions(child); var key = node.key == null ? i : node.key; + children[key]._setParent && children[key]._setParent(self); children[key]._mount(); self._children[self._getChildName(i)] = children[key]; }); @@ -486,8 +500,7 @@ BI.Layout = BI.inherit(BI.Widget, { function addNode (vnode, index) { var opt = self._getOptions(vnode); var key = opt.key == null ? index : opt.key; - delete self._children[self._getChildName(index)]; - return children[key] = self._addElement(index, vnode); + return children[key] = self._newElement(index, vnode); } function addVnodes (before, vnodes, startIdx, endIdx) { @@ -503,7 +516,6 @@ BI.Layout = BI.inherit(BI.Widget, { if (BI.isNotNull(ch)) { var node = self._getOptions(ch); var key = node.key == null ? startIdx : node.key; - delete self._children[self._getChildName(startIdx)]; children[key]._destroy(); } }