Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
11
dojo.provide("dojo.rpc.RpcService");
12
dojo.require("dojo.io.*");
13
dojo.require("dojo.json");
14
dojo.require("dojo.lang.func");
15
dojo.require("dojo.Deferred");
16
dojo.rpc.RpcService = function (url) {
17
	if (url) {
18
		this.connect(url);
19
	}
20
};
21
dojo.lang.extend(dojo.rpc.RpcService, {strictArgChecks:true, serviceUrl:"", parseResults:function (obj) {
22
	return obj;
23
}, errorCallback:function (deferredRequestHandler) {
24
	return function (type, e) {
25
		deferredRequestHandler.errback(new Error(e.message));
26
	};
27
}, resultCallback:function (deferredRequestHandler) {
28
	var tf = dojo.lang.hitch(this, function (type, obj, e) {
29
		if (obj["error"] != null) {
30
			var err = new Error(obj.error);
31
			err.id = obj.id;
32
			deferredRequestHandler.errback(err);
33
		} else {
34
			var results = this.parseResults(obj);
35
			deferredRequestHandler.callback(results);
36
		}
37
	});
38
	return tf;
39
}, generateMethod:function (method, parameters, url) {
40
	return dojo.lang.hitch(this, function () {
41
		var deferredRequestHandler = new dojo.Deferred();
42
		if ((this.strictArgChecks) && (parameters != null) && (arguments.length != parameters.length)) {
43
			dojo.raise("Invalid number of parameters for remote method.");
44
		} else {
45
			this.bind(method, arguments, deferredRequestHandler, url);
46
		}
47
		return deferredRequestHandler;
48
	});
49
}, processSmd:function (object) {
50
	dojo.debug("RpcService: Processing returned SMD.");
51
	if (object.methods) {
52
		dojo.lang.forEach(object.methods, function (m) {
53
			if (m && m["name"]) {
54
				dojo.debug("RpcService: Creating Method: this.", m.name, "()");
55
				this[m.name] = this.generateMethod(m.name, m.parameters, m["url"] || m["serviceUrl"] || m["serviceURL"]);
56
				if (dojo.lang.isFunction(this[m.name])) {
57
					dojo.debug("RpcService: Successfully created", m.name, "()");
58
				} else {
59
					dojo.debug("RpcService: Failed to create", m.name, "()");
60
				}
61
			}
62
		}, this);
63
	}
64
	this.serviceUrl = object.serviceUrl || object.serviceURL;
65
	dojo.debug("RpcService: Dojo RpcService is ready for use.");
66
}, connect:function (smdUrl) {
67
	dojo.debug("RpcService: Attempting to load SMD document from:", smdUrl);
68
	dojo.io.bind({url:smdUrl, mimetype:"text/json", load:dojo.lang.hitch(this, function (type, object, e) {
69
		return this.processSmd(object);
70
	}), sync:true});
71
}});
72