Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / direct / RemotingMethod.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * Small utility class used internally to represent a Direct method.
17  * Thi class is used internally.
18  * @class Ext.direct.RemotingMethod
19  * @ignore
20  */
21 Ext.define('Ext.direct.RemotingMethod', {
22     
23     constructor: function(config){
24         var me = this,
25             params = Ext.isDefined(config.params) ? config.params : config.len,
26             name;
27             
28         me.name = config.name;
29         me.formHandler = config.formHandler;
30         if (Ext.isNumber(params)) {
31             // given only the number of parameters
32             me.len = params;
33             me.ordered = true;
34         } else {
35             /*
36              * Given an array of either
37              * a) String
38              * b) Objects with a name property. We may want to encode extra info in here later
39              */
40             me.params = [];
41             Ext.each(params, function(param){
42                 name = Ext.isObject(param) ? param.name : param;
43                 me.params.push(name);
44             });
45         }
46     },
47     
48     /**
49      * Takes the arguments for the Direct function and splits the arguments
50      * from the scope and the callback.
51      * @param {Array} args The arguments passed to the direct call
52      * @return {Object} An object with 3 properties, args, callback & scope.
53      */
54     getCallData: function(args){
55         var me = this,
56             data = null,
57             len  = me.len,
58             params = me.params,
59             callback,
60             scope,
61             name;
62             
63         if (me.ordered) {
64             callback = args[len];
65             scope = args[len + 1];
66             if (len !== 0) {
67                 data = args.slice(0, len);
68             }
69         } else {
70             data = Ext.apply({}, args[0]);
71             callback = args[1];
72             scope = args[2];
73             
74             // filter out any non-existent properties
75             for (name in data) {
76                 if (data.hasOwnProperty(name)) {
77                     if (!Ext.Array.contains(params, name)) {
78                         delete data[name];
79                     }
80                 }
81             }
82         }
83         
84         return {
85             data: data,
86             callback: callback,
87             scope: scope    
88         };
89     }
90 });
91