I'm using StimulusJS for my js and I would like to be able to remove a listener when a user go to a new page.
So far I have created what it seems good for me
import { Controller } from "stimulus"
export default class extends Controller {
connect() {
this.addListenerCloseModal()
}
disconnect() {
this.removeListenerCloseModal()
}
addListenerCloseModal() {
window.addEventListener('click', this.closeModal)
}
removeListenerCloseModal() {
window.removeEventListener('click', this.closeModal, true)
}
closeModal(event) {
var modal = document.getElementById("myModal");
if (event.target == modal) {
modal.style.display = "none";
}
}
}
But the problem is the listener is still there. I have read that comment about bind and this Javascript removeEventListener not working but I don't really understand it . It seems like I should have a global variable which is binding my this.closeModal
but how can I have a global variable in my class ?