|
4 | 4 | @blur="onBlur">
|
5 | 5 | <md-menu :md-offset-x="8"
|
6 | 6 | md-offset-y="45"
|
7 |
| - ref="menu"> |
| 7 | + ref="menu" |
| 8 | + class="md-autocomplete-menu"> |
8 | 9 | <span md-menu-trigger></span>
|
9 | 10 | <input class="md-input"
|
10 | 11 | ref="input"
|
|
46 | 47 | selected: null,
|
47 | 48 | timeout: 0,
|
48 | 49 | parentContainer: null,
|
| 50 | + searchButton: null, |
49 | 51 | };
|
50 | 52 | },
|
51 | 53 | computed: {
|
|
89 | 91 | this.onInput();
|
90 | 92 | this.$emit('selected', this.selected, this.$refs.input.value);
|
91 | 93 | },
|
| 94 | + makeFetchRequest(queryObject) { |
| 95 | + return this.fetch(queryObject) |
| 96 | + .then((response) => { |
| 97 | + let data = response || response.data || response.body; |
| 98 | +
|
| 99 | + data = this.prepareResponseData ? |
| 100 | + this.prepareResponseData(data) : |
| 101 | + data; |
| 102 | + this.items = this.limit ? |
| 103 | + data.slice(0, this.limit) : |
| 104 | + data; |
| 105 | +
|
| 106 | + this.loading = false; |
| 107 | +
|
| 108 | + this.toggleMenu(); |
| 109 | + }); |
| 110 | + }, |
92 | 111 | onFocus() {
|
93 | 112 | if (this.parentContainer) {
|
94 | 113 | this.parentContainer.isFocused = true;
|
|
127 | 146 | this.setParentValue(value);
|
128 | 147 | this.updateValues(value);
|
129 | 148 | },
|
| 149 | + setSearchButton() { |
| 150 | + this.searchButton = this.parentContainer.$el.querySelector('[md-autocomplete-search]'); |
| 151 | +
|
| 152 | + if (this.searchButton) { |
| 153 | + this.searchButton.addEventListener('click', this.makeFetchRequest); |
| 154 | + } |
| 155 | + }, |
130 | 156 | update() {
|
131 | 157 | if (!this.query && !this.list.length) {
|
132 | 158 | return this.reset();
|
|
140 | 166 |
|
141 | 167 | const queryObject = { [this.queryParam]: this.query };
|
142 | 168 |
|
143 |
| - this.fetch(queryObject) |
144 |
| - .then((response) => { |
145 |
| - if (this.query) { |
146 |
| - let data = response || response.data || response.body; |
147 |
| -
|
148 |
| - data = this.prepareResponseData ? |
149 |
| - this.prepareResponseData(data) : |
150 |
| - data; |
151 |
| - this.items = this.limit ? |
152 |
| - data.slice(0, this.limit) : |
153 |
| - data; |
154 |
| -
|
155 |
| - this.loading = false; |
156 |
| -
|
157 |
| - this.toggleMenu(); |
158 |
| - } |
159 |
| - }); |
| 169 | + return this.makeFetchRequest(queryObject); |
160 | 170 | },
|
161 | 171 | toggleMenu() {
|
162 | 172 | if (this.items.length) {
|
|
170 | 180 | this.parentContainer.inputLength = newValue ? newValue.length : 0;
|
171 | 181 | }
|
172 | 182 | },
|
| 183 | + beforeDestroy() { |
| 184 | + if (this.searchButton) { |
| 185 | + this.searchButton.removeEventListener('click', this.makeFetchRequest); |
| 186 | + } |
| 187 | + }, |
173 | 188 | mounted() {
|
174 | 189 | this.$nextTick(() => {
|
175 | 190 | this.parentContainer = getClosestVueParent(this.$parent, 'md-input-container');
|
|
181 | 196 | this.query = this.value;
|
182 | 197 |
|
183 | 198 | this.verifyProps();
|
| 199 | + this.setSearchButton(); |
184 | 200 |
|
185 | 201 | this.setParentDisabled();
|
186 | 202 | this.setParentRequired();
|
|
0 commit comments