]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/util/JSONRequest.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / util / JSONRequest.js
1 /**
2  * JSONRequest.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 (function() {
12         var extend = tinymce.extend, JSON = tinymce.util.JSON, XHR = tinymce.util.XHR;
13
14         /**
15          * This class enables you to use JSON-RPC to call backend methods.
16          *
17          * @class tinymce.util.JSONRequest
18          * @example
19          * var json = new tinymce.util.JSONRequest({
20          *     url : 'somebackend.php'
21          * });
22          * 
23          * // Send RPC call 1
24          * json.send({
25          *     method : 'someMethod1',
26          *     params : ['a', 'b'],
27          *     success : function(result) {
28          *         console.dir(result);
29          *     }
30          * });
31          * 
32          * // Send RPC call 2
33          * json.send({
34          *     method : 'someMethod2',
35          *     params : ['a', 'b'],
36          *     success : function(result) {
37          *         console.dir(result);
38          *     }
39          * });
40          */
41         tinymce.create('tinymce.util.JSONRequest', {
42                 /**
43                  * Constructs a new JSONRequest instance.
44                  *
45                  * @constructor
46                  * @method JSONRequest
47                  * @param {Object} s Optional settings object.
48                  */
49                 JSONRequest : function(s) {
50                         this.settings = extend({
51                         }, s);
52                         this.count = 0;
53                 },
54
55                 /**
56                  * Sends a JSON-RPC call. Consult the Wiki API documentation for more details on what you can pass to this function.
57                  *
58                  * @method send
59                  * @param {Object} o Call object where there are three field id, method and params this object should also contain callbacks etc.
60                  */
61                 send : function(o) {
62                         var ecb = o.error, scb = o.success;
63
64                         o = extend(this.settings, o);
65
66                         o.success = function(c, x) {
67                                 c = JSON.parse(c);
68
69                                 if (typeof(c) == 'undefined') {
70                                         c = {
71                                                 error : 'JSON Parse error.'
72                                         };
73                                 }
74
75                                 if (c.error)
76                                         ecb.call(o.error_scope || o.scope, c.error, x);
77                                 else
78                                         scb.call(o.success_scope || o.scope, c.result);
79                         };
80
81                         o.error = function(ty, x) {
82                                 if (ecb)
83                                         ecb.call(o.error_scope || o.scope, ty, x);
84                         };
85
86                         o.data = JSON.serialize({
87                                 id : o.id || 'c' + (this.count++),
88                                 method : o.method,
89                                 params : o.params
90                         });
91
92                         // JSON content type for Ruby on rails. Bug: #1883287
93                         o.content_type = 'application/json';
94
95                         XHR.send(o);
96                 },
97
98                 'static' : {
99                         /**
100                          * Simple helper function to send a JSON-RPC request without the need to initialize an object.
101                          * Consult the Wiki API documentation for more details on what you can pass to this function.
102                          *
103                          * @method sendRPC
104                          * @static
105                          * @param {Object} o Call object where there are three field id, method and params this object should also contain callbacks etc.
106                          */
107                         sendRPC : function(o) {
108                                 return new tinymce.util.JSONRequest().send(o);
109                         }
110                 }
111         });
112 }());