4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 var extend = tinymce.extend, JSON = tinymce.util.JSON, XHR = tinymce.util.XHR;
15 * This class enables you to use JSON-RPC to call backend methods.
17 * @class tinymce.util.JSONRequest
19 * var json = new tinymce.util.JSONRequest({
20 * url : 'somebackend.php'
25 * method : 'someMethod1',
26 * params : ['a', 'b'],
27 * success : function(result) {
28 * console.dir(result);
34 * method : 'someMethod2',
35 * params : ['a', 'b'],
36 * success : function(result) {
37 * console.dir(result);
41 tinymce.create('tinymce.util.JSONRequest', {
43 * Constructs a new JSONRequest instance.
47 * @param {Object} s Optional settings object.
49 JSONRequest : function(s) {
50 this.settings = extend({
56 * Sends a JSON-RPC call. Consult the Wiki API documentation for more details on what you can pass to this function.
59 * @param {Object} o Call object where there are three field id, method and params this object should also contain callbacks etc.
62 var ecb = o.error, scb = o.success;
64 o = extend(this.settings, o);
66 o.success = function(c, x) {
69 if (typeof(c) == 'undefined') {
71 error : 'JSON Parse error.'
76 ecb.call(o.error_scope || o.scope, c.error, x);
78 scb.call(o.success_scope || o.scope, c.result);
81 o.error = function(ty, x) {
83 ecb.call(o.error_scope || o.scope, ty, x);
86 o.data = JSON.serialize({
87 id : o.id || 'c' + (this.count++),
92 // JSON content type for Ruby on rails. Bug: #1883287
93 o.content_type = 'application/json';
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.
105 * @param {Object} o Call object where there are three field id, method and params this object should also contain callbacks etc.
107 sendRPC : function(o) {
108 return new tinymce.util.JSONRequest().send(o);