src/pyams_skin/resources/js/myams-jsonrpc.js
changeset 556 3b84c6308d55
child 486 48b7cea0d903
equal deleted inserted replaced
-1:000000000000 556:3b84c6308d55
       
     1 /**
       
     2  * MyAMS JSON-RPC features
       
     3  */
       
     4 (function($, globals) {
       
     5 
       
     6 	var MyAMS = globals.MyAMS,
       
     7 		ams = MyAMS;
       
     8 
       
     9 	MyAMS.jsonrpc = {
       
    10 
       
    11 		/**
       
    12 		 * Get address relative to current page
       
    13 		 */
       
    14 		getAddr: function(addr) {
       
    15 			var href = addr || $('HTML HEAD BASE').attr('href') || window.location.href;
       
    16 			var target = href.replace(/\+\+skin\+\+\w+\//, '');
       
    17 			return target.substr(0, target.lastIndexOf("/") + 1);
       
    18 		},
       
    19 
       
    20 		/**
       
    21 		 * Execute JSON-RPC request on given method
       
    22 		 *
       
    23 		 * Query can be given as a simple "query" string or as an object containing all query parameters.
       
    24 		 * Parameters:
       
    25 		 *  - @query: query string (posted as "query" parameter) or object containing all parameters
       
    26 		 *  - @method: name of JSON-RPC procedure to call
       
    27 		 *  - @options: additional JSON-RPC procedure parameters
       
    28 		 *  - @callback: name of a callback which will be called on server response
       
    29 		 */
       
    30 		query: function(query, method, options, callback) {
       
    31 			ams.ajax.check($.jsonRPC,
       
    32 						   ams.baseURL + 'ext/jquery-jsonrpc' + ams.devext + '.js',
       
    33 						   function() {
       
    34 								if (typeof(options) === 'function') {
       
    35 									callback = options;
       
    36 									options = {};
       
    37 								}
       
    38 								else if (!options) {
       
    39 									options = {};
       
    40 								}
       
    41 								if (callback === 'undefined') {
       
    42 									callback = options.callback;
       
    43 								}
       
    44 								if (typeof(callback) === 'string') {
       
    45 									callback = ams.getFunctionByName(callback);
       
    46 								}
       
    47 								delete options.callback;
       
    48 
       
    49 								var params = {};
       
    50 								if (typeof(query) === 'string') {
       
    51 									params.query = query;
       
    52 								} else if (typeof(query) === 'object') {
       
    53 									$.extend(params, query);
       
    54 								}
       
    55 								$.extend(params, options);
       
    56 
       
    57 								var result;
       
    58 								var defaults = {
       
    59 									id: new Date().getTime(),
       
    60 									params: params,
       
    61 									success: callback || function(data) {
       
    62 										result = data;
       
    63 									},
       
    64 									error: ams.error.show
       
    65 								};
       
    66 								var settings = $.extend({}, defaults, options);
       
    67 								$.jsonRPC.withOptions({
       
    68 									endPoint: ams.jsonrpc.getAddr(options.url),
       
    69 									namespace: options.namespace,
       
    70 									cache: false
       
    71 								}, function() {
       
    72 									$.jsonRPC.request(method, settings);
       
    73 								});
       
    74 								return result;
       
    75 						   });
       
    76 		},
       
    77 
       
    78 		/**
       
    79 		 * Execute given JSON-RPC post on given method
       
    80 		 *
       
    81 		 * Parameters:
       
    82 		 *  - @method: name of JSON-RPC procedure to call
       
    83 		 *  - @options: additional JSON-RPC method call parameters
       
    84 		 *  - @callback: name of a callback which will be called on server response
       
    85 		 */
       
    86 		post: function(method, data, options, callback) {
       
    87 			ams.ajax.check($.jsonRPC,
       
    88 						   ams.baseURL + 'ext/jquery-jsonrpc' + ams.devext + '.js',
       
    89 						   function() {
       
    90 								if (typeof(options) === 'function') {
       
    91 									callback = options;
       
    92 									options = {};
       
    93 								}
       
    94 								else if (!options) {
       
    95 									options = {};
       
    96 								}
       
    97 								if (typeof(callback) === 'undefined') {
       
    98 									callback = options.callback;
       
    99 								}
       
   100 								if (typeof(callback) === 'string') {
       
   101 									callback = ams.getFunctionByName(callback);
       
   102 								}
       
   103 								delete options.callback;
       
   104 
       
   105 								var result;
       
   106 								var defaults = {
       
   107 									id: new Date().getTime(),
       
   108 									params: data,
       
   109 									success: callback || function(data) {
       
   110 										result = data;
       
   111 									},
       
   112 									error: ams.error.show
       
   113 								};
       
   114 								var settings = $.extend({}, defaults, options);
       
   115 								$.jsonRPC.withOptions({
       
   116 									endPoint: ams.jsonrpc.getAddr(options.url),
       
   117 									namespace: options.namespace,
       
   118 									cache: false
       
   119 								}, function() {
       
   120 									$.jsonRPC.request(method, settings);
       
   121 								});
       
   122 								return result;
       
   123 						   });
       
   124 		}
       
   125 	};
       
   126 
       
   127 })(jQuery, this);