Skip to content

Commit 7473669

Browse files
committed
mutiple lists
1 parent 144379b commit 7473669

File tree

6 files changed

+51
-52
lines changed

6 files changed

+51
-52
lines changed

src/App.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ body
2525
width 30px
2626
2727
.view
28-
transition all .35s ease
28+
transition all .2s ease
2929
3030
.view-enter, .view-leave-active
3131
opacity 0

src/components/NewsList.vue

+18-22
Original file line numberDiff line numberDiff line change
@@ -44,50 +44,46 @@ export default {
4444
data () {
4545
return {
4646
loading: false,
47-
displayedPage: -1,
48-
displayedItems: [],
49-
transition: 'slide-left'
47+
transition: 'slide-left',
48+
displayedPage: Number(this.$store.state.route.params.page) || 1,
49+
displayedItems: this.$store.getters.activeItems
5050
}
5151
},
5252
5353
computed: {
54-
activeItems () {
55-
return this.$store.getters.activeItems
56-
},
5754
page () {
5855
return Number(this.$store.state.route.params.page) || 1
5956
},
6057
maxPage () {
61-
const { itemsPerPage, itemIdsByType } = this.$store.state
62-
return Math.floor(itemIdsByType[this.type].length / itemsPerPage)
58+
const { itemsPerPage, lists } = this.$store.state
59+
return Math.ceil(lists[this.type].length / itemsPerPage)
6360
},
6461
hasMore () {
6562
return this.page < this.maxPage
6663
}
6764
},
6865
69-
created () {
70-
this.displayedPage = this.page
71-
this.displayedItems = this.activeItems
72-
},
73-
7466
mounted () {
75-
if (this.page > this.maxPage || this.page < 1) {
76-
this.$router.replace(`/${this.type}/1`)
77-
} else {
78-
fetchInitialData(this.type).then(() => {
79-
this.displayedItems = this.activeItems
80-
})
81-
}
67+
this.loadItems(this.page, -1)
8268
},
8369
8470
watch: {
8571
page (to, from) {
72+
this.loadItems(to, from)
73+
}
74+
},
75+
76+
methods: {
77+
loadItems (to, from) {
8678
this.loading = true
87-
this.$store.dispatch('FETCH_ACTIVE_ITEMS').then(() => {
79+
fetchInitialData(this.type).then(() => {
80+
if (this.page < 0 || this.page > this.maxPage) {
81+
this.$router.replace(`/${this.type}/1`)
82+
return
83+
}
8884
this.transition = to > from ? 'slide-left' : 'slide-right'
8985
this.displayedPage = to
90-
this.displayedItems = this.activeItems
86+
this.displayedItems = this.$store.getters.activeItems
9187
this.loading = false
9288
})
9389
}

src/router/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import Router from 'vue-router'
33

44
Vue.use(Router)
55

6-
import { createStoriesView } from '../views/CreateStoriesView'
6+
import { createListView } from '../views/CreateListView'
77
import About from '../views/About.vue'
88

99
export default new Router({
1010
mode: 'history',
1111
routes: [
12-
{ path: '/top/:page(\\d+)?', component: createStoriesView('top') },
13-
{ path: '/new/:page(\\d+)?', component: createStoriesView('new') },
14-
{ path: '/show/:page(\\d+)?', component: createStoriesView('show') },
15-
{ path: '/ask/:page(\\d+)?', component: createStoriesView('ask') },
16-
{ path: '/job/:page(\\d+)?', component: createStoriesView('job') },
12+
{ path: '/top/:page(\\d+)?', component: createListView('top') },
13+
{ path: '/new/:page(\\d+)?', component: createListView('new') },
14+
{ path: '/show/:page(\\d+)?', component: createListView('show') },
15+
{ path: '/ask/:page(\\d+)?', component: createListView('ask') },
16+
{ path: '/job/:page(\\d+)?', component: createListView('job') },
1717
{ path: '/about', component: About },
1818
{ path: '*', redirect: '/top/1' }
1919
]

src/store/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ const store = new Vuex.Store({
88
state: {
99
activeType: null,
1010
itemsPerPage: 20,
11-
// the current items being displayed
12-
activeItemIds: [/* number */],
1311
// fetched items by id. This also serves as a cache to some extent
1412
items: {/* [id: number]: Item */},
1513
// the id lists for each type of stories
1614
// will be periodically updated in realtime
17-
itemIdsByType: {
15+
lists: {
1816
top: [],
1917
new: [],
2018
show: [],
@@ -42,7 +40,7 @@ const store = new Vuex.Store({
4240
state.activeType = type
4341
},
4442
SET_IDS: (state, { type, ids }) => {
45-
state.itemIdsByType[type] = ids
43+
state.lists[type] = ids
4644
},
4745
SET_ITEMS: (state, { items }) => {
4846
items.forEach(item => {
@@ -53,12 +51,12 @@ const store = new Vuex.Store({
5351

5452
getters: {
5553
activeIds (state) {
56-
const { activeType, itemsPerPage, itemIdsByType } = state
54+
const { activeType, itemsPerPage, lists } = state
5755
const page = Number(state.route.params.page) || 1
5856
if (activeType) {
5957
const start = (page - 1) * itemsPerPage
6058
const end = page * itemsPerPage
61-
return itemIdsByType[activeType].slice(start, end)
59+
return lists[activeType].slice(start, end)
6260
} else {
6361
return []
6462
}

src/views/CreateListView.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import NewsList from '../components/NewsList.vue'
2+
import { fetchInitialData } from '../store'
3+
4+
// factory function for creating root-level list views
5+
// since they share most of the logic except for the type of items to display.
6+
export function createListView (type) {
7+
return {
8+
name: `${type}-stories`,
9+
components: {
10+
NewsList
11+
},
12+
prefetch () {
13+
return fetchInitialData(type)
14+
},
15+
created () {
16+
this.$store.commit('SET_ACTIVE_TYPE', { type })
17+
},
18+
render (h) {
19+
return h(NewsList, { props: { type }})
20+
}
21+
}
22+
}

src/views/CreateStoriesView.js

-17
This file was deleted.

0 commit comments

Comments
 (0)