DirectProxy.js
上传用户:dawnssy
上传日期:2022-08-06
资源大小:9345k
文件大小:7k
源码类别:

JavaScript

开发平台:

JavaScript

  1. /*!  * Ext JS Library 3.1.0  * Copyright(c) 2006-2009 Ext JS, LLC  * licensing@extjs.com  * http://www.extjs.com/license  */ /**
  2.  * @class Ext.data.DirectProxy
  3.  * @extends Ext.data.DataProxy
  4.  */
  5. Ext.data.DirectProxy = function(config){
  6.     Ext.apply(this, config);
  7.     if(typeof this.paramOrder == 'string'){
  8.         this.paramOrder = this.paramOrder.split(/[s,|]/);
  9.     }
  10.     Ext.data.DirectProxy.superclass.constructor.call(this, config);
  11. };
  12. Ext.extend(Ext.data.DirectProxy, Ext.data.DataProxy, {
  13.     /**
  14.      * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. A list of params to be executed
  15.      * server side.  Specify the params in the order in which they must be executed on the server-side
  16.      * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,
  17.      * comma, or pipe. For example,
  18.      * any of the following would be acceptable:<pre><code>
  19. paramOrder: ['param1','param2','param3']
  20. paramOrder: 'param1 param2 param3'
  21. paramOrder: 'param1,param2,param3'
  22. paramOrder: 'param1|param2|param'
  23.      </code></pre>
  24.      */
  25.     paramOrder: undefined,
  26.     /**
  27.      * @cfg {Boolean} paramsAsHash
  28.      * Send parameters as a collection of named arguments (defaults to <tt>true</tt>). Providing a
  29.      * <tt>{@link #paramOrder}</tt> nullifies this configuration.
  30.      */
  31.     paramsAsHash: true,
  32.     /**
  33.      * @cfg {Function} directFn
  34.      * Function to call when executing a request.  directFn is a simple alternative to defining the api configuration-parameter
  35.      * for Store's which will not implement a full CRUD api.
  36.      */
  37.     directFn : undefined,
  38.     /**
  39.      * DirectProxy implementation of {@link Ext.data.DataProxy#doRequest}
  40.      * @param {String} action The crud action type (create, read, update, destroy)
  41.      * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null
  42.      * @param {Object} params An object containing properties which are to be used as HTTP parameters
  43.      * for the request to the remote server.
  44.      * @param {Ext.data.DataReader} reader The Reader object which converts the data
  45.      * object into a block of Ext.data.Records.
  46.      * @param {Function} callback
  47.      * <div class="sub-desc"><p>A function to be called after the request.
  48.      * The <tt>callback</tt> is passed the following arguments:<ul>
  49.      * <li><tt>r</tt> : Ext.data.Record[] The block of Ext.data.Records.</li>
  50.      * <li><tt>options</tt>: Options object from the action request</li>
  51.      * <li><tt>success</tt>: Boolean success indicator</li></ul></p></div>
  52.      * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
  53.      * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
  54.      * @protected
  55.      */
  56.     doRequest : function(action, rs, params, reader, callback, scope, options) {
  57.         var args = [],
  58.             directFn = this.api[action] || this.directFn;
  59.         switch (action) {
  60.             case Ext.data.Api.actions.create:
  61.                 args.push(params.jsonData); // <-- create(Hash)
  62.                 break;
  63.             case Ext.data.Api.actions.read:
  64.                 // If the method has no parameters, ignore the paramOrder/paramsAsHash.
  65.                 if(directFn.directCfg.method.len > 0){
  66.                     if(this.paramOrder){
  67.                         for(var i = 0, len = this.paramOrder.length; i < len; i++){
  68.                             args.push(params[this.paramOrder[i]]);
  69.                         }
  70.                     }else if(this.paramsAsHash){
  71.                         args.push(params);
  72.                     }
  73.                 }
  74.                 break;
  75.             case Ext.data.Api.actions.update:
  76.                 args.push(params.jsonData);        // <-- update(Hash/Hash[])
  77.                 break;
  78.             case Ext.data.Api.actions.destroy:
  79.                 args.push(params.jsonData);        // <-- destroy(Int/Int[])
  80.                 break;
  81.         }
  82.         var trans = {
  83.             params : params || {},
  84.             request: {
  85.                 callback : callback,
  86.                 scope : scope,
  87.                 arg : options
  88.             },
  89.             reader: reader
  90.         };
  91.         args.push(this.createCallback(action, rs, trans), this);
  92.         directFn.apply(window, args);
  93.     },
  94.     // private
  95.     createCallback : function(action, rs, trans) {
  96.         return function(result, res) {
  97.             if (!res.status) {
  98.                 // @deprecated fire loadexception
  99.                 if (action === Ext.data.Api.actions.read) {
  100.                     this.fireEvent("loadexception", this, trans, res, null);
  101.                 }
  102.                 this.fireEvent('exception', this, 'remote', action, trans, res, null);
  103.                 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);
  104.                 return;
  105.             }
  106.             if (action === Ext.data.Api.actions.read) {
  107.                 this.onRead(action, trans, result, res);
  108.             } else {
  109.                 this.onWrite(action, trans, result, res, rs);
  110.             }
  111.         };
  112.     },
  113.     /**
  114.      * Callback for read actions
  115.      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  116.      * @param {Object} trans The request transaction object
  117.      * @param {Object} result Data object picked out of the server-response.
  118.      * @param {Object} res The server response
  119.      * @protected
  120.      */
  121.     onRead : function(action, trans, result, res) {
  122.         var records;
  123.         try {
  124.             records = trans.reader.readRecords(result);
  125.         }
  126.         catch (ex) {
  127.             // @deprecated: Fire old loadexception for backwards-compat.
  128.             this.fireEvent("loadexception", this, trans, res, ex);
  129.             this.fireEvent('exception', this, 'response', action, trans, res, ex);
  130.             trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);
  131.             return;
  132.         }
  133.         this.fireEvent("load", this, res, trans.request.arg);
  134.         trans.request.callback.call(trans.request.scope, records, trans.request.arg, true);
  135.     },
  136.     /**
  137.      * Callback for write actions
  138.      * @param {String} action [{@link Ext.data.Api#actions create|read|update|destroy}]
  139.      * @param {Object} trans The request transaction object
  140.      * @param {Object} result Data object picked out of the server-response.
  141.      * @param {Object} res The server response
  142.      * @param {Ext.data.Record/[Ext.data.Record]} rs The Store resultset associated with the action.
  143.      * @protected
  144.      */
  145.     onWrite : function(action, trans, result, res, rs) {
  146.         var data = trans.reader.extractData(result, false);
  147.         this.fireEvent("write", this, action, data, res, rs, trans.request.arg);
  148.         trans.request.callback.call(trans.request.scope, data, res, true);
  149.     }
  150. });