Hey all!
Inside the connect()
, I need to use window.addEventListener('click', functionName)
. The issue is, inside the functionName
, I no longer have access to the class’s this
:
static targets = [ "foo" ]
connect {
window.addEventListener('click', this.toggle)
}
disconnect() {
// I'll need to disconnect
// window.removeEventListener('click', this.toggle)
}
toggle(event) {
// this is undefined -> this.fooTarget
}
Yes, it will be undefined as it’s now the "windows’ scope, within the listener function, but how to achieve what I want?
EDIT:
Never mind. I need to use bind
[..]
connect {
window.addEventListener('click', this.toggle.bind(this))
}
[..]