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
/**
* JSONRequest.js
*
* Copyright 2009, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://tinymce.moxiecode.com/license
* Contributing: http://tinymce.moxiecode.com/contributing
*/
(function() {
var extend = tinymce.extend, JSON = tinymce.util.JSON, XHR = tinymce.util.XHR;
/**
* This class enables you to use JSON-RPC to call backend methods.
*
* @class tinymce.util.JSONRequest
* @example
* var json = new tinymce.util.JSONRequest({
* url : 'somebackend.php'
* });
*
* // Send RPC call 1
* json.send({
* method : 'someMethod1',
* params : ['a', 'b'],
* success : function(result) {
* console.dir(result);
* }
* });
*
* // Send RPC call 2
* json.send({
* method : 'someMethod2',
* params : ['a', 'b'],
* success : function(result) {
* console.dir(result);
* }
* });
*/
tinymce.create('tinymce.util.JSONRequest', {
/**
* Constructs a new JSONRequest instance.
*
* @constructor
* @method JSONRequest
* @param {Object} s Optional settings object.
*/
JSONRequest : function(s) {
this.settings = extend({
}, s);
this.count = 0;
},
/**
* Sends a JSON-RPC call. Consult the Wiki API documentation for more details on what you can pass to this function.
*
* @method send
* @param {Object} o Call object where there are three field id, method and params this object should also contain callbacks etc.
*/
send : function(o) {
var ecb = o.error, scb = o.success;
o = extend(this.settings, o);
o.success = function(c, x) {
c = JSON.parse(c);
if (typeof(c) == 'undefined') {
c = {
error : 'JSON Parse error.'
};
}
if (c.error)
ecb.call(o.error_scope || o.scope, c.error, x);
else
scb.call(o.success_scope || o.scope, c.result);
};
o.error = function(ty, x) {
if (ecb)
ecb.call(o.error_scope || o.scope, ty, x);
};
o.data = JSON.serialize({
id : o.id || 'c' + (this.count++),
method : o.method,
params : o.params
});
// JSON content type for Ruby on rails. Bug: #1883287
o.content_type = 'application/json';
XHR.send(o);
},
'static' : {
/**
* Simple helper function to send a JSON-RPC request without the need to initialize an object.
* Consult the Wiki API documentation for more details on what you can pass to this function.
*
* @method sendRPC
* @static
* @param {Object} o Call object where there are three field id, method and params this object should also contain callbacks etc.
*/
sendRPC : function(o) {
return new tinymce.util.JSONRequest().send(o);
}
}
});
}());