Upgrade to ExtJS 4.0.7 - Released 10/19/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  * @class Ext.direct.RemotingMethod
18  * @ignore
19  */
20 Ext.define('Ext.direct.RemotingMethod', {
21
22     constructor: function(config){
23         var me = this,
24             params = Ext.isDefined(config.params) ? config.params : config.len,
25             name;
26
27         me.name = config.name;
28         me.formHandler = config.formHandler;
29         if (Ext.isNumber(params)) {
30             // given only the number of parameters
31             me.len = params;
32             me.ordered = true;
33         } else {
34             /*
35              * Given an array of either
36              * a) String
37              * b) Objects with a name property. We may want to encode extra info in here later
38              */
39             me.params = [];
40             Ext.each(params, function(param){
41                 name = Ext.isObject(param) ? param.name : param;
42                 me.params.push(name);
43             });
44         }
45     },
46
47     /**
48      * Takes the arguments for the Direct function and splits the arguments
49      * from the scope and the callback.
50      * @param {Array} args The arguments passed to the direct call
51      * @return {Object} An object with 3 properties, args, callback & scope.
52      */
53     getCallData: function(args){
54         var me = this,
55             data = null,
56             len  = me.len,
57             params = me.params,
58             callback,
59             scope,
60             name;
61
62         if (me.ordered) {
63             callback = args[len];
64             scope = args[len + 1];
65             if (len !== 0) {
66                 data = args.slice(0, len);
67             }
68         } else {
69             data = Ext.apply({}, args[0]);
70             callback = args[1];
71             scope = args[2];
72
73             // filter out any non-existent properties
74             for (name in data) {
75                 if (data.hasOwnProperty(name)) {
76                     if (!Ext.Array.contains(params, name)) {
77                         delete data[name];
78                     }
79                 }
80             }
81         }
82
83         return {
84             data: data,
85             callback: callback,
86             scope: scope
87         };
88     }
89 });
90