Hi,
I’m trying to do a chat application, that when current user is the owner of the message, the massage should show on right, if other use send the massage, it should stay on the left.
I read @dhh saying tht is not possible to send current user on broadcast (Authentication and Devise with broadcasts - #4 by dhh), so I’m trying to add the current user on a parent element of the page in a data attribute and use it on turbo:before-stream-render to add de right class
But I tried to change in so many ways templateElement but don’t found a way that works.
var updateMessages = function(e) {
var user_id = $('#notes-container').data('user');
console.log('a');
var templateContent = $(e.target.templateElement.content);
templateContent.find('.message-row').each(function(idx, el) {
var el_user_id = el.dataset.user;
console.log(el_user_id);
el.classList.add(el_user_id == user_id ? 'right-message' : 'left-message')
});
}
$(document).on('turbo:before-stream-render', updateMessages);
There is a way to change the content of templateElement?
Same code without Jquery
I change updateMessages to not use Jquery
var updateMessages = function(e) {
var user_id = $('#notes-container').data('user');
e.target.templateElement.content.querySelectorAll('.message-row').forEach(function(el, idx) {
var el_user_id = el.dataset.user;
console.log(el_user_id);
el.classList.add(el_user_id == user_id ? 'right-message' : 'left-message')
});
}
Still not working