Roo/data/HttpProxy.js
[roojs1] / Roo / data / HttpProxy.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 /**
12  * @class Roo.data.HttpProxy
13  * @extends Roo.data.DataProxy
14  * An implementation of {@link Roo.data.DataProxy} that reads a data object from an {@link Roo.data.Connection} object
15  * configured to reference a certain URL.<br><br>
16  * <p>
17  * <em>Note that this class cannot be used to retrieve data from a domain other than the domain
18  * from which the running page was served.<br><br>
19  * <p>
20  * For cross-domain access to remote data, use an {@link Roo.data.ScriptTagProxy}.</em><br><br>
21  * <p>
22  * Be aware that to enable the browser to parse an XML document, the server must set
23  * the Content-Type header in the HTTP response to "text/xml".
24  * @constructor
25  * @param {Object} conn Connection config options to add to each request (e.g. {url: 'foo.php'} or
26  * an {@link Roo.data.Connection} object.  If a Connection config is passed, the singleton {@link Roo.Ajax} object
27  * will be used to make the request.
28  */
29 Roo.data.HttpProxy = function(conn){
30     Roo.data.HttpProxy.superclass.constructor.call(this);
31     // is conn a conn config or a real conn?
32     Roo.log('httpproxy:conn');
33     Roo.log(conn);
34             ')
35     this.conn = conn;
36     this.useAjax = !conn || !conn.events;
37   
38 };
39
40 Roo.extend(Roo.data.HttpProxy, Roo.data.DataProxy, {
41     // thse are take from connection...
42     
43     /**
44      * @cfg {String} url (Optional) The default URL to be used for requests to the server. (defaults to undefined)
45      */
46     /**
47      * @cfg {Object} extraParams (Optional) An object containing properties which are used as
48      * extra parameters to each request made by this object. (defaults to undefined)
49      */
50     /**
51      * @cfg {Object} defaultHeaders (Optional) An object containing request headers which are added
52      *  to each request made by this object. (defaults to undefined)
53      */
54     /**
55      * @cfg {String} method (Optional) The default HTTP method to be used for requests. (defaults to undefined; if not set but parms are present will use POST, otherwise GET)
56      */
57     /**
58      * @cfg {Number} timeout (Optional) The timeout in milliseconds to be used for requests. (defaults to 30000)
59      */
60      /**
61      * @cfg {Boolean} autoAbort (Optional) Whether this request should abort any pending requests. (defaults to false)
62      * @type Boolean
63      */
64   
65
66     /**
67      * @cfg {Boolean} disableCaching (Optional) True to add a unique cache-buster param to GET requests. (defaults to true)
68      * @type Boolean
69      */
70     /**
71      * Return the {@link Roo.data.Connection} object being used by this Proxy.
72      * @return {Connection} The Connection object. This object may be used to subscribe to events on
73      * a finer-grained basis than the DataProxy events.
74      */
75     getConnection : function(){
76         return this.useAjax ? Roo.Ajax : this.conn;
77     },
78
79     /**
80      * Load data from the configured {@link Roo.data.Connection}, read the data object into
81      * a block of Roo.data.Records using the passed {@link Roo.data.DataReader} implementation, and
82      * process that block using the passed callback.
83      * @param {Object} params An object containing properties which are to be used as HTTP parameters
84      * for the request to the remote server.
85      * @param {Roo.data.DataReader} reader The Reader object which converts the data
86      * object into a block of Roo.data.Records.
87      * @param {Function} callback The function into which to pass the block of Roo.data.Records.
88      * The function must be passed <ul>
89      * <li>The Record block object</li>
90      * <li>The "arg" argument from the load function</li>
91      * <li>A boolean success indicator</li>
92      * </ul>
93      * @param {Object} scope The scope in which to call the callback
94      * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
95      */
96     load : function(params, reader, callback, scope, arg){
97         if(this.fireEvent("beforeload", this, params) !== false){
98             var  o = {
99                 params : params || {},
100                 request: {
101                     callback : callback,
102                     scope : scope,
103                     arg : arg
104                 },
105                 reader: reader,
106                 callback : this.loadResponse,
107                 scope: this
108             };
109             if (this.conn.timeout) {
110                 Roo.log('setting timeout to ' + this.conn.timeout);
111                 o.timeout = this.conn.timeout;
112             }
113             if(this.useAjax){
114                 Roo.log("using ajax");
115                 Roo.applyIf(o, this.conn);
116                 if(this.activeRequest){
117                     Roo.Ajax.abort(this.activeRequest);
118                 }
119                 this.activeRequest = Roo.Ajax.request(o);
120             }else{
121                 Roo.log("not using ajax");
122                 this.conn.request(o);
123             }
124         }else{
125             callback.call(scope||this, null, arg, false);
126         }
127     },
128
129     // private
130     loadResponse : function(o, success, response){
131         delete this.activeRequest;
132         if(!success){
133             this.fireEvent("loadexception", this, o, response);
134             o.request.callback.call(o.request.scope, null, o.request.arg, false);
135             return;
136         }
137         var result;
138         try {
139             result = o.reader.read(response);
140         }catch(e){
141             this.fireEvent("loadexception", this, o, response, e);
142             o.request.callback.call(o.request.scope, null, o.request.arg, false);
143             return;
144         }
145         
146         this.fireEvent("load", this, o, o.request.arg);
147         o.request.callback.call(o.request.scope, result, o.request.arg, true);
148     },
149
150     // private
151     update : function(dataSet){
152
153     },
154
155     // private
156     updateResponse : function(dataSet){
157
158     }
159 });