I have the following scenario:
JSF composite component with complex JavaScript, which I'd like to refresh partially by JavaScript with a new values from a backing bean (backing bean of a page which uses this composite component, not backing component @FacesComponent
).
I don't want to make full update because it's complex JavaScript plugin and it will unacceptably break UX.
I get values from backing component @FacesComponent
by using Primefaces's <p:remoteCommand>
with callback as described here Best method for passing Data from Java/JSF2 bean to Javascript/jQuery Components
I know that it's some abuse of JSF but would like to encapsulate all the functionality in a single unit and don't mess with a JAX-RS If you can advice another nice solution how to encapsulate such complex jQuery plugin (for sake of clarity we are talking about FullCalendar, I know that Primefaces has its own implementation of this component but its functionality insufficient for my requirement so, I was need to make my own implementation) which highly related on ajax callbacks with parameters you're highly welcome to share it here.
My question is how to update values in a backing component @FacesComponent
from backing bean by using JavaScript? Currently I involved in the following chain of events:
calling from Javascript
<p:remoteCommand>
with parameters which passed to backing component@FacesComponent
to be dispatched later inAjaxBehaviorEvent
JavaScript:
refreshEvents([ {name:'start', value:start.format()}, {name:'end', value:end.format()} ]);
JSF code:
<p:remoteCommand name="refreshValues" oncomplete="loadValues()" action="#{cc.refreshLocal()}" process="@this"/>
Parameters which I passed stored in a backing component by using
getStateHelper().put(...);
jQuery event dispatched from composite component by following JavaScript code:
var hiddenField = $(document.getElementById(variables.hiddenId)); hiddenField.trigger("keypress");
In composite component's overridden method
public void queueEvent(FacesEvent event)
I add to thisAjaxBehaviorEvent
property which I stored before, in a 1st step and dispatch it forward.Dispatched event from composite component "captured" in a page where composite component nested and performed
process
on this component:<p:ajax event="refreshEvent" process="@this" listener="#{bean.refreshEvents}"/>
in
#{bean.refreshEvent}
method I perform request to@EJB
bean and load data.On callback from step 1 called again by
loadValues()
<p:remoteCommand name="loadValues" action="#{cc.getLocalData()}" oncomplete="updateValues(xhr, status, args);"/>
In a backing component's method
#{cc.getLocalData()}
I add a callback parameter by using:RequestContext.getCurrentInstance().addCallbackParam("param", ...);
function
updateValues(xhr, status, args)
from step 5 get inargs
this param's values and performs actual update.
So, my general question is it possible to simplify this process and how?
Thank you.