Skip to Content

Fundamental to any serious development is the ability to have code which is organized and easy to maintain.  Therefore I am choosing the topic of using libraries in XS as the first post for my XS/SAPUI5 blog series.

The actual mechanism for creating and using a library within XS is quite straigt-forward.  First, the libary function is placed in an .xsjslib file.  For this example, I am calling the library SimpleLib.xsjslib.

// David Brookler's XS/SAPUI5 Blog
// 001 -- Using libraries in XS
// SimpleLib.xsjslib
function simpleLibFunction(s) {
  return "This came from simpleLibFunction() -- " + s;

Next we need to import the library.  The $.import command has two parameters.  The first parameter is a string representing the path to the library with the directory separator being a period.  The second parameter is the name of the library without the .xsjslib. See line 8 below.

// David Brookler's XS/SAPUI5 Blog
// 001 -- Using libraries in XS
// simpleLibCall.xsjs
// import the library
$.import("blog.b001", "SimpleLib");
// create a variable for simpler access to the library
var SimpleLib = $.blog.b001.SimpleLib;
// create a response object
var oResponse = {};
oResponse.directCall = $.blog.b001.SimpleLib.simpleLibFunction('direct call');
oResponse.callThroughLibVariable = SimpleLib.simpleLibFunction('call through variable');
// send the response object as a JSON string
$.response.status = $.net.http.OK;

Once the library has been imported, a call to a library function simply involves prefacing the call with the fullpath to the library, including the library’s name.  The root is represented by a $.  See line 15 above,

It is also possible to create a variable to reference the library.  See line 11 above.  This variable can then be used to simplify access to the library.  See line 16 above.

The result of calling the XS web service will be:

     directCall: "This came from simpleLibFunction() -- direct call",
     callThroughLibVariable: "This came from simpleLibFunction() -- call through variable"

In the next post, I plan to look at how to create libraries which control what is exposed as its interface.

To report this post you need to login first.


You must be Logged on to comment or reply to a post.

  1. Scar Ai

    How to import a file who is not in current test’s sub folder?

    I have sample codes as below and try to import calculator in sampleTest.



    I’ve tried below things and none of them works for me


    $.import(“todo_list.todo_nodejs.calc1”, “calculator”);

    $.import(“todo_nodejs.calc1”, “calculator”);

    $.import(“../calc1”, “calculator”);

    var clib = $.import(“../calc1/calculator.xsjslib”);


    Any idea?


Leave a Reply