Showing posts with label ssjs. Show all posts
Showing posts with label ssjs. Show all posts

Thursday, December 20, 2012

Using indexOf method on array in server side JavaScript

Sometime back I was trying to use indexOf method on an Array object in server-side JavaScript (SSJS) code. But it was giving me error that indexOf method is not defined. I also couldn't find this method in help documentation. It was then it struck me that the implementation indexOf method in client side JavaScript itself is a bit fuzzy with Internet Explorer not supporting it. It can be defined using prototype property.

I found a simple solution here which implements the method. Just add the below code snippet to your SSJS code and then you would be able to use indexOf method.


Remember that this would be required for SSJS. For client side JavaScript I would recommend using dojo.indexOf.

Friday, October 26, 2012

Getting resources in database as stream in XPages (SSJS / Java)

Some time back I was trying to get the resources in database (to be precise image resource) in my SSJS / Java code. I tried a lot of options like ClassLoader.getResourceAsStream, MyClass.class.getResourceAsStream but no success. Then today while going through the sample database of Extension Library I ran into a piece of code which got the image resource as stream using a single line of code.


As simple as that. In case of Java you would write like this:


In my tests this worked for getting image resources, file resource and style sheets as streams.

Thursday, September 6, 2012

Expand / Collapse particular category in a view in XPages

In XPages, you can expand all / collapse all categories in a view either using the expandLevel property of view control or using view control API. The API technique can further be extended to expand / collapse a particular category in a view. So to expand the code goes like this:

Friday, July 27, 2012

Creating optgroup tag in combo boxes in XPage

Tommy Valand in his blog post pointed to a StackOverflow question to add <optgroup> in combo box in JSF. Using hints from there I was able to devise a way to get <optgroup> in combo box for XPages. Below is the code snippet for the combo box.

Saturday, February 11, 2012

Calling inner classes and enum from SSJS

While working on RSS Reader custom control for OpenNTF I had got stuck when trying to use enums. Basically I wanted to call the enum Proxy.Type to create instance of Proxy class. In Java you would write the code some thing like this:


But to write the same code in SSJS you need to concatenate the class and enum with $ (dollar) symbol. So your SSJS code would be:


The same goes when you are dealing with nested / inner classes. But why use the dollar symbol? I am not sure of the exact reason, but when a Java class with inner class or enum is compiled it generates two class files - one for outer and one for nested. For e.g.


The above class would generate class files as OuterClass.class and OuterClass$InnerClass.class.

Tuesday, December 13, 2011

Get Java class names for variables defined in SSJS in XPages


Every global object or variable in SSJS is an object of a class defined in Java. The help documentation says that facesContext is an object of class com.ibm.xsp.domino.context.DominoFacesContext. But what about facesContext.getExternalContext() and other variables?

Well in Java every class inherits directly or indirectly from its base class java.lang.Object. This class has a method named getClass() which returns object of class java.lang.Class (Yes, there is a class called "Class" in Java!). With this you can find out to which class the object belongs, its methods, super class and lot more.

Create a new XPage and add a computed field. Add the code given below for its value and preview it in browser.