Changeset 1085

Show
Ignore:
Timestamp:
11/29/07 10:54:19 (1 year ago)
Author:
richi
Message:

javascript: code cleanup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/javascript/stub-json-rpc-debug.js

    r866 r1085  
    1616 */ 
    1717stubbles.json.rpc.Client = function(clientObj, serviceURL) { 
    18   this.reqRespMapping = []; 
    19   this.serviceURL = serviceURL; 
     18    var reqRespMapping = []; 
    2019 
    21   this.createId = function() { 
    22    var d = new Date(); 
    23    var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 
    24    return id; 
    25   } 
     20    this.createId = function() { 
     21        var d = new Date(); 
     22        var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 
     23        return id; 
     24    }; 
    2625 
    27   this.callback = { 
    28    // closure needed 
    29    reqRespMapping: this.reqRespMapping, 
    30    success: function(o) { 
    31      var rpcObj = o.responseText.parseJSON(); 
    32      if ( rpcObj.error == null ) { 
    33         for (var i=0; i < this.reqRespMapping.length; i++) { 
    34           if (rpcObj.id == this.reqRespMapping[i].id) { 
    35             var classAndMethod = this.reqRespMapping[i].method.split("."); 
    36             console.info("Stubbles::REQID " + rpcObj.id +  " :: Calling callback method callback__" + classAndMethod[1] + "("+rpcObj.id+", "+rpcObj.result+", "+rpcObj.error+")"); 
    37             clientObj.eval('callback__' + classAndMethod[1] + '(rpcObj.id, rpcObj.result, rpcObj.error)'); 
    38             return; 
    39           } 
     26    this.callback = { 
     27        success: function(o) { 
     28            var rpcObj = o.responseText.parseJSON(); 
     29            if ( rpcObj.error === null ) { 
     30                for (var i=0; i < reqRespMapping.length; i++) { 
     31                    if (rpcObj.id === reqRespMapping[i].id) { 
     32                        var classAndMethod = reqRespMapping[i].method.split("."); 
     33                        console.info("Stubbles::REQID " + rpcObj.id +  " :: Calling callback method callback__" + classAndMethod[1] + "("+rpcObj.id+", "+rpcObj.result+", "+rpcObj.error+")"); 
     34 
     35                        // call callback method on clientObj 
     36                        var methodName = 'callback__' + classAndMethod[1]; 
     37                        clientObj[methodName].call(clientObj, rpcObj.id, rpcObj.result, rpcObj.error); 
     38                        return; 
     39                    } 
     40                } 
     41                console.error("Stubbles::REQID " + rpcObj.id +  " :: Invalid request id."); 
     42                throw 'no related request id for response id found - mapping in js obj reqRespMapping failed!'; 
     43            } else { 
     44                console.error("Stubbles::REQID " + rpcObj.id +  " :: ERROR: " + rpcObj.error); 
     45                throw rpcObj.error; 
     46            } 
     47        }, 
     48        failure: function(o) { 
     49            console.error("Stubbles::REQID n/a :: ERROR: HTTP Request failed."); 
     50            throw 'callback error due to bad request from service (instead of HTTP status code 200)'; 
    4051        } 
    41         console.error("Stubbles::REQID " + rpcObj.id +  " :: Invalid request id."); 
    42         throw 'no related request id for response id found - mapping in js obj reqRespMapping failed!'; 
    43       } else { 
    44         console.error("Stubbles::REQID " + rpcObj.id +  " :: ERROR: " + rpcObj.error); 
    45         throw rpcObj.error; 
    46       } 
    47    }, 
    48    failure: function(o) { 
    49      console.error("Stubbles::REQID n/a :: ERROR: HTTP Request failed."); 
    50      throw 'callback error due to bad request from service (instead of HTTP status code 200)'; 
    51    } 
     52    }; 
     53 
     54    this.doCall = function(classAndMethod, args) { 
     55        var id = this.createId(); 
     56 
     57        // because args is an array-like object and 
     58        // not an array a conversion is needed 
     59        for (var i=0, arr=[]; i < args.length; i++) { 
     60            arr[i] = args[i]; 
     61        } 
     62 
     63        var postUrl = serviceURL; 
     64        if (stubbles.json.rpc.appendToURL !== null) { 
     65            postUrl = postUrl + stubbles.json.rpc.appendToURL; 
     66        } 
     67         
     68        var jsonRpcReq = { 
     69            method: classAndMethod, 
     70            params: arr, 
     71            id: id 
     72        }; 
     73         
     74        YAHOO.util.Connect.asyncRequest('POST', postUrl, this.callback, jsonRpcReq.toJSONString()); 
     75        console.info("Stubbles::REQID " + id +  " :: Calling method " + classAndMethod); 
     76        reqRespMapping.push(jsonRpcReq); 
     77        return id; 
    5278  }; 
    53  
    54   this.doCall = function(classAndMethod, args) { 
    55     var id = this.createId(); 
    56  
    57     // because args is an object and 
    58     // not an array a conversion is needed 
    59     for (var i = 0, arr=[]; i < args.length; i++) { 
    60       arr[i] = args[i]; 
    61     } 
    62  
    63     var postUrl = this.serviceURL; 
    64     if (stubbles.json.rpc.appendToURL != null) { 
    65         postUrl = postUrl + stubbles.json.rpc.appendToURL; 
    66     } 
    67     var jsonRpcReq = { 
    68       'method': classAndMethod, 
    69       'params': arr, 
    70       'id': id, 
    71     }; 
    72     var transaction = YAHOO.util.Connect.asyncRequest('POST', postUrl, this.callback, jsonRpcReq.toJSONString()); 
    73     console.info("Stubbles::REQID " + id +  " :: Calling method " + classAndMethod); 
    74  
    75     this.reqRespMapping.push(jsonRpcReq); 
    76     return id; 
    77   } 
    78 
     79}; 
  • trunk/src/main/javascript/stub-json-rpc.js

    r866 r1085  
    1616 */ 
    1717stubbles.json.rpc.Client = function(clientObj, serviceURL) { 
    18   this.reqRespMapping = []; 
    19   this.serviceURL = serviceURL; 
     18    var reqRespMapping = []; 
    2019 
    21   this.createId = function() { 
    22    var d = new Date(); 
    23    var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 
    24    return id; 
    25   } 
     20    this.createId = function() { 
     21        var d = new Date(); 
     22        var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 
     23        return id; 
     24    }; 
    2625 
    27   this.callback = { 
    28    // closure needed 
    29    reqRespMapping: this.reqRespMapping, 
    30    success: function(o) { 
    31      var rpcObj = o.responseText.parseJSON(); 
    32      if ( rpcObj.error == null ) { 
    33         for (var i=0; i < this.reqRespMapping.length; i++) { 
    34           if (rpcObj.id == this.reqRespMapping[i].id) { 
    35             var classAndMethod = this.reqRespMapping[i].method.split("."); 
    36             clientObj.eval('callback__' + classAndMethod[1] + '(rpcObj.id, rpcObj.result, rpcObj.error)'); 
    37             return; 
    38           } 
     26    this.callback = { 
     27        success: function(o) { 
     28            var rpcObj = o.responseText.parseJSON(); 
     29            if ( rpcObj.error === null ) { 
     30                for (var i=0; i < reqRespMapping.length; i++) { 
     31                    if (rpcObj.id === reqRespMapping[i].id) { 
     32                        var classAndMethod = reqRespMapping[i].method.split("."); 
     33                        var methodName = 'callback__' + classAndMethod[1]; 
     34                         
     35                        // call callback method on clientObj 
     36                        clientObj[methodName].call(clientObj, rpcObj.id, rpcObj.result, rpcObj.error); 
     37                        return; 
     38                    } 
     39                } 
     40                throw 'no related request id for response id found - mapping in js obj reqRespMapping failed!'; 
     41            } else { 
     42                throw rpcObj.error; 
     43            } 
     44        }, 
     45        failure: function(o) { 
     46            throw 'callback error due to bad request from service (instead of HTTP status code 200)'; 
    3947        } 
    40         throw 'no related request id for response id found - mapping in js obj reqRespMapping failed!'; 
    41       } else { 
    42         throw rpcObj.error; 
    43       } 
    44    }, 
    45    failure: function(o) { 
    46      throw 'callback error due to bad request from service (instead of HTTP status code 200)'; 
    47    } 
    48   }; 
     48    }; 
    4949 
    50   this.doCall = function(classAndMethod, args) { 
    51     var id = this.createId(); 
     50    this.doCall = function(classAndMethod, args) { 
     51        var id = this.createId(); 
    5252 
    53     // because args is an object and 
    54     // not an array a conversion is needed 
    55     for (var i = 0, arr=[]; i < args.length; i++) { 
    56       arr[i] = args[i]; 
    57    
     53        // because args is an array-like object and 
     54        // not an array a conversion is needed 
     55        for (var i=0, arr=[]; i < args.length; i++) { 
     56            arr[i] = args[i]; 
     57       
    5858 
    59     var postUrl = this.serviceURL; 
    60     if (stubbles.json.rpc.appendToURL != null) { 
    61         postUrl = postUrl + stubbles.json.rpc.appendToURL; 
    62     } 
    63     var jsonRpcReq = { 
    64       'method': classAndMethod, 
    65       'params': arr, 
    66       'id': id, 
     59        var postUrl = serviceURL; 
     60        if (stubbles.json.rpc.appendToURL !== null) { 
     61            postUrl = postUrl + stubbles.json.rpc.appendToURL; 
     62        } 
     63     
     64        var jsonRpcReq = { 
     65            method: classAndMethod, 
     66            params: arr, 
     67            id: id 
     68        }; 
     69     
     70        YAHOO.util.Connect.asyncRequest('POST', postUrl, this.callback, jsonRpcReq.toJSONString()); 
     71        reqRespMapping.push(jsonRpcReq); 
     72        return id; 
    6773    }; 
    68     var transaction = YAHOO.util.Connect.asyncRequest('POST', postUrl, this.callback, jsonRpcReq.toJSONString()); 
    69  
    70     this.reqRespMapping.push(jsonRpcReq); 
    71     return id; 
    72   } 
    73 
     74};