1 <?php 2 class Example { 3 4 function foo() { 5 return ‘bar’; 6 } 7 } 8 ?>
<html> <head> <title>JPSpan echo Example<⁄title> <!-- JpSpan のJavaScript クライアントを読み込む --> <script type='text⁄javascript' src='server.php?client'> <⁄script> <script type='text⁄javascript'> ⁄⁄ HTML から呼び出される関数 function echoString(string) { remoteExample.echo_string(string); } ⁄⁄ コールバッククラス function exampleCallback() {} exampleCallback.prototype.echo_string = function(result) { document.getElementById('target').innerHTML += result+ "<br>"; } ⁄⁄ リモートのプロキシクラス var remoteExample = new example(new exampleCallback()); <⁄script> <⁄head> <body> <input id="source" type="text"><button onclick="echoString(document.getElementById('source').value)">Echo<⁄button> <div id="target"> <⁄div> <⁄body> <⁄html>
1 ⁄⁄ 必要に応じて自動的に作成されるオブジェクトプール 2 function objectPool() { 3 this.pool = new Array(); 4 this.avail = new Array(); 5 } 6 7 ⁄⁄ このメソッドをオーバーライドして 8 ⁄⁄ JPSpan のプロキシインスタンスを作成する 9 objectPool.prototype.createObject = function() { 10 return new Object(); 11 } 12 13 ⁄⁄ プールから新しいオブジェクトを取り出す 14 objectPool.prototype.getObject = function() { 15 ⁄⁄ 必要に応じて、オブジェクトを作成する 16 if (this.avail.length == 0) { 17 var o = this.createObject(); 18 o.poolId = this.pool.length; 19 this.pool.push(o); 20 this.avail.push(o.poolId); 21 } 22 23 var poolId = this.avail.pop(); 24 return this.pool[poolId]; 25 } 26 27 ⁄⁄ オブジェクトをプールに返す 28 objectPool.prototype.returnObject = function(poolId) { 29 this.avail.push(poolId); 30 }
1 function echoString(string) { 2 remotePool.getObject().echo_string(string); 3 } 4 5 ⁄⁄ コールバックを呼び出す 6 function exampleCallback() {} 7 exampleCallback.prototype.echo_string = function(result) { 8 document.getElementById('target').innerHTML += 9 result+"<br>"; 10 remotePool.returnObject(this.parent.poolId); 11 } 12 13 ⁄⁄ JPSpan のプロキシクラスをプールする 14 var remotePool = new objectPool(); 15 remotePool.createObject = function() { 16 var callback = new exampleCallback(); 17 var remote = new example(callback); 18 callback.parent = remote; 19 return remote; 20 }