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