JSONRequest.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * This class enables you to use JSON-RPC to call backend methods.
  12. *
  13. * @class tinymce.util.JSONRequest
  14. * @example
  15. * var json = new tinymce.util.JSONRequest({
  16. * url: 'somebackend.php'
  17. * });
  18. *
  19. * // Send RPC call 1
  20. * json.send({
  21. * method: 'someMethod1',
  22. * params: ['a', 'b'],
  23. * success: function(result) {
  24. * console.dir(result);
  25. * }
  26. * });
  27. *
  28. * // Send RPC call 2
  29. * json.send({
  30. * method: 'someMethod2',
  31. * params: ['a', 'b'],
  32. * success: function(result) {
  33. * console.dir(result);
  34. * }
  35. * });
  36. */
  37. define("tinymce/util/JSONRequest", [
  38. "tinymce/util/JSON",
  39. "tinymce/util/XHR",
  40. "tinymce/util/Tools"
  41. ], function(JSON, XHR, Tools) {
  42. var extend = Tools.extend;
  43. function JSONRequest(settings) {
  44. this.settings = extend({}, settings);
  45. this.count = 0;
  46. }
  47. /**
  48. * Simple helper function to send a JSON-RPC request without the need to initialize an object.
  49. * Consult the Wiki API documentation for more details on what you can pass to this function.
  50. *
  51. * @method sendRPC
  52. * @static
  53. * @param {Object} o Call object where there are three field id, method and params this object should also contain callbacks etc.
  54. */
  55. JSONRequest.sendRPC = function(o) {
  56. return new JSONRequest().send(o);
  57. };
  58. JSONRequest.prototype = {
  59. /**
  60. * Sends a JSON-RPC call. Consult the Wiki API documentation for more details on what you can pass to this function.
  61. *
  62. * @method send
  63. * @param {Object} args Call object where there are three field id, method and params this object should also contain callbacks etc.
  64. */
  65. send: function(args) {
  66. var ecb = args.error, scb = args.success;
  67. args = extend(this.settings, args);
  68. args.success = function(c, x) {
  69. c = JSON.parse(c);
  70. if (typeof(c) == 'undefined') {
  71. c = {
  72. error : 'JSON Parse error.'
  73. };
  74. }
  75. if (c.error) {
  76. ecb.call(args.error_scope || args.scope, c.error, x);
  77. } else {
  78. scb.call(args.success_scope || args.scope, c.result);
  79. }
  80. };
  81. args.error = function(ty, x) {
  82. if (ecb) {
  83. ecb.call(args.error_scope || args.scope, ty, x);
  84. }
  85. };
  86. args.data = JSON.serialize({
  87. id: args.id || 'c' + (this.count++),
  88. method: args.method,
  89. params: args.params
  90. });
  91. // JSON content type for Ruby on rails. Bug: #1883287
  92. args.content_type = 'application/json';
  93. XHR.send(args);
  94. }
  95. };
  96. return JSONRequest;
  97. });