Skip to Content
Author's profile photo Former Member

DB001: Using libraries in XS

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.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Thanks for this blog. Not much information on this topic out there!

      Author's profile photo Former Member
      Former Member

      thanks for the blog.....:)

      Author's profile photo Former Member
      Former Member

      Hi David ,

      May I know where the xsjs services run time object will stored in catalogue..after activation?



      Author's profile photo Scar Ai
      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?