Skip to content

Commit 37682dc

Browse files
Merge #1194
1194: Start search while typing r=carols10cents Similar to e.g. https://yarnpkg.com/ this PR changes the code to start the search while typing instead of having to click the <kbd>Enter</kbd> button. It also debounces the search task (250ms) to limit the amount of requests that are hitting the server. ![crates-live-search](https://user-images.githubusercontent.com/141300/33524728-71d1a434-d822-11e7-8d20-7025fbc5f369.gif) /cc @carols10cents
2 parents ae43922 + f921e40 commit 37682dc

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

app/controllers/application.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export default Controller.extend(EKMixin, {
2121
}),
2222

2323
actions: {
24-
search() {
24+
search(q) {
25+
if (q !== undefined) {
26+
this.set('searchQuery', q);
27+
}
28+
2529
this.transitionToRoute('search', {
2630
queryParams: {
2731
q: this.get('searchQuery'),

app/controllers/search.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { computed } from '@ember/object';
33
import { alias, bool, readOnly } from '@ember/object/computed';
44
import { inject as service } from '@ember/service';
55

6-
import { task } from 'ember-concurrency';
6+
import { task, timeout } from 'ember-concurrency';
77

88
import PaginationMixin from '../mixins/pagination';
99

10+
const DEBOUNCE_MS = 250;
11+
1012
export default Controller.extend(PaginationMixin, {
1113
search: service(),
1214
queryParams: ['q', 'page', 'per_page', 'sort'],
@@ -40,10 +42,13 @@ export default Controller.extend(PaginationMixin, {
4042
hasItems: bool('totalItems'),
4143

4244
dataTask: task(function* (params) {
45+
// debounce the search query
46+
yield timeout(DEBOUNCE_MS);
47+
4348
if (params.q !== null) {
4449
params.q = params.q.trim();
4550
}
4651

4752
return yield this.store.query('crate', params);
48-
}).drop(),
53+
}).restartable(),
4954
});

app/templates/application.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<input type="text" class="search" name="q" id="cargo-desktop-search"
2222
placeholder="Click or press 'S' to search..."
2323
value={{searchQuery}}
24-
oninput={{action (mut searchQuery) value="target.value"}}
24+
oninput={{action "search" value="target.value"}}
2525
autofocus="autofocus"
2626
tabindex="1"
2727
required
@@ -106,7 +106,7 @@
106106
<input type="text" class="search" name="q"
107107
placeholder="Search"
108108
value={{searchQuery}}
109-
oninput={{action (mut searchQuery) value="target.value"}}
109+
oninput={{action "search" value="target.value"}}
110110
autocorrect="off"
111111
tabindex="1"
112112
required>

0 commit comments

Comments
 (0)