简体   繁体   English

通过ajax重新加载JavaScript函数/重新读取jsf-bean值

[英]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} . 我有一个使用后备bean中的值的JavaScript函数,例如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). 在页面加载时, x的值是最终的,直到重新加载页面为止(因为EL被其实际值字符串替换了)。

However, the bean's value might change due to ajax requests. 但是,由于ajax请求,bean的value可能会更改。 How can I achieve that x gets updated? 如何实现x更新?

Here's an excerpt from my xml (using primefaces' calendar component) to clarify: 这是我的xml的摘录(使用primefaces的日历组件)来阐明:

<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从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. 并且javascript变量应在窗口范围内,这意味着您应在函数范围之外定义var。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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