JsonService_wev8.js
4.34 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.rpc.JsonService");
dojo.require("dojo.io.*");
dojo.require("dojo.json");
dojo.require("dojo.lang");
dojo.rpc.JsonService = function(url){
if(url){
this.connect(url);
}
}
dojo.lang.extend(dojo.rpc.JsonService, {
status: "LOADING",
lastSubmissionId:0,
createJsonRpcRequest: function(parameters, method, id){
dojo.debug("JsonService: Create JSON-RPC Request.");
var req = { "params": parameters, "method": method, "id": id };
data = dojo.json.serialize(req);
dojo.debug("JsonService: JSON-RPC Request: " + data);
return data;
},
JsonRpcCallback: function(resultsCallbackFunction, /* optional */ errorCallbackFunction){
return function(type, object, e){
//dojo.debug("Returned object: Id: " + object.id + " Results: " + object.result + " Error: " + object.error);
this.error = function(e){
dojo.debug("JsonService: Error in Callback: " + e);
}
if(dojo.lang.isFunction(resultsCallbackFunction)){
this.results = resultsCallbackFunction;
}else{
dojo.raise("JsonService: First argument to JsonRpcCallback must be the resultCallbackFunction");
}
if(dojo.lang.isFunction(errorCallbackFunction)){
this.error = errorCallbackFunction;
}
if(object.e != null){
if(dojo.lang.isFunction(this.error)){
this.error(object.error);
}else{
}
}else{
if(dojo.lang.isFunction(this.results)){
this.results(object.result, /* optional */ object.id);
}else{
dojo.debug("JsonService: Results received but no callback method was specified.");
}
}
};
},
createRemoteJsonRpcMethod: function(serviceURL, method, params){
return function(){
dojo.debug("JsonService: Executing Remote Method");
if(params){
var numberExpectedParameters = params.length;
}else{
var numberExpectedParameters = 0;
}
if(arguments.length < numberExpectedParameters){
dojo.raise("Invalid number of parameters for remote method.");
// put error stuff here, no enough params
}else if(arguments.length > numberExpectedParameters){
if(dojo.lang.isFunction(arguments[arguments.length-1])){
if(arguments.length-1 == numberExpectedParameters){
var p = [];
for(var n=0; n<numberExpectedParameters; n++){
p[n] = arguments[n];
}
dojo.io.bind({
url: serviceURL,
postContent: this.createJsonRpcRequest(p, method, this.lastSubmissionId++),
method: "POST",
mimetype: "text/json",
load: this.JsonRpcCallback(arguments[arguments.length-1])
});
return this.lastSubmissionId-1;
}else{
dojo.raise("Too many parameters supplied for remote method.");
}
}else{
//put error stuff here, extra params but the last one isn't a callback func
dojo.raise("More parameters than require and/or the extra parameter isn't a callback function");
}
}else{
dojo.raise("No Callback function supplied and synchronous rpc calls haven't been implemented");
}
};
},
processJSDL: function (type, object, e) {
dojo.debug("JsonService: Processing returned JSDL.");
dojo.debug("JsonService: Creating " + object.className + " object.");
for(var n = 0; n < object.methods.length; n++){
dojo.debug("JsonService: Creating Method: this." + object.methods[n].name + "()");
this[object.methods[n].name] = this.createRemoteJsonRpcMethod(object.serviceURL, object.methods[n].name,object.methods[n].parameters);
}
this.status="READY";
dojo.debug("JsonService: Dojo RPC Object is ready for use.");
},
viewJSDL:function(type, object, e){
dojo.debug(object);
},
connect: function(jsdlURL){
dojo.debug("JsonService: Attempting to load jsdl document from " + jsdlURL);
dojo.io.bind({
url: jsdlURL,
mimetype: "text/json",
load: dojo.lang.hitch(this, function(type, object, e){ return this.processJSDL(type,object,e) })
//comment out the above and change to below if you need to see the jsdl in a debug statement
//mimetype: "text/plain",
//load: dojo.lang.hitch(this,function(type,object,e){ return this.viewJSDL(type,object,e) })
});
}
});