count-anything/app/javascript/channels/count_channel.js

38 lines
971 B
JavaScript
Raw Normal View History

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) {
$("")
}
})