sutty-base-jekyll-theme/_packs/controllers/cart_counter_controller.js
Nulo 3a0c41b736 Limpiar eventos cuando el elemento o controlador que los escucha desaparece
Squashed commit of the following:

commit 482eea28821868f03ace33562e7bd34ab9a4478f
Merge: 5f48528 1c128f2
Author: f <f@sutty.nl>
Date:   Thu Nov 25 18:31:35 2021 -0300

    Merge branch 'master' into limpiar-eventos

commit 5f48528c28b0709bd859a4dc52a830f60bfedc6e
Author: f <f@sutty.nl>
Date:   Thu Nov 25 18:23:23 2021 -0300

    pretty

commit 70d05bc90a6cb64d1c4bfc39f48388af3fbc3c18
Merge: c4f33c0 ff1bc21
Author: Nulo <nulo@sutty.nl>
Date:   Thu Oct 28 16:46:31 2021 -0300

    Merge branch 'master' into limpiar-eventos

commit c4f33c084058002a10fc0ec2137ffe045826cfd2
Author: f <f@sutty.nl>
Date:   Thu Oct 28 14:52:41 2021 -0300

    limpiar eventos
2021-11-25 21:40:44 +00:00

39 lines
1,012 B
JavaScript

import { CartBaseController } from "./cart_base_controller";
export default class extends CartBaseController {
static targets = ["counter"];
connect() {
if (!this.hasCounterTarget) {
console.error("Missing counter target");
return;
}
this.cart_count_event = this._cart_count_event.bind(this);
this.storage_event = this._storage_event.bind(this);
window.addEventListener("cart:counter", this.cart_count_event);
window.addEventListener("storage", this.storage_event);
if (!this.cart) return;
this.counter = this.cart.data.attributes.item_count;
}
disconnect() {
window.removeEventListener("cart:counter", this.cart_count_event);
window.removeEventListener("storage", this.storage_event);
}
set counter(quantity) {
this.counterTarget.innerText = quantity;
}
_cart_count_event(event) {
this.counter = event.detail.item_count;
}
_storage_event(event) {
if (event.key == "cart:counter") this.counter = event.newValue;
}
}