FORUM PLAN UPDATE
Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.



Apr 9, 2013, 2:57 AM
67 Posts
topic has been resolvedResolved

Server side value in CSJS

  • Category: Dojo and Client Side JavaScript
  • Platform: All
  • Release: 8.5.3
  • Role: Developer
  • Tags:
  • Replies: 9
 Hi,
 
i've embedded a CS script (that works):

 <xp:script clientSide="true">
function({ 
ean: '4021972002188'
    }); 
</xp:script>


Now, the param 'ean' should be a serverside viewScope value.I've tried this  (that does'nt work): 
 
 <xp:script clientSide="true">
function({ 
ean: <![CDATA[#{viewScope.get('EAN')};}]]>
     }); 
</xp:script>
 
What is the best way to do this ??

Thanks 
Uwe 
Apr 9, 2013, 4:43 AM
53 Posts
Re: Server side value in CSJS
You can't get scoped variables in csjs at all. There's a question on stack overflow about it here http://stackoverflow.com/questions/9568519/getting-to-scope-varibles-in-client-side-javascript-csjs-on-xpages

 
It suggests using field values to store the viewscope values, including using hidden fields (which I've done successfully) 
Apr 9, 2013, 5:31 AM
54 Posts
Re: Server side value in CSJS
Try this in CSJS:

console.log("#{javascript:viewScope.EAN}") 
Apr 9, 2013, 6:34 AM
67 Posts
Re: Server side value in CSJS
 That doesn't work.
 
All Expressions like
#{javascript:viewScope.EAN}

are failing in the clientside script block.
The evaluator of the editor doesn't allow to save this, because there is a 'content  with runtime binding'.
 
Are there any other proposals ?
Idea: i write the whole script block serverside as pure HTML Code in a text field. Then i can embed the value. 
 
@Dan:
i don't know to integrate XSP.getElementById("#{id:inputHidden1}").value into my csjs ???
Nevertheless I think that would be the same problem as described above.
 
 
Apr 9, 2013, 7:47 AM
54 Posts
Re: Server side value in CSJS
Why did you use a xp:script tag and not a xp:scriptblock tag??


This wirks for me:

<xp:scriptBlock id="scriptBlock1">
        <xp:this.value>

 <![CDATA[var dd =function(){
ean = "#{javascript:viewScope.ean}";
console.log(ean);
}]]></xp:this.value>
    </xp:scriptBlock>  
Apr 9, 2013, 9:58 AM
67 Posts
Re: Server side value in CSJS
Helmut,

i didn't know xp:scriptBlock, that works !

The block is now in the client sourcecode in the body section (in the header it wasn't possible).
But it doesn't run once at loading. How can i achive that ?

Thanks
Uwe 

 
Apr 9, 2013, 10:53 AM
54 Posts
Re: Server side value in CSJS
You have to call the function to get the value.

I didn´t know at what time you need it. 
Apr 9, 2013, 11:24 AM
366 Posts
Re: Server side value in CSJS
 If you are using 8.5.3, instead of the scriptblock you add your function to the XPage onClientLoad event for client side script.


It adds the function to the header and then the function can be called from any CSJS on the page
Apr 18, 2013, 8:51 AM
67 Posts
Re: Server side value in CSJS
Thank you all.
It is solved. I've choosed Helmuts way: 


 <xp:scriptBlock>
  <xp:this.value>
<![CDATA[#{javascript:"XSP.addOnLoad(function({" + viewScope.get('EAN') + "}));"
                         }]]>
</xp:this.value>
</xp:scriptBlock>

This loads clientside and has the serverside value in it. 

FORUM PLAN UPDATE
Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.