4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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'>/**
19 </span> * @author Ed Spencer
21 * Proxies are used by {@link Ext.data.Store Stores} to handle the loading and saving of {@link Ext.data.Model Model}
22 * data. Usually developers will not need to create or interact with proxies directly.
26 * There are two main types of Proxy - {@link Ext.data.proxy.Client Client} and {@link Ext.data.proxy.Server Server}.
27 * The Client proxies save their data locally and include the following subclasses:
29 * - {@link Ext.data.proxy.LocalStorage LocalStorageProxy} - saves its data to localStorage if the browser supports it
30 * - {@link Ext.data.proxy.SessionStorage SessionStorageProxy} - saves its data to sessionStorage if the browsers supports it
31 * - {@link Ext.data.proxy.Memory MemoryProxy} - holds data in memory only, any data is lost when the page is refreshed
33 * The Server proxies save their data by sending requests to some remote server. These proxies include:
35 * - {@link Ext.data.proxy.Ajax Ajax} - sends requests to a server on the same domain
36 * - {@link Ext.data.proxy.JsonP JsonP} - uses JSON-P to send requests to a server on a different domain
37 * - {@link Ext.data.proxy.Direct Direct} - uses {@link Ext.direct.Manager} to send requests
39 * Proxies operate on the principle that all operations performed are either Create, Read, Update or Delete. These four
40 * operations are mapped to the methods {@link #create}, {@link #read}, {@link #update} and {@link #destroy}
41 * respectively. Each Proxy subclass implements these functions.
43 * The CRUD methods each expect an {@link Ext.data.Operation Operation} object as the sole argument. The Operation
44 * encapsulates information about the action the Store wishes to perform, the {@link Ext.data.Model model} instances
45 * that are to be modified, etc. See the {@link Ext.data.Operation Operation} documentation for more details. Each CRUD
46 * method also accepts a callback function to be called asynchronously on completion.
48 * Proxies also support batching of Operations via a {@link Ext.data.Batch batch} object, invoked by the {@link #batch}
51 Ext.define('Ext.data.proxy.Proxy', {
53 alternateClassName: ['Ext.data.DataProxy', 'Ext.data.Proxy'],
55 'Ext.data.reader.Json',
56 'Ext.data.writer.Json'
64 observable: 'Ext.util.Observable'
67 <span id='Ext-data-proxy-Proxy-cfg-batchOrder'> /**
68 </span> * @cfg {String} batchOrder
69 * Comma-separated ordering 'create', 'update' and 'destroy' actions when batching. Override this to set a different
70 * order for the batched CRUD actions to be executed in. Defaults to 'create,update,destroy'.
72 batchOrder: 'create,update,destroy',
74 <span id='Ext-data-proxy-Proxy-cfg-batchActions'> /**
75 </span> * @cfg {Boolean} batchActions
76 * True to batch actions of a particular type when synchronizing the store. Defaults to true.
80 <span id='Ext-data-proxy-Proxy-cfg-defaultReaderType'> /**
81 </span> * @cfg {String} defaultReaderType
82 * The default registered reader type. Defaults to 'json'.
85 defaultReaderType: 'json',
87 <span id='Ext-data-proxy-Proxy-cfg-defaultWriterType'> /**
88 </span> * @cfg {String} defaultWriterType
89 * The default registered writer type. Defaults to 'json'.
92 defaultWriterType: 'json',
94 <span id='Ext-data-proxy-Proxy-cfg-model'> /**
95 </span> * @cfg {String/Ext.data.Model} model
96 * The name of the Model to tie to this Proxy. Can be either the string name of the Model, or a reference to the
97 * Model constructor. Required.
100 <span id='Ext-data-proxy-Proxy-cfg-reader'> /**
101 </span> * @cfg {Object/String/Ext.data.reader.Reader} reader
102 * The Ext.data.reader.Reader to use to decode the server's response or data read from client. This can either be a
103 * Reader instance, a config object or just a valid Reader type name (e.g. 'json', 'xml').
106 <span id='Ext-data-proxy-Proxy-cfg-writer'> /**
107 </span> * @cfg {Object/String/Ext.data.writer.Writer} writer
108 * The Ext.data.writer.Writer to use to encode any request sent to the server or saved to client. This can either be
109 * a Writer instance, a config object or just a valid Writer type name (e.g. 'json', 'xml').
114 <span id='Ext-data-proxy-Proxy-method-constructor'> /**
115 </span> * Creates the Proxy
116 * @param {Object} config (optional) Config object.
118 constructor: function(config) {
119 config = config || {};
121 if (config.model === undefined) {
125 this.mixins.observable.constructor.call(this, config);
127 if (this.model !== undefined && !(this.model instanceof Ext.data.Model)) {
128 this.setModel(this.model);
132 <span id='Ext-data-proxy-Proxy-method-setModel'> /**
133 </span> * Sets the model associated with this proxy. This will only usually be called by a Store
135 * @param {String/Ext.data.Model} model The new model. Can be either the model name string,
136 * or a reference to the model's constructor
137 * @param {Boolean} setOnStore Sets the new model on the associated Store, if one is present
139 setModel: function(model, setOnStore) {
140 this.model = Ext.ModelManager.getModel(model);
142 var reader = this.reader,
143 writer = this.writer;
145 this.setReader(reader);
146 this.setWriter(writer);
148 if (setOnStore && this.store) {
149 this.store.setModel(this.model);
153 <span id='Ext-data-proxy-Proxy-method-getModel'> /**
154 </span> * Returns the model attached to this Proxy
155 * @return {Ext.data.Model} The model
157 getModel: function() {
161 <span id='Ext-data-proxy-Proxy-method-setReader'> /**
162 </span> * Sets the Proxy's Reader by string, config object or Reader instance
164 * @param {String/Object/Ext.data.reader.Reader} reader The new Reader, which can be either a type string,
165 * a configuration object or an Ext.data.reader.Reader instance
166 * @return {Ext.data.reader.Reader} The attached Reader object
168 setReader: function(reader) {
171 if (reader === undefined || typeof reader == 'string') {
177 if (reader.isReader) {
178 reader.setModel(me.model);
180 Ext.applyIf(reader, {
183 type : me.defaultReaderType
186 reader = Ext.createByAlias('reader.' + reader.type, reader);
193 <span id='Ext-data-proxy-Proxy-method-getReader'> /**
194 </span> * Returns the reader currently attached to this proxy instance
195 * @return {Ext.data.reader.Reader} The Reader instance
197 getReader: function() {
201 <span id='Ext-data-proxy-Proxy-method-setWriter'> /**
202 </span> * Sets the Proxy's Writer by string, config object or Writer instance
204 * @param {String/Object/Ext.data.writer.Writer} writer The new Writer, which can be either a type string,
205 * a configuration object or an Ext.data.writer.Writer instance
206 * @return {Ext.data.writer.Writer} The attached Writer object
208 setWriter: function(writer) {
209 if (writer === undefined || typeof writer == 'string') {
215 if (!(writer instanceof Ext.data.writer.Writer)) {
216 Ext.applyIf(writer, {
218 type : this.defaultWriterType
221 writer = Ext.createByAlias('writer.' + writer.type, writer);
224 this.writer = writer;
229 <span id='Ext-data-proxy-Proxy-method-getWriter'> /**
230 </span> * Returns the writer currently attached to this proxy instance
231 * @return {Ext.data.writer.Writer} The Writer instance
233 getWriter: function() {
237 <span id='Ext-data-proxy-Proxy-method-create'> /**
238 </span> * Performs the given create operation.
239 * @param {Ext.data.Operation} operation The Operation to perform
240 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
241 * @param {Object} scope Scope to execute the callback function in
246 <span id='Ext-data-proxy-Proxy-method-read'> /**
247 </span> * Performs the given read operation.
248 * @param {Ext.data.Operation} operation The Operation to perform
249 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
250 * @param {Object} scope Scope to execute the callback function in
255 <span id='Ext-data-proxy-Proxy-method-update'> /**
256 </span> * Performs the given update operation.
257 * @param {Ext.data.Operation} operation The Operation to perform
258 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
259 * @param {Object} scope Scope to execute the callback function in
264 <span id='Ext-data-proxy-Proxy-method-destroy'> /**
265 </span> * Performs the given destroy operation.
266 * @param {Ext.data.Operation} operation The Operation to perform
267 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
268 * @param {Object} scope Scope to execute the callback function in
271 destroy: Ext.emptyFn,
273 <span id='Ext-data-proxy-Proxy-method-batch'> /**
274 </span> * Performs a batch of {@link Ext.data.Operation Operations}, in the order specified by {@link #batchOrder}. Used
275 * internally by {@link Ext.data.Store}'s {@link Ext.data.Store#sync sync} method. Example usage:
278 * create : [myModel1, myModel2],
279 * update : [myModel3],
280 * destroy: [myModel4, myModel5]
283 * Where the myModel* above are {@link Ext.data.Model Model} instances - in this case 1 and 2 are new instances and
284 * have not been saved before, 3 has been saved previously but needs to be updated, and 4 and 5 have already been
285 * saved but should now be destroyed.
287 * @param {Object} operations Object containing the Model instances to act upon, keyed by action name
288 * @param {Object} listeners (optional) listeners object passed straight through to the Batch -
289 * see {@link Ext.data.Batch}
290 * @return {Ext.data.Batch} The newly created Ext.data.Batch object
292 batch: function(operations, listeners) {
294 batch = Ext.create('Ext.data.Batch', {
296 listeners: listeners || {}
298 useBatch = me.batchActions,
301 Ext.each(me.batchOrder.split(','), function(action) {
302 records = operations[action];
305 batch.add(Ext.create('Ext.data.Operation', {
310 Ext.each(records, function(record){
311 batch.add(Ext.create('Ext.data.Operation', {
324 // Ext.data.proxy.ProxyMgr.registerType('proxy', this);
326 //backwards compatibility
327 Ext.data.DataProxy = this;
328 // Ext.deprecate('platform', '2.0', function() {
329 // Ext.data.DataProxy = this;