Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  95] [ 6]  / answers: 1 / hits: 49036  / 11 Years ago, tue, february 19, 2013, 12:00:00

JSF 2.0, Mojarra 2.0.1, PrimeFaces 3.4.1



There are similar questions but I need sth. else; javascript function has to wait for the backing bean method, which is filling the variable that wanted to be pulled from js function. What I want to say is:



<p:commandLink action=#{statusBean.getStatuses} oncomplete=afterLoad()/>


Assuming js function just getting the value and printing it to the screen.



function afterLoad() {    
alert(#{statusBean.size});
}


And here is the birthday kid:



@ManagedBean
@ViewScoped
public class StatusBean {
public int size=0;
List<Status> panelList = new ArrayList<Status>();
public void getStatuses() {
this.panelList = fillList();
this.size = panelList.size(); //Assuming 3
}
//getter and setters
}


So function alerts the size as 0 which is the initial value of it, while we're expecting to see 3.



How it's working: If I add the @PostConstruct annotation to bean's head surely it gets the correct size, because bean is already constructed before the page load. But this means redundant processes, value just needed after the commandlink action. So how to postpone the js function? Any ideas?


More From » jsf

 Answers
11

JSF/EL and HTML/JS doesn't run in sync. Instead, JSF/EL run in webserver and produces HTML/JS which in turn runs in webbrowser. Open page in browser, rightclick and View Source. You see, there's no single line of JSF/EL. It's one and all HTML/JS. In place of your JS function, you'll see:



function afterLoad() {    
alert(0);
}


Exactly this JS function get invoked on complete of your command button action. So the result is fully expected.



Basically, you want to let JSF re-render that piece of JS.



<p:commandLink action=#{statusBean.getStatuses} update=afterLoad oncomplete=afterLoad()/>
<h:panelGroup id=afterLoad>
<h:outputScript>
function afterLoad() {
alert(#{statusBean.size});
}
</h:outputScript>
</h:panelGroup>


Depending on the concrete functional requirement, which you didn't tell anything about, there may be more elegant ways. For example, RequestContext#execute(), <o:onloadScript>, etc.


[#80123] Monday, February 18, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rhett

Total Points: 671
Total Questions: 100
Total Answers: 102

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
;