Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sample
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
周韬
node-sample
Commits
af890a47
Commit
af890a47
authored
Aug 09, 2016
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
basic item/user prefetch
parent
f519772c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
17 deletions
+39
-17
api.js
src/store/api.js
+4
-0
index.js
src/store/index.js
+13
-5
ItemView.vue
src/views/ItemView.vue
+8
-3
UserView.vue
src/views/UserView.vue
+14
-9
No files found.
src/store/api.js
View file @
af890a47
...
@@ -72,6 +72,10 @@ export function fetchItems (ids) {
...
@@ -72,6 +72,10 @@ export function fetchItems (ids) {
return
Promise
.
all
(
ids
.
map
(
id
=>
fetchItem
(
id
)))
return
Promise
.
all
(
ids
.
map
(
id
=>
fetchItem
(
id
)))
}
}
export
function
fetchUser
(
id
)
{
return
fetch
(
`user/
${
id
}
`
)
}
export
function
watchList
(
type
,
cb
)
{
export
function
watchList
(
type
,
cb
)
{
let
first
=
true
let
first
=
true
const
ref
=
api
.
child
(
`
${
type
}
stories`
)
const
ref
=
api
.
child
(
`
${
type
}
stories`
)
...
...
src/store/index.js
View file @
af890a47
import
Vue
from
'vue'
import
Vue
from
'vue'
import
Vuex
from
'vuex'
import
Vuex
from
'vuex'
import
{
fetchI
dsByType
,
fetchItem
,
fetchItems
}
from
'./api'
import
{
fetchI
tem
,
fetchItems
,
fetchIdsByType
,
fetchUser
}
from
'./api'
Vue
.
use
(
Vuex
)
Vue
.
use
(
Vuex
)
...
@@ -8,12 +8,10 @@ const store = new Vuex.Store({
...
@@ -8,12 +8,10 @@ const store = new Vuex.Store({
state
:
{
state
:
{
activeType
:
null
,
activeType
:
null
,
itemsPerPage
:
20
,
itemsPerPage
:
20
,
// fetched items by id. This also serves as a cache to some extent
items
:
{
/* [id: number]: Item */
},
items
:
{
/* [id: number]: Item */
},
// the id lists for each type of stories
users
:
{
/* [id: string]: User */
},
// will be periodically updated in realtime
lists
:
{
lists
:
{
top
:
[],
top
:
[
/* number */
],
new
:
[],
new
:
[],
show
:
[],
show
:
[],
ask
:
[],
ask
:
[],
...
@@ -45,6 +43,12 @@ const store = new Vuex.Store({
...
@@ -45,6 +43,12 @@ const store = new Vuex.Store({
}
else
{
}
else
{
return
Promise
.
resolve
()
return
Promise
.
resolve
()
}
}
},
FETCH_USER
:
({
commit
,
state
},
{
id
})
=>
{
return
state
.
users
[
id
]
?
Promise
.
resolve
(
state
.
users
[
id
])
:
fetchUser
(
id
).
then
(
user
=>
commit
(
'SET_USER'
,
{
user
}))
}
}
},
},
...
@@ -63,6 +67,10 @@ const store = new Vuex.Store({
...
@@ -63,6 +67,10 @@ const store = new Vuex.Store({
Vue
.
set
(
state
.
items
,
item
.
id
,
item
)
Vue
.
set
(
state
.
items
,
item
.
id
,
item
)
}
}
})
})
},
SET_USER
:
(
state
,
{
user
})
=>
{
Vue
.
set
(
state
.
users
,
user
.
id
,
user
)
}
}
},
},
...
...
src/views/ItemView.vue
View file @
af890a47
...
@@ -6,6 +6,12 @@
...
@@ -6,6 +6,12 @@
</
template
>
</
template
>
<
script
>
<
script
>
function
fetchItem
(
store
)
{
return
store
.
dispatch
(
'FETCH_ITEMS'
,
{
ids
:
[
store
.
state
.
route
.
params
.
id
]
})
}
export
default
{
export
default
{
name
:
'item-view'
,
name
:
'item-view'
,
computed
:
{
computed
:
{
...
@@ -13,10 +19,9 @@ export default {
...
@@ -13,10 +19,9 @@ export default {
return
this
.
$store
.
state
.
items
[
this
.
$route
.
params
.
id
]
return
this
.
$store
.
state
.
items
[
this
.
$route
.
params
.
id
]
}
}
},
},
preFetch
:
fetchItem
,
beforeMount
()
{
beforeMount
()
{
this
.
$store
.
dispatch
(
'FETCH_ITEMS'
,
{
fetchItem
(
this
.
$store
)
ids
:
[
this
.
$route
.
params
.
id
]
})
}
}
}
}
</
script
>
</
script
>
src/views/UserView.vue
View file @
af890a47
<
template
>
<
template
>
<div
class=
"user-view"
>
<div
class=
"user-view"
>
<h2
>
User!
</h2>
<h2
v-if=
"user"
>
User:
{{
user
.
id
}}
</h2>
</div>
</div>
</
template
>
</
template
>
<
script
>
<
script
>
function
fetchUser
(
store
)
{
return
store
.
dispatch
(
'FETCH_USER'
,
{
id
:
store
.
state
.
route
.
params
.
id
})
}
export
default
{
export
default
{
name
:
'user-view'
,
name
:
'user-view'
,
computed
:
{
preFetch
(
store
)
{
user
()
{
return
store
.
dispatch
(
'FETCH_USER'
,
{
return
this
.
$store
.
state
.
users
[
this
.
$route
.
params
.
id
]
id
:
store
.
state
.
route
.
params
.
id
}
})
},
},
preFetch
:
fetchUser
,
mounted
()
{
beforeMount
()
{
fetchUser
(
this
.
$store
)
}
}
}
}
</
script
>
</
script
>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment