Friday, June 1, 2012

Wicket: Running Javascript On Client-Side Before Submitting Data to Server

In Wicket, we usually depend on the server-side logic to perform AJAX updates on the page. However, that's not always practical. It could be because of performance reasons, or in the case of file uploads, a limitation of the Wicket framework. For the latter, it's because no AJAX updates via Wicket is possible until the file upload is completed. If for example I wish to hide the upload button immediately on submission, that can't be done via Wicket's AJAX.

To achieve this, we can use an AttributeModifier to prepend Javascript to e.g. the "onchange" attribute of the file upload input element.
fileUploadField.add(new AjaxFormSubmitBehavior(this,"onchange") {
// ----- implementation snipped ----------
});
fileUploadField.add(AttributeModifier.prepend("onchange""this.style.visibility='hidden';"));

No comments:

Post a Comment