1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.rpc.RpcService");
|
|
|
14 |
dojo.require("dojo.io.*");
|
|
|
15 |
dojo.require("dojo.json");
|
|
|
16 |
dojo.require("dojo.lang.func");
|
|
|
17 |
dojo.require("dojo.Deferred");
|
|
|
18 |
dojo.rpc.RpcService = function (url) {
|
|
|
19 |
if (url) {
|
|
|
20 |
this.connect(url);
|
|
|
21 |
}
|
|
|
22 |
};
|
|
|
23 |
dojo.lang.extend(dojo.rpc.RpcService, {strictArgChecks:true, serviceUrl:"", parseResults:function (obj) {
|
|
|
24 |
return obj;
|
|
|
25 |
}, errorCallback:function (deferredRequestHandler) {
|
|
|
26 |
return function (type, e) {
|
|
|
27 |
deferredRequestHandler.errback(new Error(e.message));
|
|
|
28 |
};
|
|
|
29 |
}, resultCallback:function (deferredRequestHandler) {
|
|
|
30 |
var tf = dojo.lang.hitch(this, function (type, obj, e) {
|
|
|
31 |
if (obj["error"] != null) {
|
|
|
32 |
var err = new Error(obj.error);
|
|
|
33 |
err.id = obj.id;
|
|
|
34 |
deferredRequestHandler.errback(err);
|
|
|
35 |
} else {
|
|
|
36 |
var results = this.parseResults(obj);
|
|
|
37 |
deferredRequestHandler.callback(results);
|
|
|
38 |
}
|
|
|
39 |
});
|
|
|
40 |
return tf;
|
|
|
41 |
}, generateMethod:function (method, parameters, url) {
|
|
|
42 |
return dojo.lang.hitch(this, function () {
|
|
|
43 |
var deferredRequestHandler = new dojo.Deferred();
|
|
|
44 |
if ((this.strictArgChecks) && (parameters != null) && (arguments.length != parameters.length)) {
|
|
|
45 |
dojo.raise("Invalid number of parameters for remote method.");
|
|
|
46 |
} else {
|
|
|
47 |
this.bind(method, arguments, deferredRequestHandler, url);
|
|
|
48 |
}
|
|
|
49 |
return deferredRequestHandler;
|
|
|
50 |
});
|
|
|
51 |
}, processSmd:function (object) {
|
|
|
52 |
dojo.debug("RpcService: Processing returned SMD.");
|
|
|
53 |
if (object.methods) {
|
|
|
54 |
dojo.lang.forEach(object.methods, function (m) {
|
|
|
55 |
if (m && m["name"]) {
|
|
|
56 |
dojo.debug("RpcService: Creating Method: this.", m.name, "()");
|
|
|
57 |
this[m.name] = this.generateMethod(m.name, m.parameters, m["url"] || m["serviceUrl"] || m["serviceURL"]);
|
|
|
58 |
if (dojo.lang.isFunction(this[m.name])) {
|
|
|
59 |
dojo.debug("RpcService: Successfully created", m.name, "()");
|
|
|
60 |
} else {
|
|
|
61 |
dojo.debug("RpcService: Failed to create", m.name, "()");
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
}, this);
|
|
|
65 |
}
|
|
|
66 |
this.serviceUrl = object.serviceUrl || object.serviceURL;
|
|
|
67 |
dojo.debug("RpcService: Dojo RpcService is ready for use.");
|
|
|
68 |
}, connect:function (smdUrl) {
|
|
|
69 |
dojo.debug("RpcService: Attempting to load SMD document from:", smdUrl);
|
|
|
70 |
dojo.io.bind({url:smdUrl, mimetype:"text/json", load:dojo.lang.hitch(this, function (type, object, e) {
|
|
|
71 |
return this.processSmd(object);
|
|
|
72 |
}), sync:true});
|
|
|
73 |
}});
|
|
|
74 |
|