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-AbstractStore'>/**
19 </span> * @author Ed Spencer
21 * AbstractStore is a superclass of {@link Ext.data.Store} and {@link Ext.data.TreeStore}. It's never used directly,
22 * but offers a set of methods used by both of those subclasses.
24 * We've left it here in the docs for reference purposes, but unless you need to make a whole new type of Store, what
25 * you're probably looking for is {@link Ext.data.Store}. If you're still interested, here's a brief description of what
26 * AbstractStore is and is not.
28 * AbstractStore provides the basic configuration for anything that can be considered a Store. It expects to be
29 * given a {@link Ext.data.Model Model} that represents the type of data in the Store. It also expects to be given a
30 * {@link Ext.data.proxy.Proxy Proxy} that handles the loading of data into the Store.
32 * AbstractStore provides a few helpful methods such as {@link #load} and {@link #sync}, which load and save data
33 * respectively, passing the requests through the configured {@link #proxy}. Both built-in Store subclasses add extra
34 * behavior to each of these functions. Note also that each AbstractStore subclass has its own way of storing data -
35 * in {@link Ext.data.Store} the data is saved as a flat {@link Ext.util.MixedCollection MixedCollection}, whereas in
36 * {@link Ext.data.TreeStore TreeStore} we use a {@link Ext.data.Tree} to maintain the data's hierarchy.
38 * The store provides filtering and sorting support. This sorting/filtering can happen on the client side
39 * or can be completed on the server. This is controlled by the {@link Ext.data.Store#remoteSort remoteSort} and
40 * {@link Ext.data.Store#remoteFilter remoteFilter} config options. For more information see the {@link #sort} and
41 * {@link Ext.data.Store#filter 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
68 * The Proxy to use for this Store. This can be either a string, a config object or a Proxy instance -
69 * see {@link #setProxy} for details.
72 <span id='Ext-data-AbstractStore-cfg-autoLoad'> /**
73 </span> * @cfg {Boolean/Object} autoLoad
74 * If data is not specified, and if autoLoad is true or an Object, this store's load method is automatically called
75 * after creation. If the value of autoLoad is an Object, this Object will be passed to the store's load method.
80 <span id='Ext-data-AbstractStore-cfg-autoSync'> /**
81 </span> * @cfg {Boolean} autoSync
82 * True to automatically sync the Store with its Proxy after every edit to one of its Records. Defaults to false.
86 <span id='Ext-data-AbstractStore-property-batchUpdateMode'> /**
87 </span> * @property {String} batchUpdateMode
88 * Sets the updating behavior based on batch synchronization. 'operation' (the default) will update the Store's
89 * internal representation of the data after each operation of the batch has completed, 'complete' will wait until
90 * the entire batch has been completed before updating the Store's data. 'complete' is a good choice for local
91 * storage proxies, 'operation' is better for remote proxies, where there is a comparatively high latency.
93 batchUpdateMode: 'operation',
95 <span id='Ext-data-AbstractStore-property-filterOnLoad'> /**
96 </span> * @property {Boolean} filterOnLoad
97 * If true, any filters attached to this Store will be run after loading data, before the datachanged event is fired.
98 * Defaults to true, ignored if {@link Ext.data.Store#remoteFilter remoteFilter} is true
102 <span id='Ext-data-AbstractStore-property-sortOnLoad'> /**
103 </span> * @property {Boolean} sortOnLoad
104 * If true, any sorters attached to this Store will be run after loading data, before the datachanged event is fired.
105 * Defaults to true, igored if {@link Ext.data.Store#remoteSort remoteSort} is true
109 <span id='Ext-data-AbstractStore-property-implicitModel'> /**
110 </span> * @property {Boolean} implicitModel
111 * True if a model was created implicitly for this Store. This happens if a fields array is passed to the Store's
112 * constructor instead of a model constructor or name.
115 implicitModel: false,
117 <span id='Ext-data-AbstractStore-property-defaultProxyType'> /**
118 </span> * @property {String} defaultProxyType
119 * The string type of the Proxy to create if none is specified. This defaults to creating a
120 * {@link Ext.data.proxy.Memory memory proxy}.
122 defaultProxyType: 'memory',
124 <span id='Ext-data-AbstractStore-property-isDestroyed'> /**
125 </span> * @property {Boolean} isDestroyed
126 * True if the Store has already been destroyed. If this is true, the reference to Store should be deleted
127 * as it will not function correctly any more.
133 <span id='Ext-data-AbstractStore-cfg-storeId'> /**
134 </span> * @cfg {String} storeId
135 * Unique identifier for this store. If present, this Store will be registered with the {@link Ext.data.StoreManager},
136 * making it easy to reuse elsewhere. Defaults to undefined.
139 <span id='Ext-data-AbstractStore-cfg-fields'> /**
140 </span> * @cfg {Object[]} fields
141 * This may be used in place of specifying a {@link #model} configuration. The fields should be a
142 * set of {@link Ext.data.Field} configuration objects. The store will automatically create a {@link Ext.data.Model}
143 * with these fields. In general this configuration option should be avoided, it exists for the purposes of
144 * backwards compatibility. For anything more complicated, such as specifying a particular id property or
145 * assocations, a {@link Ext.data.Model} should be defined and specified for the {@link #model}
149 <span id='Ext-data-AbstractStore-cfg-model'> /**
150 </span> * @cfg {String} model
151 * Name of the {@link Ext.data.Model Model} associated with this store.
152 * The string is used as an argument for {@link Ext.ModelManager#getModel}.
158 constructor: function(config) {
163 <span id='Ext-data-AbstractStore-event-add'> /**
165 * Fired when a Model instance has been added to this Store
166 * @param {Ext.data.Store} store The store
167 * @param {Ext.data.Model[]} records The Model instances that were added
168 * @param {Number} index The index at which the instances were inserted
172 <span id='Ext-data-AbstractStore-event-remove'> /**
173 </span> * @event remove
174 * Fired when a Model instance has been removed from this Store
175 * @param {Ext.data.Store} store The Store object
176 * @param {Ext.data.Model} record The record that was removed
177 * @param {Number} index The index of the record that was removed
181 <span id='Ext-data-AbstractStore-event-update'> /**
182 </span> * @event update
183 * Fires when a Model instance has been updated
184 * @param {Ext.data.Store} this
185 * @param {Ext.data.Model} record The Model instance that was updated
186 * @param {String} operation The update operation being performed. Value may be one of:
188 * Ext.data.Model.EDIT
189 * Ext.data.Model.REJECT
190 * Ext.data.Model.COMMIT
194 <span id='Ext-data-AbstractStore-event-datachanged'> /**
195 </span> * @event datachanged
196 * Fires whenever the records in the Store have changed in some way - this could include adding or removing
197 * records, or updating the data in existing records
198 * @param {Ext.data.Store} this The data store
202 <span id='Ext-data-AbstractStore-event-beforeload'> /**
203 </span> * @event beforeload
204 * Fires before a request is made for a new data object. If the beforeload handler returns false the load
205 * action will be canceled.
206 * @param {Ext.data.Store} store This Store
207 * @param {Ext.data.Operation} operation The Ext.data.Operation object that will be passed to the Proxy to
212 <span id='Ext-data-AbstractStore-event-load'> /**
213 </span> * @event load
214 * Fires whenever the store reads data from a remote data source.
215 * @param {Ext.data.Store} this
216 * @param {Ext.data.Model[]} records An array of records
217 * @param {Boolean} successful True if the operation was successful.
221 <span id='Ext-data-AbstractStore-event-write'> /**
222 </span> * @event write
223 * Fires whenever a successful write has been made via the configured {@link #proxy Proxy}
224 * @param {Ext.data.Store} store This Store
225 * @param {Ext.data.Operation} operation The {@link Ext.data.Operation Operation} object that was used in
230 <span id='Ext-data-AbstractStore-event-beforesync'> /**
231 </span> * @event beforesync
232 * Fired before a call to {@link #sync} is executed. Return false from any listener to cancel the synv
233 * @param {Object} options Hash of all records to be synchronized, broken down into create, update and destroy
236 <span id='Ext-data-AbstractStore-event-clear'> /**
237 </span> * @event clear
238 * Fired after the {@link #removeAll} method is called.
239 * @param {Ext.data.Store} this
244 Ext.apply(me, config);
245 // don't use *config* anymore from here on... use *me* instead...
247 <span id='Ext-data-AbstractStore-property-removed'> /**
248 </span> * Temporary cache in which removed model instances are kept until successfully synchronised with a Proxy,
249 * at which point this is cleared.
251 * @property {Ext.data.Model[]} removed
255 me.mixins.observable.constructor.apply(me, arguments);
256 me.model = Ext.ModelManager.getModel(me.model);
258 <span id='Ext-data-AbstractStore-property-modelDefaults'> /**
259 </span> * @property {Object} modelDefaults
261 * A set of default values to be applied to every model instance added via {@link #insert} or created via {@link #create}.
262 * This is used internally by associations to set foreign keys and other fields. See the Association classes source code
263 * for examples. This should not need to be used by application developers.
269 //Supports the 3.x style of simply passing an array of fields to the store, implicitly creating a model
270 if (!me.model && me.fields) {
271 me.model = Ext.define('Ext.data.Store.ImplicitModel-' + (me.storeId || Ext.id()), {
272 extend: 'Ext.data.Model',
274 proxy: me.proxy || me.defaultProxyType
279 me.implicitModel = true;
284 if (Ext.isDefined(Ext.global.console)) {
285 Ext.global.console.warn('Store defined with no model. You may have mistyped the model name.');
290 //ensures that the Proxy is instantiated correctly
291 me.setProxy(me.proxy || me.model.getProxy());
293 if (me.id && !me.storeId) {
299 Ext.data.StoreManager.register(me);
302 me.mixins.sortable.initSortable.call(me);
304 <span id='Ext-data-AbstractStore-property-filters'> /**
305 </span> * @property {Ext.util.MixedCollection} filters
306 * The collection of {@link Ext.util.Filter Filters} currently applied to this Store
308 filters = me.decodeFilters(me.filters);
309 me.filters = Ext.create('Ext.util.MixedCollection');
310 me.filters.addAll(filters);
313 <span id='Ext-data-AbstractStore-method-setProxy'> /**
314 </span> * Sets the Store's Proxy by string, config object or Proxy instance
315 * @param {String/Object/Ext.data.proxy.Proxy} proxy The new Proxy, which can be either a type string, a configuration object
316 * or an Ext.data.proxy.Proxy instance
317 * @return {Ext.data.proxy.Proxy} The attached Proxy object
319 setProxy: function(proxy) {
322 if (proxy instanceof Ext.data.proxy.Proxy) {
323 proxy.setModel(me.model);
325 if (Ext.isString(proxy)) {
334 proxy = Ext.createByAlias('proxy.' + proxy.type, proxy);
342 <span id='Ext-data-AbstractStore-method-getProxy'> /**
343 </span> * Returns the proxy currently attached to this proxy instance
344 * @return {Ext.data.proxy.Proxy} The Proxy instance
346 getProxy: function() {
350 //saves any phantom records
351 create: function(data, options) {
353 instance = Ext.ModelManager.create(Ext.applyIf(data, me.modelDefaults), me.model.modelName),
356 options = options || {};
358 Ext.applyIf(options, {
363 operation = Ext.create('Ext.data.Operation', options);
365 me.proxy.create(operation, me.onProxyWrite, me);
371 return this.load.apply(this, arguments);
374 onProxyRead: Ext.emptyFn,
376 update: function(options) {
379 options = options || {};
381 Ext.applyIf(options, {
383 records: me.getUpdatedRecords()
386 operation = Ext.create('Ext.data.Operation', options);
388 return me.proxy.update(operation, me.onProxyWrite, me);
391 <span id='Ext-data-AbstractStore-method-onProxyWrite'> /**
393 * Callback for any write Operation over the Proxy. Updates the Store's MixedCollection to reflect
394 * the updates provided by the Proxy
396 onProxyWrite: function(operation) {
398 success = operation.wasSuccessful(),
399 records = operation.getRecords();
401 switch (operation.action) {
403 me.onCreateRecords(records, operation, success);
406 me.onUpdateRecords(records, operation, success);
409 me.onDestroyRecords(records, operation, success);
414 me.fireEvent('write', me, operation);
415 me.fireEvent('datachanged', me);
417 //this is a callback that would have been passed to the 'create', 'update' or 'destroy' function and is optional
418 Ext.callback(operation.callback, operation.scope || me, [records, operation, success]);
422 //tells the attached proxy to destroy the given records
423 destroy: function(options) {
427 options = options || {};
429 Ext.applyIf(options, {
431 records: me.getRemovedRecords()
434 operation = Ext.create('Ext.data.Operation', options);
436 return me.proxy.destroy(operation, me.onProxyWrite, me);
439 <span id='Ext-data-AbstractStore-method-onBatchOperationComplete'> /**
441 * Attached as the 'operationcomplete' event listener to a proxy's Batch object. By default just calls through
444 onBatchOperationComplete: function(batch, operation) {
445 return this.onProxyWrite(operation);
448 <span id='Ext-data-AbstractStore-method-onBatchComplete'> /**
450 * Attached as the 'complete' event listener to a proxy's Batch object. Iterates over the batch operations
451 * and updates the Store's internal data MixedCollection.
453 onBatchComplete: function(batch, operation) {
455 operations = batch.operations,
456 length = operations.length,
461 for (i = 0; i < length; i++) {
462 me.onProxyWrite(operations[i]);
467 me.fireEvent('datachanged', me);
470 onBatchException: function(batch, operation) {
471 // //decide what to do... could continue with the next operation
474 // //or retry the last operation
478 <span id='Ext-data-AbstractStore-method-filterNew'> /**
480 * Filter function for new records.
482 filterNew: function(item) {
483 // only want phantom records that are valid
484 return item.phantom === true && item.isValid();
487 <span id='Ext-data-AbstractStore-method-getNewRecords'> /**
488 </span> * Returns all Model instances that are either currently a phantom (e.g. have no id), or have an ID but have not
489 * yet been saved on this Store (this happens when adding a non-phantom record from another Store into this one)
490 * @return {Ext.data.Model[]} The Model instances
492 getNewRecords: function() {
496 <span id='Ext-data-AbstractStore-method-getUpdatedRecords'> /**
497 </span> * Returns all Model instances that have been updated in the Store but not yet synchronized with the Proxy
498 * @return {Ext.data.Model[]} The updated Model instances
500 getUpdatedRecords: function() {
504 <span id='Ext-data-AbstractStore-method-filterUpdated'> /**
506 * Filter function for updated records.
508 filterUpdated: function(item) {
509 // only want dirty records, not phantoms that are valid
510 return item.dirty === true && item.phantom !== true && item.isValid();
513 <span id='Ext-data-AbstractStore-method-getRemovedRecords'> /**
514 </span> * Returns any records that have been removed from the store but not yet destroyed on the proxy.
515 * @return {Ext.data.Model[]} The removed Model instances
517 getRemovedRecords: function() {
521 filter: function(filters, value) {
525 <span id='Ext-data-AbstractStore-method-decodeFilters'> /**
527 * Normalizes an array of filter objects, ensuring that they are all Ext.util.Filter instances
528 * @param {Object[]} filters The filters array
529 * @return {Ext.util.Filter[]} Array of Ext.util.Filter objects
531 decodeFilters: function(filters) {
532 if (!Ext.isArray(filters)) {
533 if (filters === undefined) {
540 var length = filters.length,
541 Filter = Ext.util.Filter,
544 for (i = 0; i < length; i++) {
547 if (!(config instanceof Filter)) {
552 //support for 3.x style filters where a function can be defined as 'fn'
554 config.filterFn = config.fn;
557 //support a function to be passed as a filter definition
558 if (typeof config == 'function') {
564 filters[i] = new Filter(config);
571 clearFilter: function(supressEvent) {
575 isFiltered: function() {
579 filterBy: function(fn, scope) {
583 <span id='Ext-data-AbstractStore-method-sync'> /**
584 </span> * Synchronizes the Store with its Proxy. This asks the Proxy to batch together any new, updated
585 * and deleted records in the store, updating the Store's internal representation of the records
586 * as each operation completes.
591 toCreate = me.getNewRecords(),
592 toUpdate = me.getUpdatedRecords(),
593 toDestroy = me.getRemovedRecords(),
596 if (toCreate.length > 0) {
597 options.create = toCreate;
601 if (toUpdate.length > 0) {
602 options.update = toUpdate;
606 if (toDestroy.length > 0) {
607 options.destroy = toDestroy;
611 if (needsSync && me.fireEvent('beforesync', options) !== false) {
612 me.proxy.batch(options, me.getBatchListeners());
617 <span id='Ext-data-AbstractStore-method-getBatchListeners'> /**
619 * Returns an object which is passed in as the listeners argument to proxy.batch inside this.sync.
620 * This is broken out into a separate function to allow for customisation of the listeners
621 * @return {Object} The listeners object
623 getBatchListeners: function() {
627 exception: me.onBatchException
630 if (me.batchUpdateMode == 'operation') {
631 listeners.operationcomplete = me.onBatchOperationComplete;
633 listeners.complete = me.onBatchComplete;
639 //deprecated, will be removed in 5.0
641 return this.sync.apply(this, arguments);
644 <span id='Ext-data-AbstractStore-method-load'> /**
645 </span> * Loads the Store using its configured {@link #proxy}.
646 * @param {Object} options (optional) config object. This is passed into the {@link Ext.data.Operation Operation}
647 * object that is created and then sent to the proxy's {@link Ext.data.proxy.Proxy#read} function
649 load: function(options) {
653 options = options || {};
655 Ext.applyIf(options, {
657 filters: me.filters.items,
658 sorters: me.getSorters()
661 operation = Ext.create('Ext.data.Operation', options);
663 if (me.fireEvent('beforeload', me, operation) !== false) {
665 me.proxy.read(operation, me.onProxyLoad, me);
671 <span id='Ext-data-AbstractStore-method-afterEdit'> /**
673 * A model instance should call this method on the Store it has been {@link Ext.data.Model#join joined} to.
674 * @param {Ext.data.Model} record The model instance that was edited
676 afterEdit : function(record) {
683 me.fireEvent('update', me, record, Ext.data.Model.EDIT);
686 <span id='Ext-data-AbstractStore-method-afterReject'> /**
688 * A model instance should call this method on the Store it has been {@link Ext.data.Model#join joined} to..
689 * @param {Ext.data.Model} record The model instance that was edited
691 afterReject : function(record) {
692 this.fireEvent('update', this, record, Ext.data.Model.REJECT);
695 <span id='Ext-data-AbstractStore-method-afterCommit'> /**
697 * A model instance should call this method on the Store it has been {@link Ext.data.Model#join joined} to.
698 * @param {Ext.data.Model} record The model instance that was edited
700 afterCommit : function(record) {
701 this.fireEvent('update', this, record, Ext.data.Model.COMMIT);
704 clearData: Ext.emptyFn,
706 destroyStore: function() {
709 if (!me.isDestroyed) {
711 Ext.data.StoreManager.unregister(me);
716 // Ext.destroy(this.proxy);
717 me.reader = me.writer = null;
719 me.isDestroyed = true;
721 if (me.implicitModel) {
722 Ext.destroy(me.model);
727 doSort: function(sorterFn) {
730 //the load function will pick up the new sorters and request the sorted data from the proxy
733 me.data.sortBy(sorterFn);
734 me.fireEvent('datachanged', me);
738 getCount: Ext.emptyFn,
740 getById: Ext.emptyFn,
742 <span id='Ext-data-AbstractStore-method-removeAll'> /**
743 </span> * Removes all records from the store. This method does a "fast remove",
744 * individual remove events are not called. The {@link #clear} event is
745 * fired upon completion.
748 removeAll: Ext.emptyFn,
749 // individual substores should implement a "fast" remove
750 // and fire a clear event afterwards
752 <span id='Ext-data-AbstractStore-method-isLoading'> /**
753 </span> * Returns true if the Store is currently performing a load operation
754 * @return {Boolean} True if the Store is currently loading
756 isLoading: function() {
757 return !!this.loading;