Browse Source

无JIRA任务 updateChildren的时候不delete child

es6
windy 3 years ago
parent
commit
87b40f75c7
  1. 30
      src/core/wrapper/layout.js

30
src/core/wrapper/layout.js

@ -87,14 +87,17 @@ BI.Layout = BI.inherit(BI.Widget, {
return "" + index; return "" + index;
}, },
_addElement: function (i, item, context) { _addElement: function (i, item, context, widget) {
var self = this, w; var self = this, w;
if (widget) {
return widget;
}
if (!this.hasWidget(this._getChildName(i))) { if (!this.hasWidget(this._getChildName(i))) {
w = BI._lazyCreateWidget(item, context); w = BI._lazyCreateWidget(item, context);
w.on(BI.Events.DESTROY, function () { w.on(BI.Events.DESTROY, function () {
BI.each(self._children, function (name, child) { BI.each(self._children, function (name, child) {
if (child === w) { if (child === w) {
BI.remove(self._children, child); delete self._children[name];
self.removeItemAt(name | 0); self.removeItemAt(name | 0);
} }
}); });
@ -106,6 +109,20 @@ BI.Layout = BI.inherit(BI.Widget, {
return w; 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) { _getOptions: function (item) {
if (item instanceof BI.Widget) { if (item instanceof BI.Widget) {
item = item.options; item = item.options;
@ -441,16 +458,12 @@ BI.Layout = BI.inherit(BI.Widget, {
} else { } else {
var sameOldVnode = findOldVnode(oldCh, newStartVnode, oldStartIdx, oldEndIdx); var sameOldVnode = findOldVnode(oldCh, newStartVnode, oldStartIdx, oldEndIdx);
if (BI.isNull(sameOldVnode[0])) { // 不存在就把新的放到左边 if (BI.isNull(sameOldVnode[0])) { // 不存在就把新的放到左边
delete self._children[self._getChildName(newStartIdx)];
var node = addNode(newStartVnode, newStartIdx); var node = addNode(newStartVnode, newStartIdx);
insertBefore(node, oldStartVnode); insertBefore(node, oldStartVnode);
} else { // 如果新节点在旧节点区间中存在就复用一下 } else { // 如果新节点在旧节点区间中存在就复用一下
var sameOldIndex = sameOldVnode[1]; var sameOldIndex = sameOldVnode[1];
updated = self.patchItem(sameOldVnode[0], newStartVnode, sameOldIndex, newStartIdx) || updated; updated = self.patchItem(sameOldVnode[0], newStartVnode, sameOldIndex, newStartIdx) || updated;
children[sameOldVnode[0].key == null ? newStartIdx : sameOldVnode[0].key] = self._children[self._getChildName(sameOldIndex)]; 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; oldCh[sameOldIndex] = undefined;
insertBefore(sameOldVnode[0], oldStartVnode); insertBefore(sameOldVnode[0], oldStartVnode);
} }
@ -468,6 +481,7 @@ BI.Layout = BI.inherit(BI.Widget, {
BI.each(newCh, function (i, child) { BI.each(newCh, function (i, child) {
var node = self._getOptions(child); var node = self._getOptions(child);
var key = node.key == null ? i : node.key; var key = node.key == null ? i : node.key;
children[key]._setParent && children[key]._setParent(self);
children[key]._mount(); children[key]._mount();
self._children[self._getChildName(i)] = children[key]; self._children[self._getChildName(i)] = children[key];
}); });
@ -486,8 +500,7 @@ BI.Layout = BI.inherit(BI.Widget, {
function addNode (vnode, index) { function addNode (vnode, index) {
var opt = self._getOptions(vnode); var opt = self._getOptions(vnode);
var key = opt.key == null ? index : opt.key; var key = opt.key == null ? index : opt.key;
delete self._children[self._getChildName(index)]; return children[key] = self._newElement(index, vnode);
return children[key] = self._addElement(index, vnode);
} }
function addVnodes (before, vnodes, startIdx, endIdx) { function addVnodes (before, vnodes, startIdx, endIdx) {
@ -503,7 +516,6 @@ BI.Layout = BI.inherit(BI.Widget, {
if (BI.isNotNull(ch)) { if (BI.isNotNull(ch)) {
var node = self._getOptions(ch); var node = self._getOptions(ch);
var key = node.key == null ? startIdx : node.key; var key = node.key == null ? startIdx : node.key;
delete self._children[self._getChildName(startIdx)];
children[key]._destroy(); children[key]._destroy();
} }
} }

Loading…
Cancel
Save