Skip to content

Commit e0145c8

Browse files
author
An Phan
committed
Debounce filtering
1 parent 1555337 commit e0145c8

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

app/site/src/App.vue

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
<hgroup>
66
<h1><span class="thin">Awesome</span> Vue.js</h1>
77
<h2 class="tagline thin">A curated list of awesome things related to
8-
<a href="https://vuejs.org/">Vue.js</a></h2>
8+
<a href="https://vuejs.org/">Vue.js</a>
9+
</h2>
910
</hgroup>
1011
<form>
1112
<label>
1213
<input
1314
type="search"
1415
placeholder="type to filter"
1516
v-model="q"
17+
@input="debounceFilter()"
1618
id="seachField"
1719
autofocus="autofocus">
1820
<a :href="'#' + q"
@@ -59,39 +61,49 @@ export default {
5961
}
6062
},
6163
62-
watch: {
63-
q() {
64-
let q = this.q.trim()
65-
66-
if (q === 'everything') {
67-
q = ''
68-
}
69-
70-
if (!q) {
71-
this.groups = window.data
72-
return
73-
}
74-
75-
this.groups = this.filter(_.cloneDeep(window.data), q)
76-
}
77-
},
78-
7964
created() {
65+
// Listen to the 'tag-selected' event to trigger the filtering process.
8066
event.on('tag-selected', tag => {
8167
this.q = tag[0]
8268
8369
// Set the focus into the search field. Some little UX doesn't kill.
8470
this.$nextTick(() => {
8571
document.getElementById('seachField').focus()
8672
})
73+
74+
this.debounceFilter()
8775
})
8876
77+
// Also, upon page load, tf there's a hash, we filter the awesome list
78+
// right away.
8979
if (window.location.hash) {
9080
this.q = /^#(.*)/.exec(window.location.hash)[1].toLowerCase()
81+
this.debounceFilter()
9182
}
9283
},
9384
9485
methods: {
86+
/**
87+
* Limit filtering using lodash's debounce.
88+
* @param {Event}
89+
* @param {VueComponent}
90+
* @return {Function}
91+
*/
92+
debounceFilter: _.debounce(function () {
93+
let q = this.q.trim()
94+
95+
if (q === 'everything') {
96+
q = ''
97+
}
98+
99+
if (!q) {
100+
this.groups = window.data
101+
return
102+
}
103+
104+
this.groups = this.filter(_.cloneDeep(window.data), q)
105+
}, 100),
106+
95107
/**
96108
* Filter our awesome data.
97109
* @param {Array.<Object>} groups The groups to apply filtering on

0 commit comments

Comments
 (0)