From a9131058abfaee847e14feff0066d8ff7ed129bf Mon Sep 17 00:00:00 2001 From: 梁灏 Date: Mon, 13 Mar 2017 19:33:52 +0800 Subject: [PATCH] optimize Select event broadcast --- examples/routers/select.vue | 11 ++++++++++- src/components/select/select.vue | 12 ++++++++---- src/utils/assist.js | 31 ++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/examples/routers/select.vue b/examples/routers/select.vue index bcafc28..1da0d74 100644 --- a/examples/routers/select.vue +++ b/examples/routers/select.vue @@ -196,7 +196,16 @@ diff --git a/src/components/select/select.vue b/src/components/select/select.vue index 88050bf..89a1a5d 100644 --- a/src/components/select/select.vue +++ b/src/components/select/select.vue @@ -36,7 +36,7 @@ import Icon from '../icon'; import Drop from './dropdown.vue'; import clickoutside from '../../directives/clickoutside'; - import { oneOf, MutationObserver } from '../../utils/assist'; + import { oneOf, MutationObserver, findComponentDownward } from '../../utils/assist'; import { t } from '../../locale'; import Emitter from '../../mixins/emitter'; @@ -507,6 +507,7 @@ document.addEventListener('keydown', this.handleKeydown); // watch slot changed + // todo 在 child 的 mounted 和 beforeDestroy 里处理 if (MutationObserver) { this.observer = new MutationObserver(() => { this.modelToQuery(); @@ -590,9 +591,12 @@ } }, query (val) { - // todo 这里会重复 - this.broadcast('OptionGroup', 'on-query-change', val); - this.broadcast('iOption', 'on-query-change', val); + if (findComponentDownward(this, 'OptionGroup')) { + this.broadcast('OptionGroup', 'on-query-change', val); + this.broadcast('iOption', 'on-query-change', val); + } else { + this.broadcast('iOption', 'on-query-change', val); + } let is_hidden = true; this.$nextTick(() => { diff --git a/src/utils/assist.js b/src/utils/assist.js index 6ba371b..61ae507 100644 --- a/src/utils/assist.js +++ b/src/utils/assist.js @@ -183,4 +183,33 @@ function findComponentUpward (content, componentName, componentNames) { } return parent; } -export {findComponentUpward}; \ No newline at end of file +export {findComponentUpward}; + +// Find components downward +function findComponentDownward (content, componentName) { + let childrens = content.$children; + let children = null; + + if (childrens.length) { + childrens.forEach(child => { + const name = child.$options.name; + if (name === componentName) { + children = child; + } + }); + + for (let i = 0; i < childrens.length; i++) { + const child = childrens[i]; + const name = child.$options.name; + if (name === componentName) { + children = child; + break; + } else { + children = findComponentDownward(child, componentName); + if (children) break; + } + } + } + return children; +} +export {findComponentDownward}; \ No newline at end of file -- libgit2 0.21.4