简体   繁体   中英

Reload JavaScript function via ajax / re-read jsf-bean value

I have a JavaScript function using a value from a backing bean, like var x = #{bean.value} .

On page load, the value for x is final until the page is being reloaded (since EL is being replaced by its actual value string).

However, the bean's value might change due to ajax requests. How can I achieve that x gets updated?

Here's an excerpt from my xml (using primefaces' calendar component) to clarify:

<p:calendar beforeShowDay="highlightDays" ... />

<script>
   function highlightDays(){
      var highlightedDays= #{bean.specialDays()};
      // set css...
   }
</script>

You can use the RequestContext to do such an update from the bean:

RequestContext.getCurrentInstance().execute("highlightedDays = " + specialDays());

And the javascript variable should be on the window scope, meaning that you should define the var outside the scope of the function.

highlightedDays= #{bean.specialDays()};      
function highlightDays(){
  //make use of the var  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM