I am trying to make a function wrapper, to disable my button clicks. I would like this function to be general. This is my disable function:
disable = (event, fn) => {
let disabled = false;
return (...args) => {
if (!disabled) {
disabled = true;
const target = event.target || event.srcElement || event.currentTarget;
target.disabled = disabled;
fn(...args);
}
};
}
I can call the disable from my button like this:
<button (click)="disable($event, login)('hello')">Sign in</button>
The problem is that when fn() is called, this is not anymore in the scope. I know I am trying to blend some different fn and oop here. Can I somehow pass this or make it part of the scope?
I hope it's possible, for I would love to use this pattern!
Can I accomplish it without adding this as a parameter?
disable = (event, fn) => {
let disabled = false;
return (...args) => {
if (!disabled) {
disabled = true;
const target = event.target || event.srcElement || event.currentTarget;
target.disabled = disabled;
fn(this, ...args);
}
};
}
The above is a bad solution because the disable function isn't located in the component.