Very similar to How to prevent jquery to override "this" but in ES6.
This is my class:
class FeedbackForm {
constructor(formEl) {
this.$form = $(formEl)
this.$form.submit(this.sendStuff)
this.alerts = $('#something');
}
/**
* Sends the feedback
* @param {Event} e
*/
sendStuff(e) {
e.preventDefault()
if (this.alerts.length) {
window.alert('Notice... stuff')
}
$.ajax({
type: this.$form.prop('method'),
url: this.$form.prop('action'),
data: this.$form.serialize()
}).done(() => window.location.reload(true))
}
}
The sendStuff
method is the event handler of the form, and jQuery calls it using Function.prototype.apply
I believe. Therefore, this
inside sendStuff
is overwritten with the event target that jQuery applies and I can't access this.alerts
or any other property methods.
I'm not sure if I can apply the var that = this
trick here or how do I work around this issue?