I want to do the following:
- Select an item from an
h:selectOneMenu
- Update the backing bean with the new value, via ajax
- Run a Javascript function with the new value
In the following code though, alert(#{backingBean.derivedValue})
still contains the value from the last change (i.e. it's 0 when I select "Two", 4 when I select "One", and so on):
<h:form>
<h:selectOneMenu value="#{backingBean.input1}">
<f:selectItem itemLabel="One" itemValue="1"/>
<f:selectItem itemLabel="Two" itemValue="2"/>
<f:ajax render="@form" onevent="function(data) { if (data.status === 'success') { alert(#{backingBean.derivedValue}) }}"/>
</h:selectOneMenu>
Input value: #{backingBean.input1}
Derived value: #{backingBean.derivedValue}
</h:form>
And the backing-bean:
@ManagedBean
@ViewScoped
public class BackingBean {
private int input1;
private int derivedValue;
public int getDerivedValue() {
return derivedValue;
}
public void setDerivedValue(int derivedValue) {
this.derivedValue = derivedValue;
}
public int getInput1() {
return input1;
}
public void setInput1(int input1) {
this.input1 = input1;
derivedValue = input1 * 2;
}
}
Is there a way to do this? (BTW, I've read countless threads on this site which kind of/sort of deal with JSF/ajax/javascript working together, but not this specific issue)