diff --git a/packages/cell-group/index.js b/packages/cell-group/index.js index aa146cde..60e610c8 100644 --- a/packages/cell-group/index.js +++ b/packages/cell-group/index.js @@ -1,18 +1,39 @@ +let cellUpdateTimeout = 0; + Component({ relations: { '../cell/index': { type: 'child', - linked() {} + linked() { + this._updateIsLastCell(); + }, + linkChanged() { + this._updateIsLastCell(); + }, + unlinked() { + this._updateIsLastCell(); + } } }, - ready() { - let cells = this.getRelationNodes('../cell/index'); - if (cells.length > 0) { - let lastIndex = cells.length - 1; + methods: { + _updateIsLastCell() { + // 用 setTimeout 减少计算次数 + if (cellUpdateTimeout > 0) { + return; + } - cells.forEach((cell, index) => { - if (index < lastIndex) cell.notLastCell(); + cellUpdateTimeout = setTimeout(() => { + cellUpdateTimeout = 0; + let cells = this.getRelationNodes('../cell/index'); + + if (cells.length > 0) { + let lastIndex = cells.length - 1; + + cells.forEach((cell, index) => { + cell.updateIsLastCell(index === lastIndex); + }); + } }); } } diff --git a/packages/cell/index.js b/packages/cell/index.js index 5c5a055c..aab4267f 100644 --- a/packages/cell/index.js +++ b/packages/cell/index.js @@ -72,8 +72,10 @@ Component({ this.navigateTo(); } }, - notLastCell() { - this.setData({ isLastCell: false }); + + // 用于被 cell-group 更新,标志是否是最后一个 cell + updateIsLastCell(isLastCell) { + this.setData({ isLastCell }); } } });