I have a messages.js.erb file in the app / assets / javascripts / channels folder that needs to generate code to select each of the chat rooms and create channel subscriptions for this chat.
Here is my messages.js.erb file
$(document).on('turbolinks:load', function() {
submitNewMessage();
});
<% Chatroom.all.each do |chatroom| %>
App['room' + <%=chatroom.id%>] = App.cable.subscriptions.create({channel: 'MessagesChannel', room: <%=chatroom.id%>}, {
received: function(data) {
$("[data-chatroom='" + this.chatroomId + "']").removeClass('hidden');
return $("[data-chatroom='" + this.chatroomId + "']").append(data.message);
},
setChatroomId: function(chatroomId) {
this.chatroomId = chatroomId;
}
});
<% end %>
function submitNewMessage(){
$('textarea#message_content').keydown(function(event) {
if (event.keyCode === 13) {
var msg = event.target.value;
var chatroomId = $("[data-chatroom]").data().chatroom;
var uid = $('#uid').val();
App['room' + chatroomId].setChatroomId(chatroomId);
App['room' + chatroomId].send({message: msg, chatroom_id: chatroomId, user_id: uid});
$('[data-textarea="message"]').val(" ");
return false;
}
});
}
Preferably, this file should be reloaded every time a new chat is created, but the rails cache ( precompiling?
) the file ( asset?
) after the first download and serve it with every request.
, , , , , (.. , ), ( , something
) , , , Chatroom
, -.
, ( ) , ( ),
Uncaught TypeError: 'setChatroomId' undefined HTMLTextAreaElement. (Messages.self-cbf13df66ead12b7956faa24e880ce07c0289634216a096fafd4ac1c6d262f43.js = 1: 436) HTMLTextAreaElement.dispatch(jquery-1.11.3.min.self-ad66c584c2469d2bb527ba7aef5893549f1554ccc8ab3ccb43bd09ce8c3bf7a1.js? body = 1: 5) HTMLTextAreaElement.r.handle(jquery-1.11.3.min.self-ad66c584c2469d2bb527ba7aef5893549f1554ccc8ab3ccb43bd09ce8c3bf7a1.js? body = 1: 5)
( ). :
messages.js ( , )
$(document).on('turbolinks:load', function() {
submitNewMessage();
});
App['room' + 1] = App.cable.subscriptions.create({channel: 'MessagesChannel', room: 1}, {
received: function(data) {
$("[data-chatroom='" + this.chatroomId + "']").removeClass('hidden');
return $("[data-chatroom='" + this.chatroomId + "']").append(data.message);
},
setChatroomId: function(chatroomId) {
this.chatroomId = chatroomId;
}
});
.
.
.
.
.
.
.
App['room' + 37] = App.cable.subscriptions.create({channel: 'MessagesChannel', room: 37}, {
received: function(data) {
$("[data-chatroom='" + this.chatroomId + "']").removeClass('hidden');
return $("[data-chatroom='" + this.chatroomId + "']").append(data.message);
},
setChatroomId: function(chatroomId) {
this.chatroomId = chatroomId;
}
});
function submitNewMessage(){
$('textarea#message_content').keydown(function(event) {
if (event.keyCode === 13) {
var msg = event.target.value;
var chatroomId = $("[data-chatroom]").data().chatroom;
var uid = $('#uid').val();
App['room' + chatroomId].setChatroomId(chatroomId);
App['room' + chatroomId].send({message: msg, chatroom_id: chatroomId, user_id: uid});
$('[data-textarea="message"]').val(" ");
return false;
}
});
}
, , App['room' + 38]
id 38, ( id > 37)
, , . , ? , , / , ?