It's common to want the user to confirm his action if it would be hard to reverse, e.g. if he asked to delete something. The confirmation can be easily obtained via a javascript confirm dialog box. If you'd rather not use javascript, another approach is outlined below.
Using Javascript
Wicket 6.x
This is a solution from Sven Meier - 'overwrite #updateAjaxAttributes in your behavior or AjaxLink'. To make a reusable Component, simply create an abstract base class:
Code Block |
---|
public abstract class ConfirmationLink<T> extends AjaxLink<T>
{
private static final long serialVersionUID = 1L;
private final String text;
public ConfirmationLink( String id, String text )
{
super( id );
this.text = text;
}
@Override
protected void updateAjaxAttributes( AjaxRequestAttributes attributes )
{
super.updateAjaxAttributes( attributes );
AjaxCallListener ajaxCallListener = new AjaxCallListener();
ajaxCallListener.onPrecondition( "return confirm('" + text + "');" );
attributes.getAjaxCallListeners().add( ajaxCallListener );
}
}
|
Earlier Wicket versions
Here's one way to do get confirmation, using Javascript:
...
Code Block |
---|
public class JavascriptEventConfirmation extends AttributeModifier { public JavascriptEventConfirmation(String event, String msg) { super(event, true, new Model(msg)); } protected String newValue(final String currentValue, final String replacementValue) { String resultprefix = "returnvar conf = confirm('" + replacementValue + "'); " + "if (!conf) return false; "; String result = prefix; if (currentValue != null) { result = currentValueprefix + "; " + resultcurrentValue; } return result; } } |
Then we attach the modifier to the link:
...
Code Block |
---|
abstract public class ConfirmLink extends Link {
public ConfirmLink(String id, String msg) {
super(id);
add(new JavascriptEventConfirmation("onclick", "are you sure?"));
}
@Override
abstract public void onClick();
}
|
(The subclass could of course use SimpleAttributeModifier
instead.)
...