3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
17 * @class Ext.data.proxy.Proxy
19 * <p>Proxies are used by {@link Ext.data.Store Stores} to handle the loading and saving of {@link Ext.data.Model Model} data.
20 * Usually developers will not need to create or interact with proxies directly.</p>
21 * <p><u>Types of Proxy</u></p>
23 * <p>There are two main types of Proxy - {@link Ext.data.proxy.Client Client} and {@link Ext.data.proxy.Server Server}. The Client proxies
24 * save their data locally and include the following subclasses:</p>
26 * <ul style="list-style-type: disc; padding-left: 25px">
27 * <li>{@link Ext.data.proxy.LocalStorage LocalStorageProxy} - saves its data to localStorage if the browser supports it</li>
28 * <li>{@link Ext.data.proxy.SessionStorage SessionStorageProxy} - saves its data to sessionStorage if the browsers supports it</li>
29 * <li>{@link Ext.data.proxy.Memory MemoryProxy} - holds data in memory only, any data is lost when the page is refreshed</li>
32 * <p>The Server proxies save their data by sending requests to some remote server. These proxies include:</p>
34 * <ul style="list-style-type: disc; padding-left: 25px">
35 * <li>{@link Ext.data.proxy.Ajax Ajax} - sends requests to a server on the same domain</li>
36 * <li>{@link Ext.data.proxy.JsonP JsonP} - uses JSON-P to send requests to a server on a different domain</li>
37 * <li>{@link Ext.data.proxy.Direct Direct} - uses {@link Ext.direct} to send requests</li>
40 * <p>Proxies operate on the principle that all operations performed are either Create, Read, Update or Delete. These four operations
41 * are mapped to the methods {@link #create}, {@link #read}, {@link #update} and {@link #destroy} respectively. Each Proxy subclass
42 * implements these functions.</p>
44 * <p>The CRUD methods each expect an {@link Ext.data.Operation Operation} object as the sole argument. The Operation encapsulates
45 * information about the action the Store wishes to perform, the {@link Ext.data.Model model} instances that are to be modified, etc.
46 * See the {@link Ext.data.Operation Operation} documentation for more details. Each CRUD method also accepts a callback function to be
47 * called asynchronously on completion.</p>
49 * <p>Proxies also support batching of Operations via a {@link Ext.data.Batch batch} object, invoked by the {@link #batch} method.</p>
52 Ext.define('Ext.data.proxy.Proxy', {
54 alternateClassName: ['Ext.data.DataProxy', 'Ext.data.Proxy'],
56 'Ext.data.reader.Json',
57 'Ext.data.writer.Json'
65 observable: 'Ext.util.Observable'
69 * @cfg {String} batchOrder
70 * Comma-separated ordering 'create', 'update' and 'destroy' actions when batching. Override this
71 * to set a different order for the batched CRUD actions to be executed in. Defaults to 'create,update,destroy'
73 batchOrder: 'create,update,destroy',
76 * @cfg {Boolean} batchActions True to batch actions of a particular type when synchronizing the store.
77 * Defaults to <tt>true</tt>.
82 * @cfg {String} defaultReaderType The default registered reader type. Defaults to 'json'
85 defaultReaderType: 'json',
88 * @cfg {String} defaultWriterType The default registered writer type. Defaults to 'json'
91 defaultWriterType: 'json',
94 * @cfg {String/Ext.data.Model} model The name of the Model to tie to this Proxy. Can be either the string name of
95 * the Model, or a reference to the Model constructor. Required.
102 * @param {Object} config (optional) Config object.
104 constructor: function(config) {
105 config = config || {};
107 if (config.model === undefined) {
111 this.mixins.observable.constructor.call(this, config);
113 if (this.model !== undefined && !(this.model instanceof Ext.data.Model)) {
114 this.setModel(this.model);
119 * Sets the model associated with this proxy. This will only usually be called by a Store
120 * @param {String|Ext.data.Model} model The new model. Can be either the model name string,
121 * or a reference to the model's constructor
122 * @param {Boolean} setOnStore Sets the new model on the associated Store, if one is present
124 setModel: function(model, setOnStore) {
125 this.model = Ext.ModelManager.getModel(model);
127 var reader = this.reader,
128 writer = this.writer;
130 this.setReader(reader);
131 this.setWriter(writer);
133 if (setOnStore && this.store) {
134 this.store.setModel(this.model);
139 * Returns the model attached to this Proxy
140 * @return {Ext.data.Model} The model
142 getModel: function() {
147 * Sets the Proxy's Reader by string, config object or Reader instance
148 * @param {String|Object|Ext.data.reader.Reader} reader The new Reader, which can be either a type string, a configuration object
149 * or an Ext.data.reader.Reader instance
150 * @return {Ext.data.reader.Reader} The attached Reader object
152 setReader: function(reader) {
155 if (reader === undefined || typeof reader == 'string') {
161 if (reader.isReader) {
162 reader.setModel(me.model);
164 Ext.applyIf(reader, {
167 type : me.defaultReaderType
170 reader = Ext.createByAlias('reader.' + reader.type, reader);
178 * Returns the reader currently attached to this proxy instance
179 * @return {Ext.data.reader.Reader} The Reader instance
181 getReader: function() {
186 * Sets the Proxy's Writer by string, config object or Writer instance
187 * @param {String|Object|Ext.data.writer.Writer} writer The new Writer, which can be either a type string, a configuration object
188 * or an Ext.data.writer.Writer instance
189 * @return {Ext.data.writer.Writer} The attached Writer object
191 setWriter: function(writer) {
192 if (writer === undefined || typeof writer == 'string') {
198 if (!(writer instanceof Ext.data.writer.Writer)) {
199 Ext.applyIf(writer, {
201 type : this.defaultWriterType
204 writer = Ext.createByAlias('writer.' + writer.type, writer);
207 this.writer = writer;
213 * Returns the writer currently attached to this proxy instance
214 * @return {Ext.data.writer.Writer} The Writer instance
216 getWriter: function() {
221 * Performs the given create operation.
222 * @param {Ext.data.Operation} operation The Operation to perform
223 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
224 * @param {Object} scope Scope to execute the callback function in
230 * Performs the given read operation.
231 * @param {Ext.data.Operation} operation The Operation to perform
232 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
233 * @param {Object} scope Scope to execute the callback function in
239 * Performs the given update operation.
240 * @param {Ext.data.Operation} operation The Operation to perform
241 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
242 * @param {Object} scope Scope to execute the callback function in
248 * Performs the given destroy operation.
249 * @param {Ext.data.Operation} operation The Operation to perform
250 * @param {Function} callback Callback function to be called when the Operation has completed (whether successful or not)
251 * @param {Object} scope Scope to execute the callback function in
254 destroy: Ext.emptyFn,
257 * Performs a batch of {@link Ext.data.Operation Operations}, in the order specified by {@link #batchOrder}. Used internally by
258 * {@link Ext.data.Store}'s {@link Ext.data.Store#sync sync} method. Example usage:
261 * create : [myModel1, myModel2],
262 * update : [myModel3],
263 * destroy: [myModel4, myModel5]
266 * Where the myModel* above are {@link Ext.data.Model Model} instances - in this case 1 and 2 are new instances and have not been
267 * 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.
268 * @param {Object} operations Object containing the Model instances to act upon, keyed by action name
269 * @param {Object} listeners Optional listeners object passed straight through to the Batch - see {@link Ext.data.Batch}
270 * @return {Ext.data.Batch} The newly created Ext.data.Batch object
272 batch: function(operations, listeners) {
274 batch = Ext.create('Ext.data.Batch', {
276 listeners: listeners || {}
278 useBatch = me.batchActions,
281 Ext.each(me.batchOrder.split(','), function(action) {
282 records = operations[action];
285 batch.add(Ext.create('Ext.data.Operation', {
290 Ext.each(records, function(record){
291 batch.add(Ext.create('Ext.data.Operation', {
304 // Ext.data.proxy.ProxyMgr.registerType('proxy', this);
306 //backwards compatibility
307 Ext.data.DataProxy = this;
308 // Ext.deprecate('platform', '2.0', function() {
309 // Ext.data.DataProxy = this;