XSJS Libraries

Introduction:

In the article Introduction to SAP HANA XS, we explained that SAP HANA XS can be used to expose data from Tables, Views and Modeling Views to UI layer.
We also explained the how to create XSJS and examples in the articles SAP HANA XSJS Service and SAP HANA XSJS Examples.

In this example we will learn XSJS libraries (xsjslib).

HANA XSJS Libraries:

Server-side JavaScript libraries (xsjslib) are a special type of JavaScript program that can be imported and called in other JavaScript programs.
We can create our own libraries by creating a XSJS library file called xsjslib.

Example:

In this example we will create a method in library file (.xsjslib) to add greeting prefix and call this method from another XSJS file.

Prerequisites:


Steps to create complete application:

1. Create a XS project as mentioned in the article Create Your First HANA XS Application using HANA Studio
2. Right click and select New à Others à XS JavaScript Library File. Specify file name as “GreetLib.xsjslib”.
3. Copy paste below code.

var greetingPrefix = "Hello, ";
var greetingSuffix = "!";
function greet (name) {
        return greetingPrefix + name + greetingSuffix;
}

4. Create an XSJS file and copy paste below code. Change the package name as your package name.

// import the library
$.import("xsjslibtutorial", "greetLib");
var GREETLIB = $.xsjslibtutorial.greetLib;

var name = $.request.parameters.get("name");
var output = GREETLIB.greet(name);

$.response.setBody(output);
$.response.status = $.net.http.OK;

5. Run XSJS service and pass the parameter as

http://<XS_Webserver>:80<SAPHANA_InstanceNr>/testApp/GetStockValue.xsjs?name=Thomas


Download Full Source Code:

Click here to download the full source code.

Continue reading:

1 comment: