Commit 9c32a05699c5ab1ec5b8534f998ffd50f2ff51fd
1 parent
17db7df4
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 | 50 | import { oneOf, findComponentDownward } from '../../utils/assist'; |
| 51 | 51 | import Emitter from '../../mixins/emitter'; |
| 52 | 52 | import Locale from '../../mixins/locale'; |
| 53 | + import {debounce} from './utils'; | |
| 53 | 54 | |
| 54 | 55 | const prefixCls = 'ivu-select'; |
| 55 | 56 | |
| ... | ... | @@ -623,6 +624,18 @@ |
| 623 | 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 | 639 | // 处理 remote 初始值 |
| 627 | 640 | updateLabel () { |
| 628 | 641 | if (this.remote) { |
| ... | ... | @@ -656,34 +669,8 @@ |
| 656 | 669 | this.updateOptions(true); |
| 657 | 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 | 675 | this.$on('on-select-selected', (value) => { |
| 689 | 676 | if (this.model === value) { | ... | ... |
| 1 | +export function debounce(fn) { | |
| 2 | + let waiting; | |
| 3 | + return function() { | |
| 4 | + if (waiting) return; | |
| 5 | + waiting = true; | |
| 6 | + const context = this, | |
| 7 | + args = arguments; | |
| 8 | + const later = function() { | |
| 9 | + waiting = false; | |
| 10 | + fn.apply(context, args); | |
| 11 | + }; | |
| 12 | + this.$nextTick(later); | |
| 13 | + }; | |
| 14 | +} | ... | ... |
test/unit/specs/select.spec.js
| ... | ... | @@ -156,7 +156,7 @@ describe('Select.vue', () => { |
| 156 | 156 | }); |
| 157 | 157 | |
| 158 | 158 | describe('Performance tests', () => { |
| 159 | - xit('should handle big numbers of options', done => { | |
| 159 | + it('should handle big numbers of options', done => { | |
| 160 | 160 | const manyLaterOptions = Array.apply(null, Array(200)).map((_, i) => { |
| 161 | 161 | return { |
| 162 | 162 | value: i + 1, | ... | ... |