summaryrefslogtreecommitdiff
path: root/lib/dojo/rpc/JsonService.js
blob: 064fd0fee5528a0c0458d5aba5d324b1b06243c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


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(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;
		}
	}
);

}