Ajax for valueChangeListener

Danubian Sailor picture Danubian Sailor · Feb 7, 2013 · Viewed 22.1k times · Source

I'm using the p:ajax listener to handle value change events (because valueChangeListener is launched on form submit):

<p:ajax event="change" listener="#{bean.onNameChanged}"/>

Handle method:

public void onNameChanged(final AjaxBehaviorEvent event)

The problem is, I can't find in AjaxBehaviorEvent nor its class hierarchy the place to read the old value of the input. Neither could I find hint in google, how to get the old value...

How to access the old value in the p:ajax onChange event?

Answer

BalusC picture BalusC · Feb 8, 2013

The problem is, I can't find in AjaxBehaviorEvent nor its class hierarchy the place to read the old value of the input. Neither could I find hint in google, how to get the old value...

Use a valueChangeListener.


Unfortunatelly, valueChangeListener is invoked before p:ajax, so I don't have actual data from forms in that method, so in theory I could use valueChangeListener to remember the old value and then wait for p:ajax to process...

Queue the value change event to the invoke application phase.

public void valueChangeListenerMethod(ValueChangeEvent event) {
    if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
        event.setPhaseId(PhaseId.INVOKE_APPLICATION);
        event.queue();
        return;
    }

    // Do your original job here. 
    // It will only be invoked when current phase ID is INVOKE_APPLICATION.
}