]> git.proxmox.com Git - extjs.git/blob - extjs/packages/core/src/direct/RemotingMethod.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / src / direct / RemotingMethod.js
1 /**
2 * @private
3 * Small utility class used internally to represent a Direct method.
4 */
5 Ext.define('Ext.direct.RemotingMethod', {
6
7 constructor: function(config) {
8 var me = this,
9 params = config.params,
10 len = config.len,
11 metadataCfg = config.metadata,
12 metadata = {},
13 name, pLen, p, param;
14
15 me.name = config.name;
16 me.disableBatching = config.batched != null ? !config.batched : false;
17
18 if (config.formHandler) {
19 me.formHandler = config.formHandler;
20 }
21 else if (Ext.isNumeric(len)) {
22 // given only the number of parameters
23 me.len = len;
24 me.ordered = true;
25 }
26 else {
27 /*
28 * Given an array of either
29 * a) String
30 * b) Objects with a name property. We may want to encode extra info in here later
31 * c) Empty array signifies no mandatory parameters
32 */
33 me.named = true;
34 me.strict = config.strict !== undefined ? config.strict : true;
35 me.params = {};
36
37 // params may not be defined for a formHandler, or named method
38 // with no strict checking
39 pLen = params && params.length;
40
41 for (p = 0; p < pLen; p++) {
42 param = params[p];
43 name = Ext.isObject(param) ? param.name : param;
44 me.params[name] = true;
45 }
46 }
47
48 if (metadataCfg) {
49 params = metadataCfg.params;
50 len = metadataCfg.len;
51
52 if (Ext.isNumeric(len)) {
53 //<debug>
54 if (len === 0) {
55 Ext.raise('metadata.len cannot be 0 ' +
56 'for Ext Direct method ' + me.name);
57 }
58 //</debug>
59
60 metadata.ordered = true;
61 metadata.len = len;
62 }
63 else if (Ext.isArray(params)) {
64 metadata.named = true;
65 metadata.params = {};
66
67 for (p = 0, pLen = params.length; p < pLen; p++) {
68 param = params[p];
69 metadata.params[param] = true;
70 }
71
72 metadata.strict = metadataCfg.strict !== undefined ? metadataCfg.strict : true;
73 }
74 //<debug>
75 else {
76 Ext.raise('metadata is neither named nor ordered ' +
77 'for Ext Direct method ' + me.name);
78 }
79 //</debug>
80
81 me.metadata = metadata;
82 }
83 },
84
85 /**
86 * Prepare Direct function arguments that can be used with getCallData().
87 */
88 getArgs: function(config) {
89 var me = this,
90 params = config.params,
91 paramOrder = config.paramOrder,
92 paramsAsHash = config.paramsAsHash,
93 metadata = config.metadata,
94 options = config.options,
95 args = [],
96 i, len;
97
98 if (me.ordered) {
99 if (me.len > 0) {
100 // If a paramOrder was specified, add the params into the argument list in that order.
101 if (paramOrder) {
102 for (i = 0, len = paramOrder.length; i < len; i++) {
103 args.push(params[paramOrder[i]]);
104 }
105 }
106 else if (paramsAsHash) {
107 // If paramsAsHash was specified, add all the params as a single object argument.
108 args.push(params);
109 }
110 }
111 }
112 else {
113 args.push(params);
114 }
115
116 args.push(config.callback, config.scope || window);
117
118 if (options || metadata) {
119 options = Ext.apply({}, options);
120
121 if (metadata) {
122 // Could be either an object of named arguments,
123 // or an array of ordered arguments
124 options.metadata = metadata;
125 }
126
127 args.push(options);
128 }
129
130 return args;
131 },
132
133 /**
134 * Takes the arguments for a Direct function and splits the arguments
135 * from the scope and the callback.
136 *
137 * @param {Array} args The arguments passed to the direct call
138 *
139 * @return {Object} An object with 4 properties: args, callback, scope, and options object.
140 */
141 getCallData: function(args) {
142 var me = this,
143 data = null,
144 len = me.len,
145 params = me.params,
146 strict = me.strict,
147 form, callback, scope, name, options, metadata;
148
149 // Historically, the presence of required arguments was not checked;
150 // another idiosyncrasy is that null is sent to the server side
151 // instead of empty array when len === 0
152 if (me.ordered) {
153 callback = args[len];
154 scope = args[len + 1];
155 options = args[len + 2];
156
157 if (len !== 0) {
158 data = args.slice(0, len);
159 }
160 }
161 else if (me.formHandler) {
162 form = args[0];
163 callback = args[1];
164 scope = args[2];
165 options = args[3];
166 }
167 else {
168 data = Ext.apply({}, args[0]);
169 callback = args[1];
170 scope = args[2];
171 options = args[3];
172
173 // filter out any non-existent properties unless !strict
174 if (strict) {
175 for (name in data) {
176 if (data.hasOwnProperty(name) && !params[name]) {
177 delete data[name];
178 }
179 }
180 }
181 }
182
183 if (me.metadata && options && options.metadata) {
184 if (me.metadata.ordered) {
185 //<debug>
186 if (!Ext.isArray(options.metadata)) {
187 Ext.raise('options.metadata is not an Array ' +
188 'for Ext Direct method ' + me.name);
189 }
190 else if (options.metadata.length < me.metadata.len) {
191 Ext.raise('Not enough parameters in options.metadata ' +
192 'for Ext Direct method ' + me.name);
193 }
194 //</debug>
195
196 metadata = options.metadata.slice(0, me.metadata.len);
197 }
198 else {
199 //<debug>
200 if (!Ext.isObject(options.metadata)) {
201 Ext.raise('options.metadata is not an Object ' +
202 'for Ext Direct method ' + me.name);
203 }
204 //</debug>
205
206 metadata = Ext.apply({}, options.metadata);
207
208 if (me.metadata.strict) {
209 for (name in metadata) {
210 if (metadata.hasOwnProperty(name) && !me.metadata.params[name]) {
211 delete metadata[name];
212 }
213 }
214 }
215
216 //<debug>
217 for (name in me.metadata.params) {
218 if (!metadata.hasOwnProperty(name)) {
219 Ext.raise('Named parameter ' + name + ' is missing ' +
220 'in options.metadata for Ext Direct method ' +
221 me.name);
222 }
223 }
224 //</debug>
225 }
226
227 delete options.metadata;
228 }
229
230 return {
231 form: form,
232 data: data,
233 metadata: metadata,
234 callback: callback,
235 scope: scope,
236 options: options
237 };
238 }
239 });