Hi,
I set setDisableOnClick option on button and first expect that button will be enable after BE method execution. This is not happening. How can I enabled it manually? I would like to do double click prevention.
@Route("")
@PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {
public MainView() {
Button button = new Button("Click me",
event -> {
mySlowMethod();
event.getSource().setEnabled(true); // NOT WORKING
});
button.setDisableOnClick(true);
add(button);
}
public void mySlowMethod() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Having a debounce to prevent double clicks registering twice solves one problem. If you want to control the enabled status on the server based on some conditional logic, that’s another one (e.g. re-enable the “add to cart” button only if there are more items available in stock).
I have not investigated what debounce exactly means, but the problem being solved is the same: prevent double clicking. Only I attempt a discrete approach: disable immediately, until the server decides it can be enabled again. Either because the process has finished (or maybe it will not enable the button again, because it is not reentrant), some precondition failed, or else you will be redirected to another page.