lense
lense

Reputation: 55

Call SSJS from CSJS in XPages

I need to call a java bean method from a client side javascript library. Is there a way to call ssjs from csjs library?

something like this that works in csjs var test = #{javascript:getConfig.getKeyValuesList("param")};

Upvotes: 2

Views: 3593

Answers (3)

Newbs
Newbs

Reputation: 1632

I do this with a combination of CSJS libraries where I define objects with properties and methods and then on a custom control (usually the one with the resource for the library) I add a scriptBlock for getting the data into my client side objects. As frank says this only happens when the page is loaded but for configuration data like what you seem to be getting that works just fine.

Here is a sample csjs class for your library:

var appConfig = {
   param1 : "",
   param2 : ""
}

Then this is the scriptBlock code:

<xp:scriptBlock>
     <xp:this.value><![CDATA[
// setup config parameters
appConfig.param1 = '#{javascript:getConfig.getKeyValuesList("param1")}';
appConfig.param2 = '#{javascript:getConfig.getKeyValuesList("param2")}';
})
]]></xp:this.value>
</xp:scriptBlock>

Happy coding.

Upvotes: 3

Knut Herrmann
Knut Herrmann

Reputation: 30960

You can do it the way you showed in your example like

var test = #{javascript:yourBean.getSomething()};

The SSJS code gets executed first, the result gets inserted into CSJS code and send to client.

It depends on your use case if that can be a solution for you.

Your a bit modified example

var test = ['#{javascript:getConfig.getKeyValuesList("param").join("', '")}'];

would execute the methode getKeyValuesList(), return a List of strings, .join() would convert it to a string like "aaa', 'bbb', 'ccc" and send the following resulting code to client:

 var test = ['aaa', 'bbb', 'ccc'];

Upvotes: 3

David Leedy
David Leedy

Reputation: 3593

You want to use the Remote Service tool in the Ext. Library. That lets you define a function in SSJS and call it from CSJS.

There's discussion of it here: http://www.notesin9.com/2014/05/21/tim-explains-json-rpc-codefortim/

There's an old NotesIn9 Video should should still be very valid: http://www.notesin9.com/2011/08/25/notesin9-033-introduction-to-remote-services-in-xpages/

Upvotes: 7

Related Questions