Commits

Toby Davies  committed ad57d81

copyright

  • Participants
  • Parent commits 7b7b2b8

Comments (0)

Files changed (2)

+/* 
+
+Copyright (c) 2011, Toby Davies
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+      
+* Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above copyright
+  notice, this list of conditions and the following disclaimer in the
+  documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+   
+*/
+
+/*
+ * JSON AJAX RPC framework, client-side code.
+ * Makes remote procedure calls to PHP (see rpc.php)
+ * PHP functions can effectively be called from javascript as methods on an
+ * RpcService object initialized as RpcService(url). 
+ *
+ * Usage:
+ * RPC=RpcService(someURL);
+ * var x=RPC.someSynchronousPHPCall(arg1,arg2,arg3); //returns something after waiting for RPC to succeed, or throws an RPCError on failure
+ * RPC.someAsynchronousPHPCall(arg1,arg2,arg3,callbackIfSuccessful,callBackOnError); //returns immediately, and uses callbacks on success/error
+ * 
+ * Request Format:
+ * POST application/x-www-form-urlencoded variables:
+ *  'f' - the name of the function to be called
+ *  'a' - a JSON encoded array of arguments
+ *
+ * Response Format:
+ * {error:"Error Message As JSON Encoded String"}
+ * or
+ * {success:{some:"JSON", encoded:["object"]}
+ *
+ */
+
+//wrap in anon function call to aviod polluting global name space
+(function(){
+    //create RPCError class for errors thrown in PHP
+    window.RPCError=function(msg){
+	if(this instanceof RPCError){
+	    this.msg=msg;
+	}else{
+	    throw new RPCError(msg);
+	}
+    }	
+    //low-level XmlHttpRequest function which may or may not be asynchronous
+    function xhr(url,params,callback,async){
+	var c=new  XMLHttpRequest();
+	async=(typeof async == "undefined")?(callback||false):async;
+	c.open(params?"POST":"GET",url,async);
+	if(params) c.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+	c.onreadystatechange=callback || null;
+	c.send(params);
+	return async || (c.status==200 && c.responseText);
+    }
+    //parse text as json, calling s if successful, e on RPCError
+    function parse(text,s,e){
+	var scb=s || function(v){return v;};
+	var ecb=e || function(e){RPCError(e);};
+	var x=JSON.parse(text);
+	if(typeof(x.error) != 'undefined')
+	    return ecb(x.error);
+	return scb(x.success);
+    }
+    //make an RPC call to url calling func with argList.
+    // the last one or two args may be functions, 
+    //in which case they are interpreted as callbacks 
+    //(for success and optionaly failure)
+    function do_rpc(url,func,argList){
+	var args=[].slice.call(argList);
+	var scb=((typeof args[args.length-1])=='function')?args.pop():false;
+	var ecb=false;
+	if((typeof args[args.length-1])=='function'){
+	    ecb=scb;
+	    scb=args.pop();
+	}
+
+	var params="f="+escape(func)+"&a="+escape(args.toJSON?args.toJSON():JSON.stringify(args));
+
+	var cb=scb && (function(){
+		if(this.readyState==4) parse(this.responseText,scb,ecb);
+	    });
+
+	var result = xhr(url,params,cb);
+	return (scb && result!==false)?true:parse(result,scb,ecb);
+    }
+    //return a function f which makes an RPC call to url/func s.t.
+    // f('x') calls func('x') on the server at url.
+    function rpc(url,func){
+	return function(){return do_rpc(url,func,arguments);};
+    }
+   
+    //constructor for RpcService class
+    //call the RPC server with GET - it returns a JSON array of the method names to add to the service object
+    //(using normal json RPC format i.e. {error:"error msg"} or {success:["JSON","Object"]})
+    window.RpcService=function(url){
+	var data=parse(xhr(url));
+	for(i=0;i<data.length;i++){
+	    this[data[i]]=rpc(url,data[i]);
+	}
+    };
+})();

File rpc.js

-/*
- * JSON RPC framework, client-side code.
- * Makes remote procedure calls to PHP (see rpc.php)
- * PHP functions can effectively be called from javascript as methods on an
- * RpcService object initialized as RpcService(url). 
- *
- * Usage:
- * RPC=RpcService(someURL);
- * var x=RPC.someSynchronousPHPCall(arg1,arg2,arg3); //returns something after waiting for RPC to succeed, or throws an RPCError on failure
- * RPC.someAsynchronousPHPCall(arg1,arg2,arg3,callbackIfSuccessful,callBackOnError); //returns immediately, and uses callbacks on success/error
- * 
- * Request Format:
- * POST application/x-www-form-urlencoded variables:
- *  'f' - the name of the function to be called
- *  'a' - a JSON encoded array of arguments
- *
- * Response Format:
- * {error:"Error Message As JSON Encoded String"}
- * or
- * {success:{some:"JSON", encoded:["object"]}
- *
- */
-
-//wrap in anon function call to aviod polluting global name space
-(function(){
-    //create RPCError class for errors thrown in PHP
-    window.RPCError=function(msg){
-	if(this instanceof RPCError){
-	    this.msg=msg;
-	}else{
-	    throw new RPCError(msg);
-	}
-    }	
-    //low-level XmlHttpRequest function which may or may not be asynchronous
-    function xhr(url,params,callback,async){
-	var c=new  XMLHttpRequest();
-	async=(typeof async == "undefined")?(callback||false):async;
-	c.open(params?"POST":"GET",url,async);
-	if(params) c.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-	c.onreadystatechange=callback || null;
-	c.send(params);
-	return async || (c.status==200 && c.responseText);
-    }
-    //parse text as json, calling s if successful, e on RPCError
-    function parse(text,s,e){
-	var scb=s || function(v){return v;};
-	var ecb=e || function(e){RPCError(e);};
-	var x=JSON.parse(text);
-	if(typeof(x.error) != 'undefined')
-	    return ecb(x.error);
-	return scb(x.success);
-    }
-    //make an RPC call to url calling func with argList.
-    // the last one or two args may be functions, 
-    //in which case they are interpreted as callbacks 
-    //(for success and optionaly failure)
-    function do_rpc(url,func,argList){
-	var args=[].slice.call(argList);
-	var scb=((typeof args[args.length-1])=='function')?args.pop():false;
-	var ecb=false;
-	if((typeof args[args.length-1])=='function'){
-	    ecb=scb;
-	    scb=args.pop();
-	}
-
-	var params="f="+escape(func)+"&a="+escape(args.toJSON?args.toJSON():JSON.stringify(args));
-
-	var cb=scb && (function(){
-		if(this.readyState==4) parse(this.responseText,scb,ecb);
-	    });
-
-	var result = xhr(url,params,cb);
-	return (scb && result!==false)?true:parse(result,scb,ecb);
-    }
-    //return a function f which makes an RPC call to url/func s.t.
-    // f('x') calls func('x') on the server at url.
-    function rpc(url,func){
-	return function(){return do_rpc(url,func,arguments);};
-    }
-   
-    //constructor for RpcService class
-    //call the RPC server with GET - it returns a JSON array of the method names to add to the service object
-    //(using normal json RPC format i.e. {error:"error msg"} or {success:["JSON","Object"]})
-    window.RpcService=function(url){
-	var data=parse(xhr(url));
-	for(i=0;i<data.length;i++){
-	    this[data[i]]=rpc(url,data[i]);
-	}
-    };
-})();