11<template >
22 <div class =" item-view" v-if =" item" >
3- <spinner :show =" !item" ></spinner >
43 <template v-if =" item " >
54 <div class =" item-view-header" >
65 <a :href =" item.url" target =" _blank" >
1817 <div class =" item-view-comments" >
1918 <p class =" item-view-comments-header" >
2019 {{ item.kids ? item.descendants + ' comments' : 'No comments yet.'}}
20+ <spinner :show =" loading" ></spinner >
2121 </p >
22- <ul v-if =" item.kids " class =" comment-children" >
22+ <ul v-if =" !loading " class =" comment-children" >
2323 <comment v-for =" id in item.kids" :id =" id" ></comment >
2424 </ul >
2525 </div >
@@ -37,22 +37,49 @@ function fetchItem (store) {
3737 })
3838}
3939
40+ // recursively fetch all descendent comments
41+ function fetchComments (store , item ) {
42+ if (item .kids ) {
43+ return store .dispatch (' FETCH_ITEMS' , {
44+ ids: item .kids
45+ }).then (() => Promise .all (item .kids .map (id => {
46+ return fetchComments (store, store .state .items [id])
47+ })))
48+ }
49+ }
50+
51+ function fetchItemAndComments (store ) {
52+ return fetchItem (store).then (() => {
53+ const { items , route } = store .state
54+ return fetchComments (store, items[route .params .id ])
55+ })
56+ }
57+
4058export default {
4159 name: ' item-view' ,
4260 components: { Spinner, Comment },
61+ data () {
62+ return {
63+ loading: true
64+ }
65+ },
4366 computed: {
4467 item () {
4568 return this .$store .state .items [this .$route .params .id ]
4669 }
4770 },
71+ // on the server, only fetch the item itself
4872 preFetch: fetchItem,
73+ // on the client, fetch everything
4974 beforeMount () {
50- fetchItem (this .$store )
75+ fetchItemAndComments (this .$store ).then (() => {
76+ this .loading = false
77+ })
5178 }
5279}
5380 </script >
5481
55- <style lang="stylus">
82+ <style lang="stylus">
5683.item-view-header
5784 background-color #f f f
5885 padding 1.8em 2em 1em
@@ -71,17 +98,23 @@ export default {
7198 background-color #f f f
7299 margin-top 10px
73100 padding 0 2em
74-
101+
75102.item-view-comments-header
76103 margin 0
77104 font-size 1.1em
78105 padding 1em 0
106+ position relative
107+ .spinner
108+ position absolute
109+ top 0
110+ right 0
111+ bottom auto
79112
80113.comment-children
81114 list-style-type none
82115 padding 0
83116 margin 0
84-
117+
85118@media (max-width 600px )
86119 .item-view-header
87120 h1
0 commit comments