thread.handlebars
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<style>
.card {
padding: 10px;
margin-bottom: 10px;
}
</style>
<section id="app" style="max-width: 600px;margin: 10px auto 0;">
<div class="card">
<h3 class="card-title">
{{thread.title}}
</h3>
<h6 class="card-subtitle text-muted">#1 {{thread.writer}} {{dateFromObjectId thread._id}}</h6>
<p class="card-text">{{thread.content}}</p>
</div>
<div class="card" v-for="thread in threads">
<h6 class="card-subtitle text-muted">\{{thread.writer}} \{{dateFromObjectId(thread._id)}}</h6>
<p class="card-text">\{{thread.content}}</p>
</div>
<textarea class="form-control" id="input-content" v-model="content" rows="5" required></textarea>
<button class="btn btn-primary" @click="write()">작성</button>
</section>
<script src="https://unpkg.com/vue@next"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
let socket;
let app = {
data() {
return {
threads: [],
content: ''
}
},
methods: {
dateFromObjectId(o) {
o = new Date(parseInt(o.toString().substring(0, 8), 16) * 1000);
return `${o.getFullYear()}-${o.getMonth()}-${o.getDate()} ${o.getHours()}:${o.getMinutes()}`;
},
write() {
socket.emit('write', this.content);
this.content = '';
}
},
created() {
socket = io();
socket.emit('init', '{{thread._id}}');
socket.on('thread', (thread) => {
this.threads.push(thread);
});
}
};
Vue.createApp(app).mount('#app');
</script>