4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-proxy-Proxy-method-constructor'><span id='Ext-data-proxy-Proxy'>/**
19 </span></span> * @author Ed Spencer
20 * @class Ext.data.proxy.Proxy
22 * <p>Proxies are used by {@link Ext.data.Store Stores} to handle the loading and saving of {@link Ext.data.Model Model} data.
23 * Usually developers will not need to create or interact with proxies directly.</p>
24 * <p><u>Types of Proxy</u></p>
26 * <p>There are two main types of Proxy - {@link Ext.data.proxy.Client Client} and {@link Ext.data.proxy.Server Server}. The Client proxies
27 * save their data locally and include the following subclasses:</p>
29 * <ul style="list-style-type: disc; padding-left: 25px">
30 * <li>{@link Ext.data.proxy.LocalStorage LocalStorageProxy} - saves its data to localStorage if the browser supports it</li>
31 * <li>{@link Ext.data.proxy.SessionStorage SessionStorageProxy} - saves its data to sessionStorage if the browsers supports it</li>
32 * <li>{@link Ext.data.proxy.Memory MemoryProxy} - holds data in memory only, any data is lost when the page is refreshed</li>
35 * <p>The Server proxies save their data by sending requests to some remote server. These proxies include:</p>
37 * <ul style="list-style-type: disc; padding-left: 25px">
38 * <li>{@link Ext.data.proxy.Ajax Ajax} - sends requests to a server on the same domain</li>
39 * <li>{@link Ext.data.proxy.JsonP JsonP} - uses JSON-P to send requests to a server on a different domain</li>
40 * <li>{@link Ext.data.proxy.Direct Direct} - uses {@link Ext.direct} to send requests</li>
43 * <p>Proxies operate on the principle that all operations performed are either Create, Read, Update or Delete. These four operations
44 * are mapped to the methods {@link #create}, {@link #read}, {@link #update} and {@link #destroy} respectively. Each Proxy subclass
45 * implements these functions.</p>
47 * <p>The CRUD methods each expect an {@link Ext.data.Operation Operation} object as the sole argument. The Operation encapsulates
48 * information about the action the Store wishes to perform, the {@link Ext.data.Model model} instances that are to be modified, etc.
49 * See the {@link Ext.data.Operation Operation} documentation for more details. Each CRUD method also accepts a callback function to be
50 * called asynchronously on completion.</p>
52 * <p>Proxies also support batching of Operations via a {@link Ext.data.Batch batch} object, invoked by the {@link #batch} method.</p>
56 * @param {Object} config Optional config object
58 Ext.define('Ext.data.proxy.Proxy', {
60 alternateClassName: ['Ext.data.DataProxy', 'Ext.data.Proxy'],
62 'Ext.data.reader.Json',
63 'Ext.data.writer.Json'
71 observable: 'Ext.util.Observable'
74 <span id='Ext-data-proxy-Proxy-cfg-batchOrder'> /**
75 </span> * @cfg {String} batchOrder
76 * Comma-separated ordering 'create', 'update' and 'destroy' actions when batching. Override this
77 * to set a different order for the batched CRUD actions to be executed in. Defaults to 'create,update,destroy'
79 batchOrder: 'create,update,destroy',
81 <span id='Ext-data-proxy-Proxy-cfg-batchActions'> /**
82 </span> * @cfg {Boolean} batchActions True to batch actions of a particular type when synchronizing the store.
83 * Defaults to <tt>true</tt>.
87 <span id='Ext-data-proxy-Proxy-cfg-defaultReaderType'> /**
88 </span> * @cfg {String} defaultReaderType The default registered reader type. Defaults to 'json'
91 defaultReaderType: 'json',
93 <span id='Ext-data-proxy-Proxy-cfg-defaultWriterType'> /**
94 </span> * @cfg {String} defaultWriterType The default registered writer type. Defaults to 'json'
97 defaultWriterType: 'json',
99 <span id='Ext-data-proxy-Proxy-cfg-model'> /**
100 </span> * @cfg {String/Ext.data.Model} model The name of the Model to tie to this Proxy. Can be either the string name of
101 * the Model, or a reference to the Model constructor. Required.
106 constructor: function(config) {
107 config = config || {};
109 if (config.model === undefined) {
113 this.mixins.observable.constructor.call(this, config);
115 if (this.model !== undefined && !(this.model instanceof Ext.data.Model)) {
116 this.setModel(this.model);
120 <span id='Ext-data-proxy-Proxy-method-setModel'> /**
121 </span> * Sets the model associated with this proxy. This will only usually be called by a Store
122 * @param {String|Ext.data.Model} model The new model. Can be either the model name string,
123 * or a reference to the model's constructor
124 * @param {Boolean} setOnStore Sets the new model on the associated Store, if one is present
126 setModel: function(model, setOnStore) {
127 this.model = Ext.ModelManager.getModel(model);
129 var reader = this.reader,
130 writer = this.writer;
132 this.setReader(reader);
133 this.setWriter(writer);
135 if (setOnStore && this.store) {
136 this.store.setModel(this.model);
140 <span id='Ext-data-proxy-Proxy-method-getModel'> /**
141 </span> * Returns the model attached to this Proxy
142 * @return {Ext.data.Model} The model
144 getModel: function() {
148 <span id='Ext-data-proxy-Proxy-method-setReader'> /**
149 </span> * Sets the Proxy's Reader by string, config object or Reader instance
150 * @param {String|Object|Ext.data.reader.Reader} reader The new Reader, which can be either a type string, a configuration object
151 * or an Ext.data.reader.Reader instance
152 * @return {Ext.data.reader.Reader} The attached Reader object
154 setReader: function(reader) {
157 if (reader === undefined || typeof reader == 'string') {
163 if (reader.isReader) {
164 reader.setModel(me.model);
166 Ext.applyIf(reader, {
169 type : me.defaultReaderType
172 reader = Ext.createByAlias('reader.' + reader.type, reader);
179 <span id='Ext-data-proxy-Proxy-method-getReader'> /**
180 </span> * Returns the reader currently attached to this proxy instance
181 * @return {Ext.data.reader.Reader} The Reader instance
183 getReader: function() {
187 <span id='Ext-data-proxy-Proxy-method-setWriter'> /**
188 </span> * Sets the Proxy's Writer by string, config object or Writer instance
189 * @param {String|Object|Ext.data.writer.Writer} writer The new Writer, which can be either a type string, a configuration object
190 * or an Ext.data.writer.Writer instance
191 * @return {Ext.data.writer.Writer} The attached Writer object
193 setWriter: function(writer) {
194 if (writer === undefined || typeof writer == 'string') {
200 if (!(writer instanceof Ext.data.writer.Writer)) {
201 Ext.applyIf(writer, {
203 type : this.defaultWriterType
206 writer = Ext.createByAlias('writer.' + writer.type, writer);
209 this.writer = writer;
214 <span id='Ext-data-proxy-Proxy-method-getWriter'> /**
215 </span> * Returns the writer currently attached to this proxy instance
216 * @return {Ext.data.writer.Writer} The Writer instance
218 getWriter: function() {
222 <span id='Ext-data-proxy-Proxy-method-create'> /**
223 </span> * Performs the given create operation.
224 * @param {Ext.data.Operation} operation The Operation to perform
225 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
226 * @param {Object} scope Scope to execute the callback function in
231 <span id='Ext-data-proxy-Proxy-method-read'> /**
232 </span> * Performs the given read operation.
233 * @param {Ext.data.Operation} operation The Operation to perform
234 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
235 * @param {Object} scope Scope to execute the callback function in
240 <span id='Ext-data-proxy-Proxy-method-update'> /**
241 </span> * Performs the given update operation.
242 * @param {Ext.data.Operation} operation The Operation to perform
243 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
244 * @param {Object} scope Scope to execute the callback function in
249 <span id='Ext-data-proxy-Proxy-method-destroy'> /**
250 </span> * Performs the given destroy operation.
251 * @param {Ext.data.Operation} operation The Operation to perform
252 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
253 * @param {Object} scope Scope to execute the callback function in
256 destroy: Ext.emptyFn,
258 <span id='Ext-data-proxy-Proxy-method-batch'> /**
259 </span> * Performs a batch of {@link Ext.data.Operation Operations}, in the order specified by {@link #batchOrder}. Used internally by
260 * {@link Ext.data.Store}'s {@link Ext.data.Store#sync sync} method. Example usage:
261 * <pre><code>
263 * create : [myModel1, myModel2],
264 * update : [myModel3],
265 * destroy: [myModel4, myModel5]
267 * </code></pre>
268 * Where the myModel* above are {@link Ext.data.Model Model} instances - in this case 1 and 2 are new instances and have not been
269 * saved before, 3 has been saved previously but needs to be updated, and 4 and 5 have already been saved but should now be destroyed.
270 * @param {Object} operations Object containing the Model instances to act upon, keyed by action name
271 * @param {Object} listeners Optional listeners object passed straight through to the Batch - see {@link Ext.data.Batch}
272 * @return {Ext.data.Batch} The newly created Ext.data.Batch object
274 batch: function(operations, listeners) {
276 batch = Ext.create('Ext.data.Batch', {
278 listeners: listeners || {}
280 useBatch = me.batchActions,
283 Ext.each(me.batchOrder.split(','), function(action) {
284 records = operations[action];
287 batch.add(Ext.create('Ext.data.Operation', {
292 Ext.each(records, function(record){
293 batch.add(Ext.create('Ext.data.Operation', {
306 // Ext.data.proxy.ProxyMgr.registerType('proxy', this);
308 //backwards compatibility
309 Ext.data.DataProxy = this;
310 // Ext.deprecate('platform', '2.0', function() {
311 // Ext.data.DataProxy = this;