How to invoke function from string by XPages SSJS

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 "". Then switch the flow by the query parameter value of "func".

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

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"

(2) Use eval()

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 ",bb". (I added [args=aa,bb] as the function argument's values.) , the SSJS code becomes like below:

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:

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 -> ",bb"

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

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.

[CakePHP] JSON出力時にstring型をint型に戻す

久々のブログ、今回はCakePHPでJSON出力をするときにInteger型、String型に型変換して出力する方法について書きます。 (あ、今回も他力本願、満載)



$yourJsonObect= $this->YourModel->find('all');
$this->set('yourJsonObect', $yourJsonObect);



<?php echo $javascript->object($yourJsonObect); ?>



[{"YourModel":{"id":"10060","foreign_id":"1","short_description":"Short Desc Test.","long_description":"Long Desc Test"},

これで、殆ど場合は問題ないのですが、データベース上でIntegerで持っているスキーマ(id, foreign_idなど)も全てString型として出力されます。


余談ですが、自分の場合は、iPhone開発において、JSONデータをウェブサーバーから受け取り、Core DataにInsertする時に、iPhone側で型チェックや型変換をさせたくなかったので、サーバー側で吸収してしまおうという経緯がありました。





それに例えばよくある、intの0,1をフラグと扱って if (flag) {.... }なんて書いたらマズいんじゃないかと思うので、settypeで型変換を行う方法。

引用元: CakePHPのmodelの結果配列でstring型をint型に戻す。: ろーにーの日記.



class AppModel extends Model{
	public function afterFind($results, $primary = false) {
		parent::afterFind($results, $primary);
		$columnTypes = $this->getColumnTypes();
		foreach($results as $index => $result) {
			foreach($columnTypes as $field => $columnType) {
				if($columnType == 'integer') {
					if(isset($results[$index][$this->name][$field])) {
						settype($results[$index][$this->name][$field], 'integer');
		return $results;

これで全てのモデルが自動変換されるようになりました。 感激~


[{"YourModel":{"id":10060,"foreign_id":1,"short_description":"Short Desc Test.","long_description":"Long Desc Test"},