Changeset 479
- Timestamp:
- 04/12/07 22:01:41 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/docroot/jsonrpclab/gen_jsonrpcstub_buddyQuoteService.js
r427 r479 1 function stubbles_json_rpc(clientObj, phpfile) { 1 // This needs to be moved into a different file... 2 var stubbles = {}; 3 stubbles.json = {}; 4 stubbles.json.rpc = {}; 2 5 6 7 stubbles.json.rpc.Client = function(clientObj, serviceURL) { 3 8 this.reqRespMapping = []; 9 this.serviceURL = serviceURL; 4 10 5 11 this.createId = function() { … … 8 14 return id; 9 15 } 10 16 11 17 this.callback = { 12 18 // closure needed … … 16 22 if ( rpcObj.error == null ) { 17 23 for (var i=0; i < this.reqRespMapping.length; i++) { 18 if (rpcObj.id = this.reqRespMapping[i].id) {24 if (rpcObj.id == this.reqRespMapping[i].id) { 19 25 var classAndMethod = this.reqRespMapping[i].method.split("."); 20 26 clientObj.eval('callback__' + classAndMethod[1] + '(rpcObj.id, rpcObj.result, rpcObj.error)'); 21 } else { 22 throw 'no related request id for response id found - mapping in js obj reqRespMapping failed!'; 27 return; 23 28 } 24 29 } 30 throw 'no related request id for response id found - mapping in js obj reqRespMapping failed!'; 25 31 } else { 26 32 throw rpcObj.error; 27 33 } 28 }, 34 }, 29 35 failure: function(o) { 30 36 throw 'callback error due to bad request from service (instead of HTTP status code 200)'; … … 34 40 this.doCall = function(classAndMethod, args) { 35 41 var id = this.createId(); 36 42 37 43 // because args is an object and 38 44 // not an array a conversion is needed … … 40 46 arr[i] = args[i]; 41 47 } 42 43 var postUrl = 'http://localhost/stubbles/docroot/jsonrpclab/' + phpfile+'?processor=jsonrpc';48 49 var postUrl = this.serviceURL +'?processor=jsonrpc'; 44 50 var jsonRpcReq = { 45 51 'method': classAndMethod, … … 48 54 }; 49 55 var transaction = YAHOO.util.Connect.asyncRequest('POST', postUrl, this.callback, jsonRpcReq.toJSONString()); 50 56 51 57 this.reqRespMapping.push(jsonRpcReq); 52 58 return id; … … 56 62 57 63 // ------------------------------ 58 59 function BuddyQuoteService(clientObj) {60 this.dispatcher = new stubbles_json_rpc(clientObj, 'jsonrpc.php');61 }62 63 BuddyQuoteService.prototype = {64 getQuote: function() {65 return this.dispatcher.doCall('BuddyQuoteService.getQuote', arguments);66 }67 }68 69 // ==============================70 71 bq = new BuddyQuoteService(this);72 ticketNr = bq.getQuote(2);73 74 this.callback__getQuote = function(id, result, error) {75 alert(result);76 };trunk/docroot/jsonrpclab/test_gen_jsonrpcstub_buddyQuoteService.php
r427 r479 1 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 2 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> 3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> 4 4 <head> 5 5 <title>JSON-RPC Test</title> … … 8 8 <script type="text/javascript" src="json.js"></script> 9 9 <script type="text/javascript" src="gen_jsonrpcstub_buddyQuoteService.js"></script> 10 <script type="text/javascript"> 11 12 // This will later be generated 13 function BuddyQuoteService(clientObj) { 14 this.dispatcher = new stubbles.json.rpc.Client(clientObj, '<?php echo dirname($_SERVER['PHP_SELF']);?>/jsonrpc.php'); 15 } 16 17 BuddyQuoteService.prototype = { 18 getQuote: function() { 19 return this.dispatcher.doCall('BuddyQuoteService.getQuote', arguments); 20 } 21 } 22 23 24 // This is application code... 25 var callbackObj = { 26 callback__getQuote: function(id, result, error) { 27 alert(result); 28 } 29 }; 30 31 var bq = new BuddyQuoteService(callbackObj); 32 </script> 33 34 <fieldset> 35 <legend>Basic JSON-RPC example</legend> 36 Quote (0-6) <input type="text" id="quoteId" size="3"/> 37 <input type="button" onclick="bq.getQuote(document.getElementById('quoteId').value);" value="Get the quote!"/> 38 </fieldset> 39 40 10 41 </head> 11 42 <body>
