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