]> git.wh0rd.org - tt-rss.git/blob - lib/dojo/rpc/JsonService.js
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
[tt-rss.git] / lib / dojo / rpc / JsonService.js
1 /*
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5 */
6
7
8 if(!dojo._hasResource["dojo.rpc.JsonService"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo._hasResource["dojo.rpc.JsonService"] = true;
10 dojo.provide("dojo.rpc.JsonService");
11 dojo.require("dojo.rpc.RpcService");
12
13 dojo.declare("dojo.rpc.JsonService", dojo.rpc.RpcService, {
14 bustCache: false,
15 contentType: "application/json-rpc",
16 lastSubmissionId: 0,
17
18 callRemote: function(method, params){
19 // summary:
20 // call an arbitrary remote method without requiring it to be
21 // predefined with SMD
22 // method: string
23 // the name of the remote method you want to call.
24 // params: array
25 // array of parameters to pass to method
26
27 var deferred = new dojo.Deferred();
28 this.bind(method, params, deferred);
29 return deferred;
30 },
31
32 bind: function(method, parameters, deferredRequestHandler, url){
33 //summary:
34 // JSON-RPC bind method. Takes remote method, parameters,
35 // deferred, and a url, calls createRequest to make a JSON-RPC
36 // envelope and passes that off with bind.
37 // method: string
38 // The name of the method we are calling
39 // parameters: array
40 // The parameters we are passing off to the method
41 // deferredRequestHandler: deferred
42 // The Deferred object for this particular request
43
44 var def = dojo.rawXhrPost({
45 url: url||this.serviceUrl,
46 postData: this.createRequest(method, parameters),
47 contentType: this.contentType,
48 timeout: this.timeout,
49 handleAs: "json-comment-optional"
50 });
51 def.addCallbacks(this.resultCallback(deferredRequestHandler), this.errorCallback(deferredRequestHandler));
52 },
53
54 createRequest: function(method, params){
55 // summary:
56 // create a JSON-RPC envelope for the request
57 // method: string
58 // The name of the method we are creating the requst for
59 // params: array
60 // The array of parameters for this request;
61
62 var req = { "params": params, "method": method, "id": ++this.lastSubmissionId };
63 var data = dojo.toJson(req);
64 return data;
65 },
66
67 parseResults: function(/*anything*/obj){
68 //summary:
69 // parse the result envelope and pass the results back to
70 // the callback function
71 // obj: Object
72 // Object containing envelope of data we recieve from the server
73
74 if(dojo.isObject(obj)){
75 if("result" in obj){
76 return obj.result;
77 }
78 if("Result" in obj){
79 return obj.Result;
80 }
81 if("ResultSet" in obj){
82 return obj.ResultSet;
83 }
84 }
85 return obj;
86 }
87 }
88 );
89
90 }