Commit e493492e50e4e094b404fe173fe81fa33eabfeaf
Committed by
GitHub
Merge pull request #1777 from SergioCrisostomo/optimise-select-child-search
Optimize append and remove options
Showing
3 changed files
with
30 additions
and
29 deletions
Show diff stats
src/components/select/select.vue
@@ -50,6 +50,7 @@ | @@ -50,6 +50,7 @@ | ||
50 | import { oneOf, findComponentDownward } from '../../utils/assist'; | 50 | import { oneOf, findComponentDownward } from '../../utils/assist'; |
51 | import Emitter from '../../mixins/emitter'; | 51 | import Emitter from '../../mixins/emitter'; |
52 | import Locale from '../../mixins/locale'; | 52 | import Locale from '../../mixins/locale'; |
53 | + import {debounce} from './utils'; | ||
53 | 54 | ||
54 | const prefixCls = 'ivu-select'; | 55 | const prefixCls = 'ivu-select'; |
55 | 56 | ||
@@ -623,6 +624,18 @@ | @@ -623,6 +624,18 @@ | ||
623 | this.broadcast('iOption', 'on-query-change', val); | 624 | this.broadcast('iOption', 'on-query-change', val); |
624 | } | 625 | } |
625 | }, | 626 | }, |
627 | + debouncedAppendRemove: debounce(function(){ | ||
628 | + if (!this.remote) { | ||
629 | + this.modelToQuery(); | ||
630 | + this.$nextTick(() => this.broadcastQuery('')); | ||
631 | + } else { | ||
632 | + this.findChild((child) => { | ||
633 | + child.selected = this.multiple ? this.model.indexOf(child.value) > -1 : this.model === child.value; | ||
634 | + }); | ||
635 | + } | ||
636 | + this.slotChange(); | ||
637 | + this.updateOptions(true, true); | ||
638 | + }), | ||
626 | // 处理 remote 初始值 | 639 | // 处理 remote 初始值 |
627 | updateLabel () { | 640 | updateLabel () { |
628 | if (this.remote) { | 641 | if (this.remote) { |
@@ -656,34 +669,8 @@ | @@ -656,34 +669,8 @@ | ||
656 | this.updateOptions(true); | 669 | this.updateOptions(true); |
657 | document.addEventListener('keydown', this.handleKeydown); | 670 | document.addEventListener('keydown', this.handleKeydown); |
658 | 671 | ||
659 | - this.$on('append', () => { | ||
660 | - if (!this.remote) { | ||
661 | - this.modelToQuery(); | ||
662 | - this.$nextTick(() => { | ||
663 | - this.broadcastQuery(''); | ||
664 | - }); | ||
665 | - } else { | ||
666 | - this.findChild(child => { | ||
667 | - child.selected = this.multiple ? this.model.indexOf(child.value) > -1 : this.model === child.value; | ||
668 | - }); | ||
669 | - } | ||
670 | - this.slotChange(); | ||
671 | - this.updateOptions(true, true); | ||
672 | - }); | ||
673 | - this.$on('remove', () => { | ||
674 | - if (!this.remote) { | ||
675 | - this.modelToQuery(); | ||
676 | - this.$nextTick(() => { | ||
677 | - this.broadcastQuery(''); | ||
678 | - }); | ||
679 | - } else { | ||
680 | - this.findChild(child => { | ||
681 | - child.selected = this.multiple ? this.model.indexOf(child.value) > -1 : this.model === child.value; | ||
682 | - }); | ||
683 | - } | ||
684 | - this.slotChange(); | ||
685 | - this.updateOptions(true, true); | ||
686 | - }); | 672 | + this.$on('append', this.debouncedAppendRemove); |
673 | + this.$on('remove', this.debouncedAppendRemove); | ||
687 | 674 | ||
688 | this.$on('on-select-selected', (value) => { | 675 | this.$on('on-select-selected', (value) => { |
689 | if (this.model === value) { | 676 | if (this.model === value) { |
test/unit/specs/select.spec.js
@@ -156,7 +156,7 @@ describe('Select.vue', () => { | @@ -156,7 +156,7 @@ describe('Select.vue', () => { | ||
156 | }); | 156 | }); |
157 | 157 | ||
158 | describe('Performance tests', () => { | 158 | describe('Performance tests', () => { |
159 | - xit('should handle big numbers of options', done => { | 159 | + it('should handle big numbers of options', done => { |
160 | const manyLaterOptions = Array.apply(null, Array(200)).map((_, i) => { | 160 | const manyLaterOptions = Array.apply(null, Array(200)).map((_, i) => { |
161 | return { | 161 | return { |
162 | value: i + 1, | 162 | value: i + 1, |