Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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.setBody(JSON.stringify(oResponse));
$.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.

4 Comments
Labels in this area