PageRenderTime 55ms CodeModel.GetById 29ms app.highlight 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ext-4.1.0_b3/src/data/proxy/Direct.js

https://bitbucket.org/srogerf/javascript
JavaScript | 181 lines | 76 code | 26 blank | 79 comment | 11 complexity | 2f30e33771ea645c2c7ebcebbf5a60ac MD5 | raw file
  1/**
  2 * This class is used to send requests to the server using {@link Ext.direct.Manager Ext.Direct}. When a
  3 * request is made, the transport mechanism is handed off to the appropriate
  4 * {@link Ext.direct.RemotingProvider Provider} to complete the call.
  5 *
  6 * # Specifying the function
  7 *
  8 * This proxy expects a Direct remoting method to be passed in order to be able to complete requests.
  9 * This can be done by specifying the {@link #directFn} configuration. This will use the same direct
 10 * method for all requests. Alternatively, you can provide an {@link #api} configuration. This
 11 * allows you to specify a different remoting method for each CRUD action.
 12 *
 13 * # Parameters
 14 *
 15 * This proxy provides options to help configure which parameters will be sent to the server.
 16 * By specifying the {@link #paramsAsHash} option, it will send an object literal containing each
 17 * of the passed parameters. The {@link #paramOrder} option can be used to specify the order in which
 18 * the remoting method parameters are passed.
 19 *
 20 * # Example Usage
 21 *
 22 *     Ext.define('User', {
 23 *         extend: 'Ext.data.Model',
 24 *         fields: ['firstName', 'lastName'],
 25 *         proxy: {
 26 *             type: 'direct',
 27 *             directFn: MyApp.getUsers,
 28 *             paramOrder: 'id' // Tells the proxy to pass the id as the first parameter to the remoting method.
 29 *         }
 30 *     });
 31 *     User.load(1);
 32 */
 33Ext.define('Ext.data.proxy.Direct', {
 34    /* Begin Definitions */
 35
 36    extend: 'Ext.data.proxy.Server',
 37    alternateClassName: 'Ext.data.DirectProxy',
 38
 39    alias: 'proxy.direct',
 40
 41    requires: ['Ext.direct.Manager'],
 42
 43    /* End Definitions */
 44
 45    /**
 46     * @cfg {String/String[]} paramOrder
 47     * Defaults to undefined. A list of params to be executed server side.  Specify the params in the order in
 48     * which they must be executed on the server-side as either (1) an Array of String values, or (2) a String
 49     * of params delimited by either whitespace, comma, or pipe. For example, any of the following would be
 50     * acceptable:
 51     *
 52     *     paramOrder: ['param1','param2','param3']
 53     *     paramOrder: 'param1 param2 param3'
 54     *     paramOrder: 'param1,param2,param3'
 55     *     paramOrder: 'param1|param2|param'
 56     */
 57    paramOrder: undefined,
 58
 59    /**
 60     * @cfg {Boolean} paramsAsHash
 61     * Send parameters as a collection of named arguments.
 62     * Providing a {@link #paramOrder} nullifies this configuration.
 63     */
 64    paramsAsHash: true,
 65
 66    /**
 67     * @cfg {Function/String} directFn
 68     * Function to call when executing a request. directFn is a simple alternative to defining the api configuration-parameter
 69     * for Store's which will not implement a full CRUD api. The directFn may also be a string reference to the fully qualified
 70     * name of the function, for example: 'MyApp.company.GetProfile'. This can be useful when using dynamic loading. The string 
 71     * will be looked up when the proxy is created.
 72     */
 73    directFn : undefined,
 74
 75    /**
 76     * @cfg {Object} api
 77     * The same as {@link Ext.data.proxy.Server#api}, however instead of providing urls, you should provide a direct
 78     * function call. See {@link #directFn}.
 79     */
 80
 81    /**
 82     * @cfg {Object} extraParams
 83     * Extra parameters that will be included on every read request. Individual requests with params
 84     * of the same name will override these params when they are in conflict.
 85     */
 86
 87    // private
 88    paramOrderRe: /[\s,|]/,
 89
 90    constructor: function(config){
 91        var me = this,
 92            paramOrder,
 93            fn,
 94            api;
 95            
 96        me.callParent(arguments);
 97        
 98        paramOrder = me.paramOrder;
 99        if (Ext.isString(paramOrder)) {
100            me.paramOrder = paramOrder.split(me.paramOrderRe);
101        }
102        
103        fn = me.directFn;
104        if (fn) {
105            me.directFn = Ext.direct.Manager.parseMethod(fn);
106        }
107        
108        api = me.api;
109        for (fn in api) {
110            if (api.hasOwnProperty(fn)) {
111                api[fn] = Ext.direct.Manager.parseMethod(api[fn]);
112            }
113        }
114    },
115
116    doRequest: function(operation, callback, scope) {
117        var me = this,
118            writer = me.getWriter(),
119            request = me.buildRequest(operation, callback, scope),
120            fn = me.api[request.action]  || me.directFn,
121            params = request.params,
122            args = [],
123            method;
124
125        //<debug>
126        if (!fn) {
127            Ext.Error.raise('No direct function specified for this proxy');
128        }
129        //</debug>
130
131        if (operation.allowWrite()) {
132            request = writer.write(request);
133        }
134
135        if (operation.action == 'read') {
136            // We need to pass params
137            method = fn.directCfg.method;
138            args = method.getArgs(params, me.paramOrder, me.paramsAsHash);
139        } else {
140            args.push(request.jsonData);
141        }
142
143        Ext.apply(request, {
144            args: args,
145            directFn: fn
146        });
147        args.push(me.createRequestCallback(request, operation, callback, scope), me);
148        fn.apply(window, args);
149    },
150
151    /*
152     * Inherit docs. We don't apply any encoding here because
153     * all of the direct requests go out as jsonData
154     */
155    applyEncoding: function(value){
156        return value;
157    },
158
159    createRequestCallback: function(request, operation, callback, scope){
160        var me = this;
161
162        return function(data, event){
163            me.processResponse(event.status, operation, request, event, callback, scope);
164        };
165    },
166
167    // inherit docs
168    extractResponseData: function(response){
169        return Ext.isDefined(response.result) ? response.result : response.data;
170    },
171
172    // inherit docs
173    setException: function(operation, response) {
174        operation.setException(response.message);
175    },
176
177    // inherit docs
178    buildUrl: function(){
179        return '';
180    }
181});