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
|
/*
Script: Rpc.js
A JSON-RPC proxy built ontop of mootools.
Copyright:
Damien Churchill (c) 2008 <damoxc@gmail.com>
Class: JSON.RPC
Class to create a proxy to a json-rpc interface on a server.
Example:
client = new JSON.RPC('/json/rpc');
client.hello_world({
onSuccess: function(result) {
alert(result);
}
});
alert(client.hello_world({async: false;}));
client.add_name('Damien', {
onSuccess: function(result) {
alert(result);
}
});
Returns:
The proxy that can be used to directly call methods on the server.
*/
JSON.RPC = new Class({
Implements: Options,
options: {
async: true,
methods: []
},
initialize: function(url, options) {
this.setOptions(options)
this.url = url
if (this.options.methods.length == 0) {
this._execute('system.listMethods', {async: false}).each(function(method) {
this[method] = function() {
var options = this._parseargs(arguments)
return this._execute(method, options)
}.bind(this)
}, this)
}
},
/*
Property: _parseargs
Internal method for parsing the arguments given to the method
Arguments:
args - A list of the methods arguments
Returns:
An options object with the arguments set as options.params
*/
_parseargs: function(args) {
var params = $A(args), options = params.getLast()
if ($type(options) == 'object') {
var option_keys = ['async', 'onRequest', 'onComplete',
'onSuccess', 'onFailure', 'onException', 'onCancel'], keys =
new Hash(options).getKeys(), is_option = false
option_keys.each(function(key) {
if (keys.contains(key)) {
is_option = true
}
})
if (is_option) {
params.erase(options)
} else {
options = {}
}
} else { options = {} }
options.params = params
return options
},
/*
Property: _execute
An internal method to make the call to the rpc page
Arguements:
method - the name of the method
options - An options dict providing any additional options for the
call.
Example:
alert(client.hello_world({async: false;}));
Returns:
If not async returns the json result
*/
_execute: function(method, options) {
options = $pick(options, {})
options.params = $pick(options.params, [])
options.async = $pick(options.async, this.options.async)
data = JSON.encode({
method: method,
params: options.params,
id: 1
})
var request = new Request.JSON({
url: this.url,
async: options.async,
onRequest: options.onRequest,
onComplete: options.onComplete,
onSuccess: function(response) {
if (options.onSuccess) {options.onSuccess(response.result)}
},
onFailure: options.onFailure,
onException: options.onException,
onCancel: options.onCancel
}).send(data)
if (!options.async) {
return request.response.json.result
}
}
})
|