From 19a0c000a1e02d080bcec55f1497d2561b7f6752 Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 10 Sep 2018 17:10:36 +0800 Subject: [PATCH] [improvement] Tabbar: optimize performance (#547) --- dist/col/index.js | 4 +++- dist/tabbar-item/index.js | 7 +++++++ dist/tabbar/index.js | 10 +++++++--- packages/col/index.js | 4 +++- packages/tabbar-item/index.js | 7 +++++++ packages/tabbar/index.js | 10 +++++++--- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/dist/col/index.js b/dist/col/index.js index b803361b..b87fd356 100644 --- a/dist/col/index.js +++ b/dist/col/index.js @@ -16,7 +16,9 @@ create({ setGutter(gutter) { const padding = `${gutter / 2}px`; const style = gutter ? `padding-left: ${padding}; padding-right: ${padding};` : ''; - this.setData({ style }); + if (style !== this.data.style) { + this.setData({ style }); + } } } }); diff --git a/dist/tabbar-item/index.js b/dist/tabbar-item/index.js index ff936043..ca2fb7d5 100644 --- a/dist/tabbar-item/index.js +++ b/dist/tabbar-item/index.js @@ -25,6 +25,13 @@ create({ parent.onChange(this); } this.$emit('click'); + }, + + setActive(data) { + const { active, count } = this.data; + if (active !== data.active || count !== data.count) { + this.setData(data); + } } } }); diff --git a/dist/tabbar/index.js b/dist/tabbar/index.js index 2c765016..2a9be4fe 100644 --- a/dist/tabbar/index.js +++ b/dist/tabbar/index.js @@ -34,12 +34,16 @@ create({ linked(target) { this.data.items.push(target); - this.setActiveItem(); + setTimeout(() => { + this.setActiveItem(); + }); }, unlinked(target) { this.data.items = this.data.items.filter(item => item !== target); - this.setActiveItem(); + setTimeout(() => { + this.setActiveItem(); + }); } } }, @@ -47,7 +51,7 @@ create({ methods: { setActiveItem() { this.data.items.forEach((item, index) => { - item.setData({ + item.setActive({ active: index === this.data.currentActive, count: this.data.items.length }); diff --git a/packages/col/index.js b/packages/col/index.js index b803361b..b87fd356 100644 --- a/packages/col/index.js +++ b/packages/col/index.js @@ -16,7 +16,9 @@ create({ setGutter(gutter) { const padding = `${gutter / 2}px`; const style = gutter ? `padding-left: ${padding}; padding-right: ${padding};` : ''; - this.setData({ style }); + if (style !== this.data.style) { + this.setData({ style }); + } } } }); diff --git a/packages/tabbar-item/index.js b/packages/tabbar-item/index.js index ff936043..ca2fb7d5 100644 --- a/packages/tabbar-item/index.js +++ b/packages/tabbar-item/index.js @@ -25,6 +25,13 @@ create({ parent.onChange(this); } this.$emit('click'); + }, + + setActive(data) { + const { active, count } = this.data; + if (active !== data.active || count !== data.count) { + this.setData(data); + } } } }); diff --git a/packages/tabbar/index.js b/packages/tabbar/index.js index 2c765016..2a9be4fe 100644 --- a/packages/tabbar/index.js +++ b/packages/tabbar/index.js @@ -34,12 +34,16 @@ create({ linked(target) { this.data.items.push(target); - this.setActiveItem(); + setTimeout(() => { + this.setActiveItem(); + }); }, unlinked(target) { this.data.items = this.data.items.filter(item => item !== target); - this.setActiveItem(); + setTimeout(() => { + this.setActiveItem(); + }); } } }, @@ -47,7 +51,7 @@ create({ methods: { setActiveItem() { this.data.items.forEach((item, index) => { - item.setData({ + item.setActive({ active: index === this.data.currentActive, count: this.data.items.length });