2
votes

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
You cant set ServerSideVariable that easy check out: stackoverflow.com/questions/15943985/… - Michael Saiz

1 Answers

5
votes

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.