Just wondering if anyone else had come across the problem of firing events from a controller’s disconnect method?
I have a nested form where the parent controller needs to know when child controllers have been added and removed from the DOM so it can update its total.
Parent: line_items
connect() {
this.element.addEventListener("lineItem:added", this.calculate.bind(this));
this.element.addEventListener("lineItem:removed", this.calculate.bind(this));
}
Child: line_item
connect() {
fire(this.element, "lineItem:added");
}
disconnect() {
// This does not get fired because the element
// has already been removed from the DOM
fire(this.element, "lineItem:removed");
}
To get around this, I have been storing this.element.parentNode
on connect and then firing the event on the parentNode on disconnect, but this feels a bit clunky.
Child: line_item
connect() {
this.parent = this.element.parentNode;
fire(this.element, "lineItem:added");
}
disconnect() {
fire(this.parent, "lineItem:removed");
}