Last active
March 14, 2019 08:13
-
-
Save GerardRodes/7456d4bb776d4a762bf72e1c85bd2dbd to your computer and use it in GitHub Desktop.
Good implementation without data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<ul> | |
<li v-for="item in results" :key="item.id"> | |
{{ item.name }} | |
</li> | |
</ul> | |
<Checkbox | |
:value="!!$route.query.isNew" | |
@input="$router.push({ query: { ...$route.query, isNew: !!$event } })" | |
/> | |
<Pagination | |
:value="$route.query.page || 1" | |
@input="$router.push({ query: { ...$route.query, page: $event } })" | |
/> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'ResultsPage', | |
beforeRouteEnter (to, from, next) { | |
next(vm => { vm.fetchResultsFromRoute(to) }) | |
}, | |
async beforeRouteUpdate (to, from, next) { | |
await this.fetchResultsFromRoute(to) | |
next() | |
}, | |
data: () => ({ | |
results: [] | |
}), | |
methods: { | |
async fetchResultsFromRoute ({ query: { isNew = false, page = 1 } = {}) { | |
const { data } = await SomeApiCall({ | |
filters: { isNew }, | |
page | |
}) | |
this.results = data | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment