sutty-base-jekyll-theme/_packs/controllers/cart_counter_controller.js

26 lines
646 B
JavaScript
Raw Normal View History

2021-06-01 21:33:49 +00:00
import { CartBaseController } from './cart_base_controller'
export default class extends CartBaseController {
static targets = [ 'counter' ]
connect () {
if (!this.hasCounterTarget) {
console.error("Missing counter target")
return
}
window.addEventListener('cart:counter', event => this.counter = event.detail.item_count)
window.addEventListener('storage', event => {
if (event.key == 'cart:counter') this.counter = event.newValue
})
if (!this.cart) return
this.counter = this.cart.data.attributes.item_count
}
set counter (quantity) {
this.counterTarget.innerText = quantity
}
}