-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatVue.js
40 lines (40 loc) · 942 Bytes
/
chatVue.js
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
var socket = null;
var app = new Vue({
// State 0: select username
// State 1: chat application
el: '#app',
data: {
messages: [],
message: '',
username: '',
state: 0
},
methods: {
sendMessage: function () {
socket.emit('message', this.message);
this.message = '';
},
setUsername: function () {
socket.emit('join', this.username);
this.username = '';
this.state = 1;
},
continueWithoutUsername: function () {
socket.emit('join', null);
this.state = 1;
}
},
created: function () {
socket = io();
},
mounted: function () {
socket.on('message', function (message) {
app.messages.push(message);
// this needs to be done AFTER vue updates the page!!
app.$nextTick(function () {
var messageBox = document.getElementById('chatbox');
messageBox.scrollTop = messageBox.scrollHeight;
});
});
}
});