| 1 |
<!-- https://github.com/vuejs/vue-hackernews/raw/gh-pages/src/components/ItemView.vue --> |
| 2 |
<template> |
| 3 |
<div class="item-view" v-show="item"> |
| 4 |
<item :item="item"></item> |
| 5 |
<p class="itemtext" v-if="hasText" v-html="item.text"></p> |
| 6 |
<ul class="poll-options" v-if="pollOptions"> |
| 7 |
<li v-for="option in pollOptions"> |
| 8 |
<p>{{option.text}}</p> |
| 9 |
<p class="subtext">{{option.score}} points</p> |
| 10 |
</li> |
| 11 |
</ul> |
| 12 |
<ul class="comments" v-if="comments"> |
| 13 |
<comment |
| 14 |
v-for="comment in comments" |
| 15 |
:comment="comment"> |
| 16 |
</comment> |
| 17 |
</ul> |
| 18 |
<p v-show="!comments.length && !isJob">No comments yet.</p> |
| 19 |
</div> |
| 20 |
</template> |
| 21 |
|
| 22 |
<script> |
| 23 |
import store from '../store' |
| 24 |
import Item from './Item.vue' |
| 25 |
import Comment from './Comment.vue' |
| 26 |
|
| 27 |
export default { |
| 28 |
|
| 29 |
name: 'ItemView', |
| 30 |
|
| 31 |
components: { |
| 32 |
Item, |
| 33 |
Comment |
| 34 |
}, |
| 35 |
|
| 36 |
data () { |
| 37 |
return { |
| 38 |
item: {}, |
| 39 |
comments: [], |
| 40 |
pollOptions: null |
| 41 |
} |
| 42 |
}, |
| 43 |
|
| 44 |
route: { |
| 45 |
data ({ to }) { |
| 46 |
return store.fetchItem(to.params.id).then(item => { |
| 47 |
document.title = item.title + ' | Vue.js HN Clone' |
| 48 |
return { |
| 49 |
item, |
| 50 |
// the final resolved data can further contain Promises |
| 51 |
comments: store.fetchItems(item.kids), |
| 52 |
pollOptions: item.type === 'poll' |
| 53 |
? store.fetchItems(item.parts) |
| 54 |
: null |
| 55 |
} |
| 56 |
}) |
| 57 |
} |
| 58 |
}, |
| 59 |
|
| 60 |
computed: { |
| 61 |
isJob () { |
| 62 |
return this.item.type === 'job' |
| 63 |
}, |
| 64 |
|
| 65 |
hasText () { |
| 66 |
return this.item.hasOwnProperty('text') |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
</script> |
| 71 |
|
| 72 |
<style lang="stylus"> |
| 73 |
@import "../variables.styl" |
| 74 |
|
| 75 |
.item-view |
| 76 |
.item |
| 77 |
padding-left 0 |
| 78 |
margin-bottom 30px |
| 79 |
.index |
| 80 |
display none |
| 81 |
.poll-options |
| 82 |
margin-left 30px |
| 83 |
margin-bottom 40px |
| 84 |
li |
| 85 |
margin 12px 0 |
| 86 |
p |
| 87 |
margin 8px 0 |
| 88 |
.subtext |
| 89 |
color $gray |
| 90 |
font-size 11px |
| 91 |
.itemtext |
| 92 |
color $gray |
| 93 |
margin-top 0 |
| 94 |
margin-bottom 30px |
| 95 |
.itemtext p |
| 96 |
margin 10px 0 |
| 97 |
</style> |