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
90411aad
Commit
90411aad
authored
Nov 02, 2016
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve comments loading perf
parent
6e8987ce
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
8 deletions
+36
-8
Comment.vue
src/components/Comment.vue
+0
-5
ItemView.vue
src/views/ItemView.vue
+36
-3
No files found.
src/components/Comment.vue
View file @
90411aad
...
@@ -30,11 +30,6 @@ export default {
...
@@ -30,11 +30,6 @@ export default {
return
this
.
$store
.
state
.
items
[
this
.
id
]
return
this
.
$store
.
state
.
items
[
this
.
id
]
}
}
},
},
beforeMount
()
{
this
.
$store
.
dispatch
(
'FETCH_ITEMS'
,
{
ids
:
[
this
.
id
]
})
},
methods
:
{
methods
:
{
pluralize
(
n
)
{
pluralize
(
n
)
{
return
n
+
(
n
===
1
?
' reply'
:
' replies'
)
return
n
+
(
n
===
1
?
' reply'
:
' replies'
)
...
...
src/views/ItemView.vue
View file @
90411aad
<
template
>
<
template
>
<div
class=
"item-view"
v-if=
"item"
>
<div
class=
"item-view"
v-if=
"item"
>
<spinner
:show=
"!item"
></spinner>
<template
v-if=
"item"
>
<template
v-if=
"item"
>
<div
class=
"item-view-header"
>
<div
class=
"item-view-header"
>
<a
:href=
"item.url"
target=
"_blank"
>
<a
:href=
"item.url"
target=
"_blank"
>
...
@@ -18,8 +17,9 @@
...
@@ -18,8 +17,9 @@
<div
class=
"item-view-comments"
>
<div
class=
"item-view-comments"
>
<p
class=
"item-view-comments-header"
>
<p
class=
"item-view-comments-header"
>
{{
item
.
kids
?
item
.
descendants
+
' comments'
:
'No comments yet.'
}}
{{
item
.
kids
?
item
.
descendants
+
' comments'
:
'No comments yet.'
}}
<spinner
:show=
"loading"
></spinner>
</p>
</p>
<ul
v-if=
"
item.kids
"
class=
"comment-children"
>
<ul
v-if=
"
!loading
"
class=
"comment-children"
>
<comment
v-for=
"id in item.kids"
:id=
"id"
></comment>
<comment
v-for=
"id in item.kids"
:id=
"id"
></comment>
</ul>
</ul>
</div>
</div>
...
@@ -37,17 +37,44 @@ function fetchItem (store) {
...
@@ -37,17 +37,44 @@ function fetchItem (store) {
})
})
}
}
// recursively fetch all descendent comments
function
fetchComments
(
store
,
item
)
{
if
(
item
.
kids
)
{
return
store
.
dispatch
(
'FETCH_ITEMS'
,
{
ids
:
item
.
kids
}).
then
(()
=>
Promise
.
all
(
item
.
kids
.
map
(
id
=>
{
return
fetchComments
(
store
,
store
.
state
.
items
[
id
])
})))
}
}
function
fetchItemAndComments
(
store
)
{
return
fetchItem
(
store
).
then
(()
=>
{
const
{
items
,
route
}
=
store
.
state
return
fetchComments
(
store
,
items
[
route
.
params
.
id
])
})
}
export
default
{
export
default
{
name
:
'item-view'
,
name
:
'item-view'
,
components
:
{
Spinner
,
Comment
},
components
:
{
Spinner
,
Comment
},
data
()
{
return
{
loading
:
true
}
},
computed
:
{
computed
:
{
item
()
{
item
()
{
return
this
.
$store
.
state
.
items
[
this
.
$route
.
params
.
id
]
return
this
.
$store
.
state
.
items
[
this
.
$route
.
params
.
id
]
}
}
},
},
// on the server, only fetch the item itself
preFetch
:
fetchItem
,
preFetch
:
fetchItem
,
// on the client, fetch everything
beforeMount
()
{
beforeMount
()
{
fetchItem
(
this
.
$store
)
fetchItemAndComments
(
this
.
$store
).
then
(()
=>
{
this
.
loading
=
false
})
}
}
}
}
</
script
>
</
script
>
...
@@ -76,6 +103,12 @@ export default {
...
@@ -76,6 +103,12 @@ export default {
margin 0
margin 0
font-size 1.1em
font-size 1.1em
padding 1em 0
padding 1em 0
position relative
.spinner
position absolute
top 0
right 0
bottom auto
.comment-children
.comment-children
list-style-type none
list-style-type none
...
...
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