]> git.wh0rd.org - tt-rss.git/blob - lib/dojo/rpc/JsonService.js
upgrade Dojo to 1.6.1
[tt-rss.git] / lib / dojo / rpc / JsonService.js
1 /*
2 Copyright (c) 2004-2011, 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
14 dojo.declare("dojo.rpc.JsonService", dojo.rpc.RpcService, {
15 bustCache: false,
16 contentType: "application/json-rpc",
17 lastSubmissionId: 0,
18
19 callRemote: function(method, params){
20 // summary:
21 // call an arbitrary remote method without requiring it to be
22 // predefined with SMD
23 // method: string
24 // the name of the remote method you want to call.
25 // params: array
26 // array of parameters to pass to method
27
28 var deferred = new dojo.Deferred();
29 this.bind(method, params, deferred);
30 return deferred;
31 },
32
33 bind: function(method, parameters, deferredRequestHandler, url){
34 //summary:
35 // JSON-RPC bind method. Takes remote method, parameters,
36 // deferred, and a url, calls createRequest to make a JSON-RPC
37 // envelope and passes that off with bind.
38 // method: string
39 // The name of the method we are calling
40 // parameters: array
41 // The parameters we are passing off to the method
42 // deferredRequestHandler: deferred
43 // The Deferred object for this particular request
44
45 var def = dojo.rawXhrPost({
46 url: url||this.serviceUrl,
47 postData: this.createRequest(method, parameters),
48 contentType: this.contentType,
49 timeout: this.timeout,
50 handleAs: "json-comment-optional"
51 });
52 def.addCallbacks(this.resultCallback(deferredRequestHandler), this.errorCallback(deferredRequestHandler));
53 },
54
55 createRequest: function(method, params){
56 // summary:
57 // create a JSON-RPC envelope for the request
58 // method: string
59 // The name of the method we are creating the requst for
60 // params: array
61 // The array of parameters for this request;
62
63 var req = { "params": params, "method": method, "id": ++this.lastSubmissionId };
64 var data = dojo.toJson(req);
65 return data;
66 },
67
68 parseResults: function(/*anything*/obj){
69 //summary:
70 // parse the result envelope and pass the results back to
71 // the callback function
72 // obj: Object
73 // Object containing envelope of data we recieve from the server
74
75 if(dojo.isObject(obj)){
76 if("result" in obj){
77 return obj.result;
78 }
79 if("Result" in obj){
80 return obj.Result;
81 }
82 if("ResultSet" in obj){
83 return obj.ResultSet;
84 }
85 }
86 return obj;
87 }
88 }
89 );
90
91 }