1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.data.HttpProxy |
---|
11 | * @extends Ext.data.DataProxy |
---|
12 | * An implementation of {@link Ext.data.DataProxy} that reads a data object from a {@link Ext.data.Connection Connection} object |
---|
13 | * configured to reference a certain URL.<br> |
---|
14 | * <p> |
---|
15 | * <b>Note that this class cannot be used to retrieve data from a domain other than the domain |
---|
16 | * from which the running page was served.<br> |
---|
17 | * <p> |
---|
18 | * For cross-domain access to remote data, use a {@link Ext.data.ScriptTagProxy ScriptTagProxy}.</b><br> |
---|
19 | * <p> |
---|
20 | * Be aware that to enable the browser to parse an XML document, the server must set |
---|
21 | * the Content-Type header in the HTTP response to "text/xml". |
---|
22 | * @constructor |
---|
23 | * @param {Object} conn an {@link Ext.data.Connection} object, or options parameter to {@link Ext.Ajax#request}. |
---|
24 | * If an options parameter is passed, the singleton {@link Ext.Ajax} object will be used to make the request. |
---|
25 | */ |
---|
26 | Ext.data.HttpProxy = function(conn){ |
---|
27 | Ext.data.HttpProxy.superclass.constructor.call(this); |
---|
28 | /** |
---|
29 | * The Connection object (Or options parameter to {@link Ext.Ajax#request}) which this HttpProxy uses to make requests to the server. |
---|
30 | * Properties of this object may be changed dynamically to change the way data is requested. |
---|
31 | * @property |
---|
32 | */ |
---|
33 | this.conn = conn; |
---|
34 | this.useAjax = !conn || !conn.events; |
---|
35 | |
---|
36 | /** |
---|
37 | * @event loadexception |
---|
38 | * Fires if an exception occurs in the Proxy during data loading. This event can be fired for one of two reasons: |
---|
39 | * <ul><li><b>The load call returned success: false.</b> This means the server logic returned a failure |
---|
40 | * status and there is no data to read. In this case, this event will be raised and the |
---|
41 | * fourth parameter (read error) will be null.</li> |
---|
42 | * <li><b>The load succeeded but the reader could not read the response.</b> This means the server returned |
---|
43 | * data, but the configured Reader threw an error while reading the data. In this case, this event will be |
---|
44 | * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul> |
---|
45 | * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly |
---|
46 | * on any Store instance. |
---|
47 | * @param {Object} this |
---|
48 | * @param {Object} options The loading options that were specified (see {@link #load} for details) |
---|
49 | * @param {Object} response The XMLHttpRequest object containing the response data |
---|
50 | * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data. |
---|
51 | * If the load call returned success: false, this parameter will be null. |
---|
52 | */ |
---|
53 | }; |
---|
54 | |
---|
55 | Ext.extend(Ext.data.HttpProxy, Ext.data.DataProxy, { |
---|
56 | /** |
---|
57 | * Return the {@link Ext.data.Connection} object being used by this Proxy. |
---|
58 | * @return {Connection} The Connection object. This object may be used to subscribe to events on |
---|
59 | * a finer-grained basis than the DataProxy events. |
---|
60 | */ |
---|
61 | getConnection : function(){ |
---|
62 | return this.useAjax ? Ext.Ajax : this.conn; |
---|
63 | }, |
---|
64 | |
---|
65 | /** |
---|
66 | * Load data from the configured {@link Ext.data.Connection}, read the data object into |
---|
67 | * a block of Ext.data.Records using the passed {@link Ext.data.DataReader} implementation, and |
---|
68 | * process that block using the passed callback. |
---|
69 | * @param {Object} params An object containing properties which are to be used as HTTP parameters |
---|
70 | * for the request to the remote server. |
---|
71 | * @param {Ext.data.DataReader} reader The Reader object which converts the data |
---|
72 | * object into a block of Ext.data.Records. |
---|
73 | * @param {Function} callback The function into which to pass the block of Ext.data.Records. |
---|
74 | * The function must be passed <ul> |
---|
75 | * <li>The Record block object</li> |
---|
76 | * <li>The "arg" argument from the load function</li> |
---|
77 | * <li>A boolean success indicator</li> |
---|
78 | * </ul> |
---|
79 | * @param {Object} scope The scope in which to call the callback |
---|
80 | * @param {Object} arg An optional argument which is passed to the callback as its second parameter. |
---|
81 | */ |
---|
82 | load : function(params, reader, callback, scope, arg){ |
---|
83 | if(this.fireEvent("beforeload", this, params) !== false){ |
---|
84 | var o = { |
---|
85 | params : params || {}, |
---|
86 | request: { |
---|
87 | callback : callback, |
---|
88 | scope : scope, |
---|
89 | arg : arg |
---|
90 | }, |
---|
91 | reader: reader, |
---|
92 | callback : this.loadResponse, |
---|
93 | scope: this |
---|
94 | }; |
---|
95 | if(this.useAjax){ |
---|
96 | Ext.applyIf(o, this.conn); |
---|
97 | if(this.activeRequest){ |
---|
98 | Ext.Ajax.abort(this.activeRequest); |
---|
99 | } |
---|
100 | this.activeRequest = Ext.Ajax.request(o); |
---|
101 | }else{ |
---|
102 | this.conn.request(o); |
---|
103 | } |
---|
104 | }else{ |
---|
105 | callback.call(scope||this, null, arg, false); |
---|
106 | } |
---|
107 | }, |
---|
108 | |
---|
109 | // private |
---|
110 | loadResponse : function(o, success, response){ |
---|
111 | delete this.activeRequest; |
---|
112 | if(!success){ |
---|
113 | this.fireEvent("loadexception", this, o, response); |
---|
114 | o.request.callback.call(o.request.scope, null, o.request.arg, false); |
---|
115 | return; |
---|
116 | } |
---|
117 | var result; |
---|
118 | try { |
---|
119 | result = o.reader.read(response); |
---|
120 | }catch(e){ |
---|
121 | this.fireEvent("loadexception", this, o, response, e); |
---|
122 | o.request.callback.call(o.request.scope, null, o.request.arg, false); |
---|
123 | return; |
---|
124 | } |
---|
125 | this.fireEvent("load", this, o, o.request.arg); |
---|
126 | o.request.callback.call(o.request.scope, result, o.request.arg, true); |
---|
127 | }, |
---|
128 | |
---|
129 | // private |
---|
130 | update : function(dataSet){ |
---|
131 | |
---|
132 | }, |
---|
133 | |
---|
134 | // private |
---|
135 | updateResponse : function(dataSet){ |
---|
136 | |
---|
137 | } |
---|
138 | }); |
---|