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-AbstractStore'>/**
19 </span> * @author Ed Spencer
20 * @class Ext.data.AbstractStore
22 * <p>AbstractStore is a superclass of {@link Ext.data.Store} and {@link Ext.data.TreeStore}. It's never used directly,
23 * but offers a set of methods used by both of those subclasses.</p>
25 * <p>We've left it here in the docs for reference purposes, but unless you need to make a whole new type of Store, what
26 * you're probably looking for is {@link Ext.data.Store}. If you're still interested, here's a brief description of what
27 * AbstractStore is and is not.</p>
29 * <p>AbstractStore provides the basic configuration for anything that can be considered a Store. It expects to be
30 * given a {@link Ext.data.Model Model} that represents the type of data in the Store. It also expects to be given a
31 * {@link Ext.data.proxy.Proxy Proxy} that handles the loading of data into the Store.</p>
33 * <p>AbstractStore provides a few helpful methods such as {@link #load} and {@link #sync}, which load and save data
34 * respectively, passing the requests through the configured {@link #proxy}. Both built-in Store subclasses add extra
35 * behavior to each of these functions. Note also that each AbstractStore subclass has its own way of storing data -
36 * in {@link Ext.data.Store} the data is saved as a flat {@link Ext.util.MixedCollection MixedCollection}, whereas in
37 * {@link Ext.data.TreeStore TreeStore} we use a {@link Ext.data.Tree} to maintain the data's hierarchy.</p>
39 * The store provides filtering and sorting support. This sorting/filtering can happen on the client side
40 * or can be completed on the server. This is controlled by the {@link #remoteSort} and (@link #remoteFilter{ config
41 * options. For more information see the {@link #sort} and {@link #filter} methods.
43 Ext.define('Ext.data.AbstractStore', {
44 requires: ['Ext.util.MixedCollection', 'Ext.data.Operation', 'Ext.util.Filter'],
47 observable: 'Ext.util.Observable',
48 sortable: 'Ext.util.Sortable'
52 create: function(store){
57 store = Ext.createByAlias('store.' + store.type, store);
66 <span id='Ext-data-AbstractStore-cfg-proxy'> /**
67 </span> * @cfg {String/Ext.data.proxy.Proxy/Object} proxy The Proxy to use for this Store. This can be either a string, a config
68 * object or a Proxy instance - see {@link #setProxy} for details.
71 <span id='Ext-data-AbstractStore-cfg-autoLoad'> /**
72 </span> * @cfg {Boolean/Object} autoLoad If data is not specified, and if autoLoad is true or an Object, this store's load method
73 * is automatically called after creation. If the value of autoLoad is an Object, this Object will be passed to the store's
74 * load method. Defaults to false.
78 <span id='Ext-data-AbstractStore-cfg-autoSync'> /**
79 </span> * @cfg {Boolean} autoSync True to automatically sync the Store with its Proxy after every edit to one of its Records.
84 <span id='Ext-data-AbstractStore-property-batchUpdateMode'> /**
85 </span> * Sets the updating behavior based on batch synchronization. 'operation' (the default) will update the Store's
86 * internal representation of the data after each operation of the batch has completed, 'complete' will wait until
87 * the entire batch has been completed before updating the Store's data. 'complete' is a good choice for local
88 * storage proxies, 'operation' is better for remote proxies, where there is a comparatively high latency.
89 * @property batchUpdateMode
92 batchUpdateMode: 'operation',
94 <span id='Ext-data-AbstractStore-property-filterOnLoad'> /**
95 </span> * If true, any filters attached to this Store will be run after loading data, before the datachanged event is fired.
96 * Defaults to true, ignored if {@link #remoteFilter} is true
97 * @property filterOnLoad
102 <span id='Ext-data-AbstractStore-property-sortOnLoad'> /**
103 </span> * If true, any sorters attached to this Store will be run after loading data, before the datachanged event is fired.
104 * Defaults to true, igored if {@link #remoteSort} is true
105 * @property sortOnLoad
110 <span id='Ext-data-AbstractStore-property-implicitModel'> /**
111 </span> * True if a model was created implicitly for this Store. This happens if a fields array is passed to the Store's constructor
112 * instead of a model constructor or name.
113 * @property implicitModel
117 implicitModel: false,
119 <span id='Ext-data-AbstractStore-property-defaultProxyType'> /**
120 </span> * The string type of the Proxy to create if none is specified. This defaults to creating a {@link Ext.data.proxy.Memory memory proxy}.
121 * @property defaultProxyType
124 defaultProxyType: 'memory',
126 <span id='Ext-data-AbstractStore-property-isDestroyed'> /**
127 </span> * True if the Store has already been destroyed via {@link #destroyStore}. If this is true, the reference to Store should be deleted
128 * as it will not function correctly any more.
129 * @property isDestroyed
136 <span id='Ext-data-AbstractStore-cfg-storeId'> /**
137 </span> * @cfg {String} storeId Optional unique identifier for this store. If present, this Store will be registered with
138 * the {@link Ext.data.StoreManager}, making it easy to reuse elsewhere. Defaults to undefined.
141 <span id='Ext-data-AbstractStore-cfg-fields'> /**
142 </span> * @cfg {Array} fields
143 * This may be used in place of specifying a {@link #model} configuration. The fields should be a
144 * set of {@link Ext.data.Field} configuration objects. The store will automatically create a {@link Ext.data.Model}
145 * with these fields. In general this configuration option should be avoided, it exists for the purposes of
146 * backwards compatibility. For anything more complicated, such as specifying a particular id property or
147 * assocations, a {@link Ext.data.Model} should be defined and specified for the {@link #model} config.
153 constructor: function(config) {
158 <span id='Ext-data-AbstractStore-event-add'> /**
160 * Fired when a Model instance has been added to this Store
161 * @param {Ext.data.Store} store The store
162 * @param {Array} records The Model instances that were added
163 * @param {Number} index The index at which the instances were inserted
167 <span id='Ext-data-AbstractStore-event-remove'> /**
168 </span> * @event remove
169 * Fired when a Model instance has been removed from this Store
170 * @param {Ext.data.Store} store The Store object
171 * @param {Ext.data.Model} record The record that was removed
172 * @param {Number} index The index of the record that was removed
176 <span id='Ext-data-AbstractStore-event-update'> /**
177 </span> * @event update
178 * Fires when a Record has been updated
179 * @param {Store} this
180 * @param {Ext.data.Model} record The Model instance that was updated
181 * @param {String} operation The update operation being performed. Value may be one of:
182 * <pre><code>
184 Ext.data.Model.REJECT
185 Ext.data.Model.COMMIT
186 * </code></pre>
190 <span id='Ext-data-AbstractStore-event-datachanged'> /**
191 </span> * @event datachanged
192 * Fires whenever the records in the Store have changed in some way - this could include adding or removing records,
193 * or updating the data in existing records
194 * @param {Ext.data.Store} this The data store
198 <span id='Ext-data-AbstractStore-event-beforeload'> /**
199 </span> * @event beforeload
201 * @param {Ext.data.Store} store This Store
202 * @param {Ext.data.Operation} operation The Ext.data.Operation object that will be passed to the Proxy to load the Store
206 <span id='Ext-data-AbstractStore-event-load'> /**
207 </span> * @event load
208 * Fires whenever the store reads data from a remote data source.
209 * @param {Ext.data.Store} this
210 * @param {Array} records An array of records
211 * @param {Boolean} successful True if the operation was successful.
215 <span id='Ext-data-AbstractStore-event-beforesync'> /**
216 </span> * @event beforesync
217 * Called before a call to {@link #sync} is executed. Return false from any listener to cancel the synv
218 * @param {Object} options Hash of all records to be synchronized, broken down into create, update and destroy
221 <span id='Ext-data-AbstractStore-event-clear'> /**
222 </span> * @event clear
223 * Fired after the {@link #removeAll} method is called.
224 * @param {Ext.data.Store} this
229 Ext.apply(me, config);
230 // don't use *config* anymore from here on... use *me* instead...
232 <span id='Ext-data-AbstractStore-property-removed'> /**
233 </span> * Temporary cache in which removed model instances are kept until successfully synchronised with a Proxy,
234 * at which point this is cleared.
241 me.mixins.observable.constructor.apply(me, arguments);
242 me.model = Ext.ModelManager.getModel(me.model);
244 <span id='Ext-data-AbstractStore-property-modelDefaults'> /**
245 </span> * @property modelDefaults
248 * A set of default values to be applied to every model instance added via {@link #insert} or created via {@link #create}.
249 * This is used internally by associations to set foreign keys and other fields. See the Association classes source code
250 * for examples. This should not need to be used by application developers.
256 //Supports the 3.x style of simply passing an array of fields to the store, implicitly creating a model
257 if (!me.model && me.fields) {
258 me.model = Ext.define('Ext.data.Store.ImplicitModel-' + (me.storeId || Ext.id()), {
259 extend: 'Ext.data.Model',
261 proxy: me.proxy || me.defaultProxyType
266 me.implicitModel = true;
269 //ensures that the Proxy is instantiated correctly
270 me.setProxy(me.proxy || me.model.getProxy());
272 if (me.id && !me.storeId) {
278 Ext.data.StoreManager.register(me);
281 me.mixins.sortable.initSortable.call(me);
283 <span id='Ext-data-AbstractStore-property-filters'> /**
284 </span> * The collection of {@link Ext.util.Filter Filters} currently applied to this Store
286 * @type Ext.util.MixedCollection
288 filters = me.decodeFilters(me.filters);
289 me.filters = Ext.create('Ext.util.MixedCollection');
290 me.filters.addAll(filters);
293 <span id='Ext-data-AbstractStore-method-setProxy'> /**
294 </span> * Sets the Store's Proxy by string, config object or Proxy instance
295 * @param {String|Object|Ext.data.proxy.Proxy} proxy The new Proxy, which can be either a type string, a configuration object
296 * or an Ext.data.proxy.Proxy instance
297 * @return {Ext.data.proxy.Proxy} The attached Proxy object
299 setProxy: function(proxy) {
302 if (proxy instanceof Ext.data.proxy.Proxy) {
303 proxy.setModel(me.model);
305 if (Ext.isString(proxy)) {
314 proxy = Ext.createByAlias('proxy.' + proxy.type, proxy);
322 <span id='Ext-data-AbstractStore-method-getProxy'> /**
323 </span> * Returns the proxy currently attached to this proxy instance
324 * @return {Ext.data.proxy.Proxy} The Proxy instance
326 getProxy: function() {
330 //saves any phantom records
331 create: function(data, options) {
333 instance = Ext.ModelManager.create(Ext.applyIf(data, me.modelDefaults), me.model.modelName),
336 options = options || {};
338 Ext.applyIf(options, {
343 operation = Ext.create('Ext.data.Operation', options);
345 me.proxy.create(operation, me.onProxyWrite, me);
351 return this.load.apply(this, arguments);
354 onProxyRead: Ext.emptyFn,
356 update: function(options) {
359 options = options || {};
361 Ext.applyIf(options, {
363 records: me.getUpdatedRecords()
366 operation = Ext.create('Ext.data.Operation', options);
368 return me.proxy.update(operation, me.onProxyWrite, me);
371 <span id='Ext-data-AbstractStore-method-onProxyWrite'> /**
373 * Callback for any write Operation over the Proxy. Updates the Store's MixedCollection to reflect
374 * the updates provided by the Proxy
376 onProxyWrite: function(operation) {
378 success = operation.wasSuccessful(),
379 records = operation.getRecords();
381 switch (operation.action) {
383 me.onCreateRecords(records, operation, success);
386 me.onUpdateRecords(records, operation, success);
389 me.onDestroyRecords(records, operation, success);
394 me.fireEvent('write', me, operation);
395 me.fireEvent('datachanged', me);
397 //this is a callback that would have been passed to the 'create', 'update' or 'destroy' function and is optional
398 Ext.callback(operation.callback, operation.scope || me, [records, operation, success]);
402 //tells the attached proxy to destroy the given records
403 destroy: function(options) {
407 options = options || {};
409 Ext.applyIf(options, {
411 records: me.getRemovedRecords()
414 operation = Ext.create('Ext.data.Operation', options);
416 return me.proxy.destroy(operation, me.onProxyWrite, me);
419 <span id='Ext-data-AbstractStore-method-onBatchOperationComplete'> /**
421 * Attached as the 'operationcomplete' event listener to a proxy's Batch object. By default just calls through
424 onBatchOperationComplete: function(batch, operation) {
425 return this.onProxyWrite(operation);
428 <span id='Ext-data-AbstractStore-method-onBatchComplete'> /**
430 * Attached as the 'complete' event listener to a proxy's Batch object. Iterates over the batch operations
431 * and updates the Store's internal data MixedCollection.
433 onBatchComplete: function(batch, operation) {
435 operations = batch.operations,
436 length = operations.length,
441 for (i = 0; i < length; i++) {
442 me.onProxyWrite(operations[i]);
447 me.fireEvent('datachanged', me);
450 onBatchException: function(batch, operation) {
451 // //decide what to do... could continue with the next operation
454 // //or retry the last operation
458 <span id='Ext-data-AbstractStore-method-filterNew'> /**
460 * Filter function for new records.
462 filterNew: function(item) {
463 // only want phantom records that are valid
464 return item.phantom === true && item.isValid();
467 <span id='Ext-data-AbstractStore-method-getNewRecords'> /**
468 </span> * Returns all Model instances that are either currently a phantom (e.g. have no id), or have an ID but have not
469 * yet been saved on this Store (this happens when adding a non-phantom record from another Store into this one)
470 * @return {Array} The Model instances
472 getNewRecords: function() {
476 <span id='Ext-data-AbstractStore-method-getUpdatedRecords'> /**
477 </span> * Returns all Model instances that have been updated in the Store but not yet synchronized with the Proxy
478 * @return {Array} The updated Model instances
480 getUpdatedRecords: function() {
484 <span id='Ext-data-AbstractStore-method-filterUpdated'> /**
486 * Filter function for updated records.
488 filterUpdated: function(item) {
489 // only want dirty records, not phantoms that are valid
490 return item.dirty === true && item.phantom !== true && item.isValid();
493 <span id='Ext-data-AbstractStore-method-getRemovedRecords'> /**
494 </span> * Returns any records that have been removed from the store but not yet destroyed on the proxy.
495 * @return {Array} The removed Model instances
497 getRemovedRecords: function() {
501 filter: function(filters, value) {
505 <span id='Ext-data-AbstractStore-method-decodeFilters'> /**
507 * Normalizes an array of filter objects, ensuring that they are all Ext.util.Filter instances
508 * @param {Array} filters The filters array
509 * @return {Array} Array of Ext.util.Filter objects
511 decodeFilters: function(filters) {
512 if (!Ext.isArray(filters)) {
513 if (filters === undefined) {
520 var length = filters.length,
521 Filter = Ext.util.Filter,
524 for (i = 0; i < length; i++) {
527 if (!(config instanceof Filter)) {
532 //support for 3.x style filters where a function can be defined as 'fn'
534 config.filterFn = config.fn;
537 //support a function to be passed as a filter definition
538 if (typeof config == 'function') {
544 filters[i] = new Filter(config);
551 clearFilter: function(supressEvent) {
555 isFiltered: function() {
559 filterBy: function(fn, scope) {
563 <span id='Ext-data-AbstractStore-method-sync'> /**
564 </span> * Synchronizes the Store with its Proxy. This asks the Proxy to batch together any new, updated
565 * and deleted records in the store, updating the Store's internal representation of the records
566 * as each operation completes.
571 toCreate = me.getNewRecords(),
572 toUpdate = me.getUpdatedRecords(),
573 toDestroy = me.getRemovedRecords(),
576 if (toCreate.length > 0) {
577 options.create = toCreate;
581 if (toUpdate.length > 0) {
582 options.update = toUpdate;
586 if (toDestroy.length > 0) {
587 options.destroy = toDestroy;
591 if (needsSync && me.fireEvent('beforesync', options) !== false) {
592 me.proxy.batch(options, me.getBatchListeners());
597 <span id='Ext-data-AbstractStore-method-getBatchListeners'> /**
599 * Returns an object which is passed in as the listeners argument to proxy.batch inside this.sync.
600 * This is broken out into a separate function to allow for customisation of the listeners
601 * @return {Object} The listeners object
603 getBatchListeners: function() {
607 exception: me.onBatchException
610 if (me.batchUpdateMode == 'operation') {
611 listeners.operationcomplete = me.onBatchOperationComplete;
613 listeners.complete = me.onBatchComplete;
619 //deprecated, will be removed in 5.0
621 return this.sync.apply(this, arguments);
624 <span id='Ext-data-AbstractStore-method-load'> /**
625 </span> * Loads the Store using its configured {@link #proxy}.
626 * @param {Object} options Optional config object. This is passed into the {@link Ext.data.Operation Operation}
627 * object that is created and then sent to the proxy's {@link Ext.data.proxy.Proxy#read} function
629 load: function(options) {
633 options = options || {};
635 Ext.applyIf(options, {
637 filters: me.filters.items,
638 sorters: me.getSorters()
641 operation = Ext.create('Ext.data.Operation', options);
643 if (me.fireEvent('beforeload', me, operation) !== false) {
645 me.proxy.read(operation, me.onProxyLoad, me);
651 <span id='Ext-data-AbstractStore-method-afterEdit'> /**
653 * A model instance should call this method on the Store it has been {@link Ext.data.Model#join joined} to.
654 * @param {Ext.data.Model} record The model instance that was edited
656 afterEdit : function(record) {
663 me.fireEvent('update', me, record, Ext.data.Model.EDIT);
666 <span id='Ext-data-AbstractStore-method-afterReject'> /**
668 * A model instance should call this method on the Store it has been {@link Ext.data.Model#join joined} to..
669 * @param {Ext.data.Model} record The model instance that was edited
671 afterReject : function(record) {
672 this.fireEvent('update', this, record, Ext.data.Model.REJECT);
675 <span id='Ext-data-AbstractStore-method-afterCommit'> /**
677 * A model instance should call this method on the Store it has been {@link Ext.data.Model#join joined} to.
678 * @param {Ext.data.Model} record The model instance that was edited
680 afterCommit : function(record) {
681 this.fireEvent('update', this, record, Ext.data.Model.COMMIT);
684 clearData: Ext.emptyFn,
686 destroyStore: function() {
689 if (!me.isDestroyed) {
691 Ext.data.StoreManager.unregister(me);
696 // Ext.destroy(this.proxy);
697 me.reader = me.writer = null;
699 me.isDestroyed = true;
701 if (me.implicitModel) {
702 Ext.destroy(me.model);
707 doSort: function(sorterFn) {
710 //the load function will pick up the new sorters and request the sorted data from the proxy
713 me.data.sortBy(sorterFn);
714 me.fireEvent('datachanged', me);
718 getCount: Ext.emptyFn,
720 getById: Ext.emptyFn,
722 <span id='Ext-data-AbstractStore-method-removeAll'> /**
723 </span> * Removes all records from the store. This method does a "fast remove",
724 * individual remove events are not called. The {@link #clear} event is
725 * fired upon completion.
728 removeAll: Ext.emptyFn,
729 // individual substores should implement a "fast" remove
730 // and fire a clear event afterwards
732 <span id='Ext-data-AbstractStore-method-isLoading'> /**
733 </span> * Returns true if the Store is currently performing a load operation
734 * @return {Boolean} True if the Store is currently loading
736 isLoading: function() {