What’s the case you want to invoke the function from string?

There are many situations you want to invoke the function from string of function name. One of case is for example, you might want to switch the functions from the parameter value of HTML Get/Post method.

The example below is client-side javascript(CSJS) when it’s triggered from the URL like “http://mydomain.com/ApplyFuncXAgent.xsp?func=callMe”. Then switch the flow by the query parameter value of “func”.

var strFunc = location.search.split('func=')[1];
switch(location.search.split('func=')[1]){
case "func1": func1(); break;
case "func2": func2(); break;
case "callMe": callMe(); break;
default: break;
}

If you want to simplify the code above, you can write the code like below:

var strFunc = location.search.split('func=')[1];
var funcObj = window[strFunc];
if (typeof funcObj === "function") funcObj();

In this case, window object is used to retrieve the function object, then call the target function. However Server-side Javascript(SSJS) does not have the window object like CSJS. Therefore you need to tweak this code a little.

(1) Use “this” instead of “window”


var funcObj = this[strFunc];

(2) Use eval()


eval(strFunc+"()");

Both (1),(2) can get the same result.

To pass the parameters, Use call() or apply()

Now we could call the function from string of function name. Then if you want to pass the parameters as well when call the funcion, you can use call() or apply() functions. Actually this way is pretty same as CSJS.

For example, when I call the HTTP Get request like “http://mydomain.com/ApplyFuncXAgent.xsp?func=callMe&args=aa,bb”. (I added [args=aa,bb] as the function argument’s values.) , the SSJS code becomes like below:

var argsAry = @Explode(args, ",");
var funcObj = this[func];
return funcObj.call(this, argsAry[0], argsAry[1]);

The code above is the example of using call(). If you want to use apply(), then instead of passing the arguments separated by comma, pass the Array to the second argument of apply(). Below is the sample code:

var argsAry = @Explode(args, ",");
var funcObj = this[func];
return funcObj.apply(this, argsAry);

Sample code of using XAgent

In my situation, I needed to get the JSON data from the many notes views as the asynchronous connection. (Passing the JSON data for Kendo UI Grid data.) So I decided to use the XAgent to return the JSON data, however I didn’t want to create XAgent XPages as many as notes views. So the sample code below calls the function from the Get parameter value of function name and returns the JSON data by the specified function.
HTTP request is like this -> “http://mydomain.com/hoge.nsf/ApplyFuncXAgent.xsp?func=testFunc&args=aa,bb”

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
	<xp:this.resources>
		<xp:script src="/xpCommon.jss" clientSide="false"></xp:script>
	</xp:this.resources>
	<xp:this.afterRenderResponse><![CDATA[#{javascript:var externalContext = facesContext.getExternalContext();
var writer = facesContext.getResponseWriter();
var response = externalContext.getResponse();

// Set context type etc
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-cache");

// read parameters
var func = context.getUrlParameter("func");
var args = context.getUrlParameter("args");

var retTxt = callFuncByString(func, args);
writer.write(retTxt);
writer.endDocument();}]]></xp:this.afterRenderResponse>
</xp:view>

Below is the sample code of xpCommon.jss which contains the callFuncByString() above.

function callFuncByString(func, args){
	try{
		var argsAry = @Explode(args, ",");
		var funcObj = this[func];
		return funcObj.call(this, argsAry[0], argsAry[1]);
	}catch(e){
		print(e);
	}
}

function testFunc(arg1, arg2){
	return "[{\"arg1\":\""+arg1+"\", \"arg2\":\""+arg2+"\"}]";
}

The advantage of this logic is avoiding to create many XAgent for notes views. But please remember that dynamically calling the functions from string value sometimes lose the serviceability of code. For example debugging purpose.
*This XAgent is just for example and for the real coding, please pay attention to the security when you use this concept since any SSJS standard functions can be called remotely if you don’t add any restriction and it is very dangerous.

About ktatsuki

ケートリック株式会社 代表をしています。 が、根っからのエンジニア脳です。 IBM Notes/Dominoの開発を得意としますが、 C++ / Java / PHP / Javascript などの言語を使ってWEBアプリ、iPhone / Android アプリ開発などをしたりします。 XPagesの仕事をしているとテンションが通常の1.25倍ぐらい高くなります。 I am owner of KTrick Co., Ltd. and Notes/Domino developer. IBM Champion for 2015 - 2017. I am interested in web application development and preferred languages are Notes/Domino, C++ / Java / PHP / Javascript.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *