From a089699c8915636ba4f158d77dba9b012bc93208 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 4 Mar 2011 19:02:28 +0300 Subject: build custom layer of Dojo to speed up loading of tt-rss (refs #293) --- lib/dojo/rpc/JsonService.js | 107 +++++++++++++++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 27 deletions(-) (limited to 'lib/dojo/rpc/JsonService.js') diff --git a/lib/dojo/rpc/JsonService.js b/lib/dojo/rpc/JsonService.js index b88e5f531..757e328be 100644 --- a/lib/dojo/rpc/JsonService.js +++ b/lib/dojo/rpc/JsonService.js @@ -5,33 +5,86 @@ */ -if(!dojo._hasResource["dojo.rpc.JsonService"]){ -dojo._hasResource["dojo.rpc.JsonService"]=true; +if(!dojo._hasResource["dojo.rpc.JsonService"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojo.rpc.JsonService"] = true; dojo.provide("dojo.rpc.JsonService"); dojo.require("dojo.rpc.RpcService"); -dojo.declare("dojo.rpc.JsonService",dojo.rpc.RpcService,{bustCache:false,contentType:"application/json-rpc",lastSubmissionId:0,callRemote:function(_1,_2){ -var _3=new dojo.Deferred(); -this.bind(_1,_2,_3); -return _3; -},bind:function(_4,_5,_6,_7){ -var _8=dojo.rawXhrPost({url:_7||this.serviceUrl,postData:this.createRequest(_4,_5),contentType:this.contentType,timeout:this.timeout,handleAs:"json-comment-optional"}); -_8.addCallbacks(this.resultCallback(_6),this.errorCallback(_6)); -},createRequest:function(_9,_a){ -var _b={"params":_a,"method":_9,"id":++this.lastSubmissionId}; -var _c=dojo.toJson(_b); -return _c; -},parseResults:function(_d){ -if(dojo.isObject(_d)){ -if("result" in _d){ -return _d.result; -} -if("Result" in _d){ -return _d.Result; -} -if("ResultSet" in _d){ -return _d.ResultSet; -} -} -return _d; -}}); + +dojo.declare("dojo.rpc.JsonService", dojo.rpc.RpcService, { + bustCache: false, + contentType: "application/json-rpc", + lastSubmissionId: 0, + + callRemote: function(method, params){ + // summary: + // call an arbitrary remote method without requiring it to be + // predefined with SMD + // method: string + // the name of the remote method you want to call. + // params: array + // array of parameters to pass to method + + var deferred = new dojo.Deferred(); + this.bind(method, params, deferred); + return deferred; + }, + + bind: function(method, parameters, deferredRequestHandler, url){ + //summary: + // JSON-RPC bind method. Takes remote method, parameters, + // deferred, and a url, calls createRequest to make a JSON-RPC + // envelope and passes that off with bind. + // method: string + // The name of the method we are calling + // parameters: array + // The parameters we are passing off to the method + // deferredRequestHandler: deferred + // The Deferred object for this particular request + + var def = dojo.rawXhrPost({ + url: url||this.serviceUrl, + postData: this.createRequest(method, parameters), + contentType: this.contentType, + timeout: this.timeout, + handleAs: "json-comment-optional" + }); + def.addCallbacks(this.resultCallback(deferredRequestHandler), this.errorCallback(deferredRequestHandler)); + }, + + createRequest: function(method, params){ + // summary: + // create a JSON-RPC envelope for the request + // method: string + // The name of the method we are creating the requst for + // params: array + // The array of parameters for this request; + + var req = { "params": params, "method": method, "id": ++this.lastSubmissionId }; + var data = dojo.toJson(req); + return data; + }, + + parseResults: function(/*anything*/obj){ + //summary: + // parse the result envelope and pass the results back to + // the callback function + // obj: Object + // Object containing envelope of data we recieve from the server + + if(dojo.isObject(obj)){ + if("result" in obj){ + return obj.result; + } + if("Result" in obj){ + return obj.Result; + } + if("ResultSet" in obj){ + return obj.ResultSet; + } + } + return obj; + } + } +); + } -- cgit v1.2.3