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
01d04adc
Commit
01d04adc
authored
Apr 22, 2017
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
handle view param changes and user not found
parent
8255a01b
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
55 additions
and
23 deletions
+55
-23
entry-client.js
src/entry-client.js
+19
-6
entry-server.js
src/entry-server.js
+4
-4
actions.js
src/store/actions.js
+1
-1
mutations.js
src/store/mutations.js
+2
-2
CreateListView.js
src/views/CreateListView.js
+2
-2
ItemView.vue
src/views/ItemView.vue
+18
-5
UserView.vue
src/views/UserView.vue
+9
-3
No files found.
src/entry-client.js
View file @
01d04adc
...
...
@@ -2,15 +2,17 @@ import Vue from 'vue'
import
'es6-promise/auto'
import
{
createApp
}
from
'./app'
// a global mixin t
o invoke fetchData on the client
// a global mixin t
hat calls `asyncData` when a route component's params change
Vue
.
mixin
({
before
Mount
(
)
{
before
RouteUpdate
(
to
,
from
,
next
)
{
const
{
asyncData
}
=
this
.
$options
if
(
asyncData
)
{
this
.
dataPromise
=
asyncData
(
this
.
$store
,
this
.
$route
)
asyncData
({
store
:
this
.
$store
,
route
:
to
}).
then
(
next
)
}
else
{
next
()
}
}
})
...
...
@@ -26,6 +28,17 @@ if (window.__INITIAL_STATE__) {
// wait until router has resolved all async before hooks
// and async components...
router
.
onReady
(()
=>
{
// add router hook for handling asyncData
// doing it after initial route is resolved so that we don't double-fetch
// the data that we already have.
router
.
beforeResolve
((
to
,
from
,
next
)
=>
{
Promise
.
all
(
router
.
getMatchedComponents
(
to
).
map
(
c
=>
{
if
(
c
.
asyncData
)
{
return
c
.
asyncData
({
store
,
route
:
to
})
}
})).
then
(
next
)
})
// actually mount to DOM
app
.
$mount
(
'#app'
)
})
...
...
src/entry-server.js
View file @
01d04adc
...
...
@@ -27,11 +27,11 @@ export default context => {
// which is resolved when the action is complete and store state has been
// updated.
Promise
.
all
(
matchedComponents
.
map
(
component
=>
{
return
component
.
asyncData
&&
component
.
asyncData
(
return
component
.
asyncData
&&
component
.
asyncData
(
{
store
,
router
.
currentRoute
,
context
)
route
:
route
r
.
currentRoute
,
ssrContext
:
context
}
)
})).
then
(()
=>
{
isDev
&&
console
.
log
(
`data pre-fetch:
${
Date
.
now
()
-
s
}
ms`
)
// After all preFetch hooks are resolved, our store is now
...
...
src/store/actions.js
View file @
01d04adc
...
...
@@ -44,6 +44,6 @@ export default {
FETCH_USER
:
({
commit
,
state
},
{
id
})
=>
{
return
state
.
users
[
id
]
?
Promise
.
resolve
(
state
.
users
[
id
])
:
fetchUser
(
id
).
then
(
user
=>
commit
(
'SET_USER'
,
{
user
}))
:
fetchUser
(
id
).
then
(
user
=>
commit
(
'SET_USER'
,
{
id
,
user
}))
}
}
src/store/mutations.js
View file @
01d04adc
...
...
@@ -17,7 +17,7 @@ export default {
})
},
SET_USER
:
(
state
,
{
user
})
=>
{
Vue
.
set
(
state
.
users
,
user
.
id
,
user
)
SET_USER
:
(
state
,
{
id
,
user
})
=>
{
Vue
.
set
(
state
.
users
,
id
,
user
||
false
)
/* false means user not found */
}
}
src/views/CreateListView.js
View file @
01d04adc
...
...
@@ -10,9 +10,9 @@ export default function createListView (type) {
return
{
name
:
`
${
type
}
-stories-view`
,
asyncData
(
store
,
route
,
context
)
{
asyncData
(
{
store
,
ssrContext
}
)
{
return
store
.
dispatch
(
'FETCH_LIST_DATA'
,
{
type
}).
then
(()
=>
{
setTitle
(
camelize
(
type
),
c
ontext
)
setTitle
(
camelize
(
type
),
ssrC
ontext
)
})
},
...
...
src/views/ItemView.vue
View file @
01d04adc
...
...
@@ -46,20 +46,33 @@ export default {
}
},
asyncData
(
store
,
{
params
:
{
id
}},
context
)
{
// We only fetch the item itself before entering the view, because
// it might take a long time to load threads with hundreds of comments
// due to how the HN Firebase API works.
asyncData
({
store
,
route
:
{
params
:
{
id
}},
ssrContext
})
{
return
store
.
dispatch
(
'FETCH_ITEMS'
,
{
ids
:
[
id
]
}).
then
(()
=>
{
const
item
=
store
.
state
.
items
[
id
]
setTitle
(
item
.
title
,
c
ontext
)
setTitle
(
item
.
title
,
ssrC
ontext
)
})
},
//
on the client, fetch all comments
//
Fetch comments when mounted on the client
beforeMount
()
{
this
.
dataPromise
.
then
(()
=>
{
this
.
fetchComments
()
},
// refetch comments if item changed
watch
:
{
item
:
'fetchComments'
},
methods
:
{
fetchComments
()
{
this
.
loading
=
true
fetchComments
(
this
.
$store
,
this
.
item
).
then
(()
=>
{
this
.
loading
=
false
})
}
)
}
}
}
...
...
src/views/UserView.vue
View file @
01d04adc
<
template
>
<div
class=
"user-view"
>
<spinner
:show=
"!user"
></spinner>
<spinner
:show=
"!user
Loaded
"
></spinner>
<template
v-if=
"user"
>
<h1>
User :
{{
user
.
id
}}
</h1>
<ul
class=
"meta"
>
...
...
@@ -13,6 +13,9 @@
<a
:href=
"'https://news.ycombinator.com/threads?id=' + user.id"
>
comments
</a>
</p>
</
template
>
<
template
v-else-if=
"user === false"
>
<h1>
User not found.
</h1>
</
template
>
</div>
</template>
...
...
@@ -27,13 +30,16 @@ export default {
computed
:
{
user
()
{
return
this
.
$store
.
state
.
users
[
this
.
$route
.
params
.
id
]
},
userLoaded
()
{
return
this
.
$route
.
params
.
id
in
this
.
$store
.
state
.
users
}
},
asyncData
(
store
,
{
params
:
{
id
}},
context
)
{
asyncData
(
{
store
,
route
:
{
params
:
{
id
}},
ssrContext
}
)
{
return
store
.
dispatch
(
'FETCH_USER'
,
{
id
}).
then
(()
=>
{
const
user
=
store
.
state
.
users
[
id
]
setTitle
(
user
.
id
,
c
ontext
)
setTitle
(
user
?
user
.
id
:
'User not found'
,
ssrC
ontext
)
})
}
}
...
...
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