From 18485e23937db46c76c8db028b8a0365832bf722 Mon Sep 17 00:00:00 2001 From: Yao Date: Fri, 11 May 2018 14:14:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20cell=20=E5=9C=A8=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E6=9B=B4=E6=96=B0=E5=90=8E=EF=BC=8C=E8=BE=B9=E6=A1=86?= =?UTF-8?q?=E4=BC=9A=E4=B8=8D=E6=98=BE=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#236)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cell-group/index.js | 35 ++++++++++++++++++++++++++++------- packages/cell/index.js | 6 ++++-- 2 files changed, 32 insertions(+), 9 deletions(-) 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 }); } } });