XPages - Client side code to set viewScope value?

瘦欲@ 提交于 2019-12-11 03:42:34

问题


I have one button and on click of that button i have client side code to set viewScope value:

  var val = 'TEST_VALUE';
  "#{javascript: viewScope.testVal = " + val + "}" 

and on server side of that button I am trying to get viewScope value:

print("ViewScope val " + viewScope.testVal); 

Instead of variable value, variable name is getting stored in ViewScope.


回答1:


SSJS in a CSJS is computed on the server when the page is being rendered and the SSJS replaced with the result. So you're CSJS is not running SSJS. A browser cannot, by it's very nature of being a client, run server-side code. So you can include the result of SSJS in a piece of CSJS, but it will not have values updated since the last time that SSJS was recalculated on the server and passed to the browser.

You cannot update a viewScope variable via CSJS(Client Side JS), because the viewScope variable is only held on the server. It's a server-side Map, not a client-side browser cookie. So it can only be updated by server-side code or a post to run server-side code.

If you're wanting to update a viewScope variable with content from CSJS, you'll need to update a hidden input with the value, bind that hidden input to the viewScope variable and run a partial refresh to post the content of the hidden input to the server. If it's elsewhere on the page, just use SSJS to access the component / datasource and avoid the CSJS, but it's unclear what your use case is.



来源:https://stackoverflow.com/questions/16815183/xpages-client-side-code-to-set-viewscope-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!