Skip to content

Instantly share code, notes, and snippets.

@comfuture
Last active May 19, 2018 09:57
Show Gist options
  • Save comfuture/2d40d2cc1d659614ec982a7613e0fe38 to your computer and use it in GitHub Desktop.
Save comfuture/2d40d2cc1d659614ec982a7613e0fe38 to your computer and use it in GitHub Desktop.
const jar = {}
function canonical(path) {
// remove trailing slash
if (path[path.length - 1] === '/') {
path = path.substring(0, path.length - 1)
}
return path
}
export function extendRoute(route) {
let path = canonical(route.path)
if (jar.hasOwnProperty(path)) {
route.query = Object.assign({}, jar[path], route.query)
}
return route
}
export default {
beforeRouteLeave(to, from, next) {
// keep query of path
jar[canonical(from.path)] = from.query
next()
},
beforeRouteEnter(to, from, next) {
let path = canonical(to.path)
if (jar.hasOwnProperty(path)) {
// use stored query if exists
let query = Object.assign({}, jar[path], to.query) // merge previous query
let newRoute = Object.assign({}, to, {query})
delete jar[path] // prevent infinity loop gate
next(newRoute)
} else {
next()
}
}
}
<template>
<list-component :items="items" />
<paginate-component :page="page" />
</template>
<script>
import KeepQuery, { extendRoute } from '~/mixins/keepquery'
import api from '~/core/api'
export default {
name: 'media',
mixins: [KeepQuery],
async asyncData({route}) {
route = extendRoute(route)
let {total, items, pageSize} = await api.bulletin.list(route.query.page || 1)
return {total, items, pageSize}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment