Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: use AttributeModifier instead of AbstractBehavior

...

SimpleAttributeModifier will replace any existing onclick handler in the html. We can instead add our javascript to any existing javascript by attaching an IBehavior object to the Link. First we define a class that implements IBehaviorcreating our own AttributeModifier instead of using SimpleAttributeModifier:

Code Block
public class JavascriptEventConfirmation extends AbstractBehaviorAttributeModifier {
	private final String msg;

	private final String event;

	public JavascriptEventConfirmation(String event, String msg) {
		this.msg = msg;
		this.event = eventsuper(event, true, new Model(msg));
	}

	@Override
	public void onComponentTag(Component component, ComponentTag tagprotected String newValue(final String currentValue,
			final String replacementValue) {
		String scriptresult = (String) tag.getAttributes().get(event)"return confirm('" + replacementValue + "')";
		scriptif (currentValue != "confirm('" + msgnull) {				
			result = currentValue + "'); " + scriptresult;
		}
		tag.put(event, script)return result;
	}
}

Then we add attach the behavior instead of an attribute modifiermodifier to the link:

Code Block
	removeLink.add(new JavascriptEventConfirmation("onclick", "are you sure?"));

...