How to invoke function from string by XPages SSJS
Monday March 14th, 2016JSON,Lotus Domino Designer,XPages アプリケーション開発,XPagesNotes/Domino 日本語,XPages,JSON,Notes/Domino,XPages アプリケーション開発
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".
[code lang="javascript" highlight=""]
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;
}
[/code]
If you want to simplify the code above, you can write the code like below:
[code lang="javascript" highlight="2"]
var strFunc = location.search.split('func=')[1];
var funcObj = window[strFunc];
if (typeof funcObj === "function") funcObj();
[/code]
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"
[code lang="javascript" highlight=""]
var funcObj = this[strFunc];
[/code]
(2) Use eval()
[code lang="javascript" highlight=""]
eval(strFunc+"()");
[/code]
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:
[code lang="javascript" highlight="3"]
var argsAry = @Explode(args, ",");
var funcObj = this[func];
return funcObj.call(this, argsAry[0], argsAry[1]);
[/code]
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:
[code lang="javascript" highlight="3"]
var argsAry = @Explode(args, ",");
var funcObj = this[func];
return funcObj.apply(this, argsAry);
[/code]
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"
[code lang="xml" highlight="15,16,18"]
<?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>
[/code]
Below is the sample code of xpCommon.jss which contains the callFuncByString() above.
[code lang="javascript" highlight=""]
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+"\"}]";
}
[/code]
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.
(日本語) IBM Connect 2016 XPages最新動向
Wednesday February 3rd, 2016Lotus Domino Designer,Notes/Domino,XPages,IBM ChampionIBM Connect 2016,Notes/Domino 日本語,XPages
Sorry, this entry is only available in Japanese.
(日本語) IBM Connect会場内の雰囲気
Wednesday February 3rd, 2016IBM Connect 2016
Sorry, this entry is only available in Japanese.
(日本語) Business Partner Summit 基調講演 - 2016/01/31
Tuesday February 2nd, 2016IBM Connect 2016
Sorry, this entry is only available in Japanese.
(日本語) IBM Connect 2016 - Day1 基調講演
Tuesday February 2nd, 2016IBM Verse,IBM Connect 2016,Notes/Domino 日本語
Sorry, this entry is only available in Japanese.
(日本語) ケートリック 関東オフィスが移転致しました。
Tuesday February 2nd, 2016未分類,ニュース
Sorry, this entry is only available in Japanese.
(日本語) IBM Connect 2016の濃い一週間が始まります。
Sunday January 31st, 2016IBM Connect 2016,ibm connectNotes/Domino 日本語
Sorry, this entry is only available in Japanese.
(日本語) IBM Champion 2016に任命されました
Friday November 27th, 2015ニュースNotes/Domino,xpagesday,XPages,IBM Champion
Sorry, this entry is only available in Japanese.


