質問編集履歴

3

補足

2020/01/21 21:20

投稿

raurauji
raurauji

スコア13

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ```
12
12
 
13
- エラーは起こっていないのですが、自分が送ったメッセージが相手側の画面上に非同期で更新される機能がうまく行きません。
13
+ エラーは起こっていないのですが、自分が送ったメッセージが相手側の画面上に非同期で更新される機能がうまく行きません。リロードすれば両者ともメッセージは表示されます。
14
14
 
15
15
  ```
16
16
 

2

ソースコードの追加

2020/01/21 21:20

投稿

raurauji
raurauji

スコア13

test CHANGED
File without changes
test CHANGED
@@ -16,6 +16,194 @@
16
16
 
17
17
  ###ソースコード
18
18
 
19
+
20
+
21
+ ###app/assets/javascripts/message.js
22
+
23
+ ```
24
+
25
+ $(function(){
26
+
27
+ function buildHTML(message){
28
+
29
+ if ( message.image ) {
30
+
31
+ var html =
32
+
33
+ `<div class="main-chat__message-list__message" data-message-id=${message.id}>
34
+
35
+ <div class="main-chat__message-list__message__top">
36
+
37
+ <div class="main-chat__message-list__message__top__name">
38
+
39
+ ${message.user_name}
40
+
41
+ </div>
42
+
43
+ <div class="main-chat__message-list__message__top__date">
44
+
45
+ ${message.created_at.strftime("%Y年%m月%d日 %H時%M分")}
46
+
47
+ </div>
48
+
49
+ </div>
50
+
51
+ <div class="main-chat__message-list__message__bottom">
52
+
53
+ <p class="main-chat__message-list__message__bottom__text">
54
+
55
+ ${message.content}
56
+
57
+ </p>
58
+
59
+ </div>
60
+
61
+ <img src=${message.image} >
62
+
63
+ </div>`
64
+
65
+ return html;
66
+
67
+ } else {
68
+
69
+ var html =
70
+
71
+ `<div class="main-chat__message-list__message" data-message-id=${message.id}>
72
+
73
+ <div class="main-chat__message-list__message__top">
74
+
75
+ <div class="main-chat__message-list__message__top__name">
76
+
77
+ ${message.user_name}
78
+
79
+ </div>
80
+
81
+ <div class="main-chat__message-list__message__top__date">
82
+
83
+ ${message.created_at}
84
+
85
+ </div>
86
+
87
+ </div>
88
+
89
+ <div class="main-chat__message-list__message__bottom">
90
+
91
+ <p class="main-chat__message-list__message__bottom__text">
92
+
93
+ ${message.content}
94
+
95
+ </p>
96
+
97
+ </div>
98
+
99
+ </div>`
100
+
101
+ return html;
102
+
103
+ };
104
+
105
+ }
106
+
107
+ $('#new_message').on('submit', function(e){
108
+
109
+ e.preventDefault();
110
+
111
+ var formData = new FormData(this);
112
+
113
+ var url = $(this).attr('action')
114
+
115
+ $.ajax({
116
+
117
+ url: url,
118
+
119
+ type: "POST",
120
+
121
+ data: formData,
122
+
123
+ dataType: 'json',
124
+
125
+ processData: false,
126
+
127
+ contentType: false
128
+
129
+ })
130
+
131
+ .done(function(data){
132
+
133
+ var html = buildHTML(data);
134
+
135
+ $('.main-chat__message-list').append(html);
136
+
137
+ $('.main-chat__message-list').animate({ scrollTop: $('.main-chat__message-list')[0].scrollHeight});
138
+
139
+ $('form')[0].reset();
140
+
141
+ $('.form__submit').prop('disabled', false);
142
+
143
+ })
144
+
145
+
146
+
147
+ var reloadMessages = function() {
148
+
149
+ last_message_id = $('.main-chat__message-list__message:last').data("message-id");
150
+
151
+ $.ajax({
152
+
153
+ url: "api/messages",
154
+
155
+ type: 'get',
156
+
157
+ dataType: 'json',
158
+
159
+ data: {id: last_message_id}
160
+
161
+ })
162
+
163
+ .done(function(messages) {
164
+
165
+ if (messages.length !== 0) {
166
+
167
+ var insertHTML = '';
168
+
169
+ $.each(messages, function(i, message) {
170
+
171
+ insertHTML += buildHTML(message)
172
+
173
+ });
174
+
175
+ $('.main-chat__message-list').append(insertHTML);
176
+
177
+ $('.main-chat__message-list').animate({ scrollTop: $('.main-chat__message-list')[0].scrollHeight});
178
+
179
+ $('#new_message')[0].reset();
180
+
181
+ $('.form__submit').prop('disabled', false);
182
+
183
+ }
184
+
185
+ })
186
+
187
+ .fail(function() {
188
+
189
+ console.log('error');
190
+
191
+ });
192
+
193
+ if (document.location.href.match(//groups/\d+/messages/)) {
194
+
195
+ setInterval(reloadMessages, 7000);
196
+
197
+ }
198
+
199
+ };
200
+
201
+ });
202
+
203
+ });
204
+
205
+
206
+
19
207
  ```
20
208
 
21
209
  githubのリンクを貼っておきます。"JavaScript/message.js"に重点をおいて見ていただけるとわかりやすいかと思います。
@@ -23,5 +211,3 @@
23
211
  https://github.com/momotarou1090/chat-space3.git
24
212
 
25
213
  ブランチはauto-renewブランチです。
26
-
27
- ```

1

補足

2020/01/21 21:08

投稿

raurauji
raurauji

スコア13

test CHANGED
File without changes
test CHANGED
@@ -22,4 +22,6 @@
22
22
 
23
23
  https://github.com/momotarou1090/chat-space3.git
24
24
 
25
+ ブランチはauto-renewブランチです。
26
+
25
27
  ```