Skip to content

Commit af890a4

Browse files
committed
basic item/user prefetch
1 parent f519772 commit af890a4

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

src/store/api.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export function fetchItems (ids) {
7272
return Promise.all(ids.map(id => fetchItem(id)))
7373
}
7474

75+
export function fetchUser (id) {
76+
return fetch(`user/${id}`)
77+
}
78+
7579
export function watchList (type, cb) {
7680
let first = true
7781
const ref = api.child(`${type}stories`)

src/store/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
3-
import { fetchIdsByType, fetchItem, fetchItems } from './api'
3+
import { fetchItem, fetchItems, fetchIdsByType, fetchUser } from './api'
44

55
Vue.use(Vuex)
66

77
const store = new Vuex.Store({
88
state: {
99
activeType: null,
1010
itemsPerPage: 20,
11-
// fetched items by id. This also serves as a cache to some extent
1211
items: {/* [id: number]: Item */},
13-
// the id lists for each type of stories
14-
// will be periodically updated in realtime
12+
users: {/* [id: string]: User */},
1513
lists: {
16-
top: [],
14+
top: [/* number */],
1715
new: [],
1816
show: [],
1917
ask: [],
@@ -45,6 +43,12 @@ const store = new Vuex.Store({
4543
} else {
4644
return Promise.resolve()
4745
}
46+
},
47+
48+
FETCH_USER: ({ commit, state }, { id }) => {
49+
return state.users[id]
50+
? Promise.resolve(state.users[id])
51+
: fetchUser(id).then(user => commit('SET_USER', { user }))
4852
}
4953
},
5054

@@ -63,6 +67,10 @@ const store = new Vuex.Store({
6367
Vue.set(state.items, item.id, item)
6468
}
6569
})
70+
},
71+
72+
SET_USER: (state, { user }) => {
73+
Vue.set(state.users, user.id, user)
6674
}
6775
},
6876

src/views/ItemView.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
</template>
77

88
<script>
9+
function fetchItem (store) {
10+
return store.dispatch('FETCH_ITEMS', {
11+
ids: [store.state.route.params.id]
12+
})
13+
}
14+
915
export default {
1016
name: 'item-view',
1117
computed: {
1218
item () {
1319
return this.$store.state.items[this.$route.params.id]
1420
}
1521
},
22+
preFetch: fetchItem,
1623
beforeMount () {
17-
this.$store.dispatch('FETCH_ITEMS', {
18-
ids: [this.$route.params.id]
19-
})
24+
fetchItem(this.$store)
2025
}
2126
}
2227
</script>

src/views/UserView.vue

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
<template>
22
<div class="user-view">
3-
<h2>User!</h2>
3+
<h2 v-if="user">User: {{ user.id }}</h2>
44
</div>
55
</template>
66

77
<script>
8+
function fetchUser (store) {
9+
return store.dispatch('FETCH_USER', {
10+
id: store.state.route.params.id
11+
})
12+
}
13+
814
export default {
915
name: 'user-view',
10-
11-
preFetch (store) {
12-
return store.dispatch('FETCH_USER', {
13-
id: store.state.route.params.id
14-
})
16+
computed: {
17+
user () {
18+
return this.$store.state.users[this.$route.params.id]
19+
}
1520
},
16-
17-
mounted () {
18-
21+
preFetch: fetchUser,
22+
beforeMount () {
23+
fetchUser(this.$store)
1924
}
2025
}
2126
</script>

0 commit comments

Comments
 (0)