count-anything/app/javascript/channels/count_channel.js
Louis Vallat d07857c011
feat: added counts and instant update and destroy
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
2023-02-05 21:01:07 +01:00

38 lines
971 B
JavaScript

import consumer from "./consumer"
const count_channel = consumer.subscriptions.create("CountChannel", {
connected() {
// Called when the subscription is ready for use on the server
console.warn("Connected to CountChannel.");
},
disconnected() {
// Called when the subscription has been terminated by the server
},
received(data) {
// Called when there's incoming data on the websocket for this channel
console.log(data)
if (data.update !== undefined)
this.update(data.update)
if (data.destroy !== undefined)
this.destroy(data.destroy)
},
update(data) {
$("[data-count-id='"+ data.id + "'] .count").text(data.count)
$("[data-count-id='"+ data.id + "'] .name").text(data.name)
},
destroy(data) {
$("[data-count-id='"+ data + "']").remove()
},
});
$(document).on('turbo:load', function() {
if (count_channel !== undefined && !count_channel.consumer.connection.disconnected) {
$("")
}
})