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 "http://mydomain.com/ApplyFuncXAgent.xsp?func=callMe". 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 "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:

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 -> "http://mydomain.com/hoge.nsf/ApplyFuncXAgent.xsp?func=testFunc&args=aa,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型に型変換して出力する方法について書きます。 (あ、今回も他力本願、満載)

まず、CakePHPでJSON出力をする為には

Controllerで

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

としてやり

Viewで

<?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型として出力されます。

もし、JSONデータを受け取る側が厳密にInteger型、String型を区別して受け取りたい場合などに以下の方法でスマートに解決できたので紹介します。

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

参考にしたサイトはこちらです。

CakePHPのmodelの結果配列でstring型をint型に戻す。

CakePHPでmodel::findなどの関数で結果配列を取得したときに、データベースではint型の値string型として返って来ます。

今回は、flashにjson出力でデータを渡したかったので、別に文字列型のまま渡しても問題ないのかもしれないけど、なんか気持ち悪い。

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

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

モデルの親クラスで、Findで取得したデータ(デフォルトは全てString)をデータベースのTypeを参照して、型変換を行っています。

app_model.phpにafterFind()オーバーライド関数を追加してやるだけ。

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;
	}
}

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

変換後のJSON出力を見ると以下のようになります。

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

Integer型の数字の部分からダブルクォーテーションが消えました。

めでたしめでたし。

http://app.cocolog-nifty.com/t/trackback/120702/48427427