Changeset 479

Show
Ignore:
Timestamp:
04/12/07 22:01:41 (2 years ago)
Author:
schst
Message:

Automatically detect the correct path, fixed various errors in javascript implementation, moved client into stubbles namespace

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... 
     2var stubbles = {}; 
     3stubbles.json = {}; 
     4stubbles.json.rpc = {}; 
    25 
     6 
     7stubbles.json.rpc.Client = function(clientObj, serviceURL) { 
    38  this.reqRespMapping = []; 
     9  this.serviceURL = serviceURL; 
    410 
    511  this.createId = function() { 
     
    814   return id; 
    915  } 
    10   
     16 
    1117  this.callback = { 
    1218   // closure needed 
     
    1622     if ( rpcObj.error == null ) { 
    1723        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) { 
    1925            var classAndMethod = this.reqRespMapping[i].method.split("."); 
    2026            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; 
    2328          } 
    2429        } 
     30        throw 'no related request id for response id found - mapping in js obj reqRespMapping failed!'; 
    2531      } else { 
    2632        throw rpcObj.error; 
    2733      } 
    28    },  
     34   }, 
    2935   failure: function(o) { 
    3036     throw 'callback error due to bad request from service (instead of HTTP status code 200)'; 
     
    3440  this.doCall = function(classAndMethod, args) { 
    3541    var id = this.createId(); 
    36      
     42 
    3743    // because args is an object and 
    3844    // not an array a conversion is needed 
     
    4046      arr[i] = args[i]; 
    4147    } 
    42       
    43     var postUrl = 'http://localhost/stubbles/docroot/jsonrpclab/' + phpfile +'?processor=jsonrpc'; 
     48 
     49    var postUrl = this.serviceURL +'?processor=jsonrpc'; 
    4450    var jsonRpcReq = { 
    4551      'method': classAndMethod, 
     
    4854    }; 
    4955    var transaction = YAHOO.util.Connect.asyncRequest('POST', postUrl, this.callback, jsonRpcReq.toJSONString()); 
    50       
     56 
    5157    this.reqRespMapping.push(jsonRpcReq); 
    5258    return id; 
     
    5662 
    5763// ------------------------------ 
    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  
    11<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    22    "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"> 
    44<head> 
    55  <title>JSON-RPC Test</title> 
     
    88  <script type="text/javascript" src="json.js"></script> 
    99  <script type="text/javascript" src="gen_jsonrpcstub_buddyQuoteService.js"></script> 
     10<script type="text/javascript"> 
     11 
     12// This will later be generated 
     13function BuddyQuoteService(clientObj) { 
     14 this.dispatcher = new stubbles.json.rpc.Client(clientObj, '<?php echo dirname($_SERVER['PHP_SELF']);?>/jsonrpc.php'); 
     15} 
     16 
     17BuddyQuoteService.prototype  = { 
     18  getQuote: function() { 
     19    return this.dispatcher.doCall('BuddyQuoteService.getQuote', arguments); 
     20  } 
     21} 
     22 
     23 
     24// This is application code... 
     25var callbackObj = { 
     26  callback__getQuote: function(id, result, error) { 
     27    alert(result); 
     28  } 
     29}; 
     30 
     31var 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 
    1041</head> 
    1142<body>