Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / direct / RemotingMethod.js
1 /**
2  * Small utility class used internally to represent a Direct method.
3  * Thi class is used internally.
4  * @class Ext.direct.RemotingMethod
5  * @ignore
6  */
7 Ext.define('Ext.direct.RemotingMethod', {
8     
9     constructor: function(config){
10         var me = this,
11             params = Ext.isDefined(config.params) ? config.params : config.len,
12             name;
13             
14         me.name = config.name;
15         me.formHandler = config.formHandler;
16         if (Ext.isNumber(params)) {
17             // given only the number of parameters
18             me.len = params;
19             me.ordered = true;
20         } else {
21             /*
22              * Given an array of either
23              * a) String
24              * b) Objects with a name property. We may want to encode extra info in here later
25              */
26             me.params = [];
27             Ext.each(params, function(param){
28                 name = Ext.isObject(param) ? param.name : param;
29                 me.params.push(name);
30             });
31         }
32     },
33     
34     /**
35      * Takes the arguments for the Direct function and splits the arguments
36      * from the scope and the callback.
37      * @param {Array} args The arguments passed to the direct call
38      * @return {Object} An object with 3 properties, args, callback & scope.
39      */
40     getCallData: function(args){
41         var me = this,
42             data = null,
43             len  = me.len,
44             params = me.params,
45             callback,
46             scope,
47             name;
48             
49         if (me.ordered) {
50             callback = args[len];
51             scope = args[len + 1];
52             if (len !== 0) {
53                 data = args.slice(0, len);
54             }
55         } else {
56             data = Ext.apply({}, args[0]);
57             callback = args[1];
58             scope = args[2];
59             
60             // filter out any non-existent properties
61             for (name in data) {
62                 if (data.hasOwnProperty(name)) {
63                     if (!Ext.Array.contains(params, name)) {
64                         delete data[name];
65                     }
66                 }
67             }
68         }
69         
70         return {
71             data: data,
72             callback: callback,
73             scope: scope    
74         };
75     }
76 });