How can I refresh a javascript variable, populated by JSF, via ajax? -
i want following:
- select item
h:selectonemenu
- update backing bean new value, via ajax
- run javascript function new value
in following code though, alert(#{backingbean.derivedvalue})
still contains value last change (i.e. it's 0 when select "two", 4 when select "one", , 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 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 way this? (btw, i've read countless threads on site kind of/sort of deal jsf/ajax/javascript working together, not specific issue)
i had success implementing solution proposed in answer, getting backing bean value javascript uses primeface's requestcontext add javascript callback parameter server-side.
here facelets page:
<h:form> <p:selectonemenu value="#{backingbean.input1}"> <f:selectitem itemlabel="one" itemvalue="1"/> <f:selectitem itemlabel="two" itemvalue="2"/> <p:ajax oncomplete="afterload(xhr, status, args)"/> </p:selectonemenu> <h:outputscript> function afterload(xhr, status, args) { alert("input * 2 = " + args.derived); } </h:outputscript> </h:form>
and here backing-bean:
import javax.faces.bean.managedbean; import javax.faces.bean.viewscoped; import org.primefaces.context.requestcontext; @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; requestcontext.getcurrentinstance().addcallbackparam("derived", derivedvalue); } }
(i don't know if solution integrating jsf , d3.)