Skip to content

Instantly share code, notes, and snippets.

@ananthakumaran
Created February 26, 2010 11:04
Show Gist options
  • Save ananthakumaran/315634 to your computer and use it in GitHub Desktop.
Save ananthakumaran/315634 to your computer and use it in GitHub Desktop.
MyApp.Ajax = {
// contains the list of handler to be invoked
// after ajax response
postAjaxHandlers : [],
// contains the ids of the changed elements
changedDomIds : [],
// registers the post ajax handles
registerPostAjax : function(fn) {
this.postAjaxHandlers.push(fn);
},
// this should be invoked by the the wicket
handle : function(changed) {
this.changedDomIds = changed;
},
// fires the post ajax event with collection of updated dom
firePostHandlers : function() {
var that = MyApp.Ajax;
// fire the hanldler only if there is updated dom ids
if (!that.changedDomIds.length == 0) {
var selector = '';
$.each(that.changedDomIds, function() {
selector += '#' + this + ',';
});
var elements = $(selector);
// invoke the handlers
$.each(that.postAjaxHandlers, function() {
this(elements);
});
// clear the ids
that.changedDomIds = [];
}
}
};
MyApp.Ajax.registerPostAjax(function(changed$){
// do you work here
changed$.find('mydiv.myclass');
});
// register the handlers
$(document).ready(function() {
if (Wicket.Ajax) {
Wicket.Ajax.registerPostCallHandler(MyApp.Ajax.firePostHandlers);
}
});
/**
* fires a event with the collection of all the updated dom elements after
* the wicket ajax response. To subscribe the event call the
* <code>MyApp.Ajax.registerPostAjax</code>. Your callback function
* will be called with a jQuery Wrapped set of all the update dom as the
* first argument.
*
* NOTE: call this only once after all the components are added to the
* target
*
*
* @param target
* ajax target
*/
public void firePostAjaxUpdateEvent(final AjaxRequestTarget target)
{
final StringBuffer script = new StringBuffer(" MyApp.Ajax.handle([");
for (final Component component : target.getComponents())
{
script.append("\"" + component.getMarkupId() + "\",");
}
script.append("])");
target.getHeaderResponse().renderOnDomReadyJavascript(script.toString());
}
/**
* sample usage
*/
@Override
protected void onSubmit(final AjaxRequestTarget target)
{
// add all the components
firePostAjaxUpdateEvent(target);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment