Skip to content

Instantly share code, notes, and snippets.

@GerardRodes
Last active March 14, 2019 08:13
Show Gist options
  • Save GerardRodes/0daf895284460fa9a5990c9333d97452 to your computer and use it in GitHub Desktop.
Save GerardRodes/0daf895284460fa9a5990c9333d97452 to your computer and use it in GitHub Desktop.
Good implementation
<template>
<div>
<ul>
<li v-for="item in results" :key="item.id">
{{ item.name }}
</li>
</ul>
<Checkbox
:value="filters.isNew"
@input="$router.push({ query: { ...$route.query, isNew: !$event }})"
/>
<Pagination
:value="page"
@input="$router.push({ query: { ...$route.query, page: $event }})"
/>
</div>
</template>
<script>
export default {
name: 'ResultsPage',
beforeRouteEnter (to, from, next) {
next(vm => {
vm.page = to.page.query.page || 1
vm.filters.isNew = !!to.page.query.isNew
vm.fetchResults()
})
},
async beforeRouteUpdate (to, from, next) {
this.page = to.page.query.page || 1
this.filters.isNew = !!to.page.query.isNew
await this.fetchResults()
next()
},
data: () => ({
results: [],
page: 1,
filters: {
isNew: false
}
}),
methods: {
async fetchResults () {
const { data } = await SomeApiCall({
filters: this.filters,
page: this.page
})
this.results = data
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment