1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.Store-method-constructor'><span id='Ext-data.Store'>/**
2 </span></span> * @author Ed Spencer
3 * @class Ext.data.Store
4 * @extends Ext.data.AbstractStore
6 * <p>The Store class encapsulates a client side cache of {@link Ext.data.Model Model} objects. Stores load
7 * data via a {@link Ext.data.proxy.Proxy Proxy}, and also provide functions for {@link #sort sorting},
8 * {@link #filter filtering} and querying the {@link Ext.data.Model model} instances contained within it.</p>
10 * <p>Creating a Store is easy - we just tell it the Model and the Proxy to use to load and save its data:</p>
12 <pre><code>
13 // Set up a {@link Ext.data.Model model} to use in our Store
15 extend: 'Ext.data.Model',
17 {name: 'firstName', type: 'string'},
18 {name: 'lastName', type: 'string'},
19 {name: 'age', type: 'int'},
20 {name: 'eyeColor', type: 'string'}
24 var myStore = new Ext.data.Store({
36 </code></pre>
38 * <p>In the example above we configured an AJAX proxy to load data from the url '/users.json'. We told our Proxy
39 * to use a {@link Ext.data.reader.Json JsonReader} to parse the response from the server into Model object -
40 * {@link Ext.data.reader.Json see the docs on JsonReader} for details.</p>
42 * <p><u>Inline data</u></p>
44 * <p>Stores can also load data inline. Internally, Store converts each of the objects we pass in as {@link #data}
45 * into Model instances:</p>
47 <pre><code>
51 {firstName: 'Ed', lastName: 'Spencer'},
52 {firstName: 'Tommy', lastName: 'Maintz'},
53 {firstName: 'Aaron', lastName: 'Conran'},
54 {firstName: 'Jamie', lastName: 'Avins'}
57 </code></pre>
59 * <p>Loading inline data using the method above is great if the data is in the correct format already (e.g. it doesn't need
60 * to be processed by a {@link Ext.data.reader.Reader reader}). If your inline data requires processing to decode the data structure,
61 * use a {@link Ext.data.proxy.Memory MemoryProxy} instead (see the {@link Ext.data.proxy.Memory MemoryProxy} docs for an example).</p>
63 * <p>Additional data can also be loaded locally using {@link #add}.</p>
65 * <p><u>Loading Nested Data</u></p>
67 * <p>Applications often need to load sets of associated data - for example a CRM system might load a User and her Orders.
68 * Instead of issuing an AJAX request for the User and a series of additional AJAX requests for each Order, we can load a nested dataset
69 * and allow the Reader to automatically populate the associated models. Below is a brief example, see the {@link Ext.data.reader.Reader} intro
70 * docs for a full explanation:</p>
72 <pre><code>
73 var store = new Ext.data.Store({
75 model: "User",
85 </code></pre>
87 * <p>Which would consume a response like this:</p>
89 <pre><code>
94 "name": "Ed",
98 "total": 10.76,
99 "status": "invoiced"
103 "total": 13.45,
104 "status": "shipped"
110 </code></pre>
112 * <p>See the {@link Ext.data.reader.Reader} intro docs for a full explanation.</p>
114 * <p><u>Filtering and Sorting</u></p>
116 * <p>Stores can be sorted and filtered - in both cases either remotely or locally. The {@link #sorters} and {@link #filters} are
117 * held inside {@link Ext.util.MixedCollection MixedCollection} instances to make them easy to manage. Usually it is sufficient to
118 * either just specify sorters and filters in the Store configuration or call {@link #sort} or {@link #filter}:
120 <pre><code>
121 var store = new Ext.data.Store({
129 property : 'firstName',
136 property: 'firstName',
141 </code></pre>
143 * <p>The new Store will keep the configured sorters and filters in the MixedCollection instances mentioned above. By default, sorting
144 * and filtering are both performed locally by the Store - see {@link #remoteSort} and {@link #remoteFilter} to allow the server to
145 * perform these operations instead.</p>
147 * <p>Filtering and sorting after the Store has been instantiated is also easy. Calling {@link #filter} adds another filter to the Store
148 * and automatically filters the dataset (calling {@link #filter} with no arguments simply re-applies all existing filters). Note that by
149 * default {@link #sortOnFilter} is set to true, which means that your sorters are automatically reapplied if using local sorting.</p>
151 <pre><code>
152 store.filter('eyeColor', 'Brown');
153 </code></pre>
155 * <p>Change the sorting at any time by calling {@link #sort}:</p>
157 <pre><code>
158 store.sort('height', 'ASC');
159 </code></pre>
161 * <p>Note that all existing sorters will be removed in favor of the new sorter data (if {@link #sort} is called with no arguments,
162 * the existing sorters are just reapplied instead of being removed). To keep existing sorters and add new ones, just add them
163 * to the MixedCollection:</p>
165 <pre><code>
166 store.sorters.add(new Ext.util.Sorter({
167 property : 'shoeSize',
172 </code></pre>
174 * <p><u>Registering with StoreManager</u></p>
176 * <p>Any Store that is instantiated with a {@link #storeId} will automatically be registed with the {@link Ext.data.StoreManager StoreManager}.
177 * This makes it easy to reuse the same store in multiple views:</p>
179 <pre><code>
180 //this store can be used several times
183 storeId: 'usersStore'
189 //other config goes here
195 //other config goes here
197 </code></pre>
199 * <p><u>Further Reading</u></p>
201 * <p>Stores are backed up by an ecosystem of classes that enables their operation. To gain a full understanding of these
202 * pieces and how they fit together, see:</p>
204 * <ul style="list-style-type: disc; padding-left: 25px">
205 * <li>{@link Ext.data.proxy.Proxy Proxy} - overview of what Proxies are and how they are used</li>
206 * <li>{@link Ext.data.Model Model} - the core class in the data package</li>
207 * <li>{@link Ext.data.reader.Reader Reader} - used by any subclass of {@link Ext.data.proxy.Server ServerProxy} to read a response</li>
211 * @param {Object} config Optional config object
213 Ext.define('Ext.data.Store', {
214 extend: 'Ext.data.AbstractStore',
216 alias: 'store.store',
218 requires: ['Ext.ModelManager', 'Ext.data.Model', 'Ext.util.Grouper'],
219 uses: ['Ext.data.proxy.Memory'],
221 <span id='Ext-data.Store-cfg-remoteSort'> /**
222 </span> * @cfg {Boolean} remoteSort
223 * True to defer any sorting operation to the server. If false, sorting is done locally on the client. Defaults to <tt>false</tt>.
227 <span id='Ext-data.Store-cfg-remoteFilter'> /**
228 </span> * @cfg {Boolean} remoteFilter
229 * True to defer any filtering operation to the server. If false, filtering is done locally on the client. Defaults to <tt>false</tt>.
233 <span id='Ext-data.Store-cfg-remoteGroup'> /**
234 </span> * @cfg {Boolean} remoteGroup
235 * True if the grouping should apply on the server side, false if it is local only (defaults to false). If the
236 * grouping is local, it can be applied immediately to the data. If it is remote, then it will simply act as a
237 * helper, automatically sending the grouping information to the server.
241 <span id='Ext-data.Store-cfg-proxy'> /**
242 </span> * @cfg {String/Ext.data.proxy.Proxy/Object} proxy The Proxy to use for this Store. This can be either a string, a config
243 * object or a Proxy instance - see {@link #setProxy} for details.
246 <span id='Ext-data.Store-cfg-data'> /**
247 </span> * @cfg {Array} data Optional array of Model instances or data objects to load locally. See "Inline data" above for details.
250 <span id='Ext-data.Store-cfg-model'> /**
251 </span> * @cfg {String} model The {@link Ext.data.Model} associated with this store
254 <span id='Ext-data.Store-property-groupField'> /**
255 </span> * The (optional) field by which to group data in the store. Internally, grouping is very similar to sorting - the
256 * groupField and {@link #groupDir} are injected as the first sorter (see {@link #sort}). Stores support a single
257 * level of grouping, and groups can be fetched via the {@link #getGroups} method.
258 * @property groupField
261 groupField: undefined,
263 <span id='Ext-data.Store-property-groupDir'> /**
264 </span> * The direction in which sorting should be applied when grouping. Defaults to "ASC" - the other supported value is "DESC"
268 groupDir: "ASC",
270 <span id='Ext-data.Store-property-pageSize'> /**
271 </span> * The number of records considered to form a 'page'. This is used to power the built-in
272 * paging using the nextPage and previousPage functions. Defaults to 25.
278 <span id='Ext-data.Store-property-currentPage'> /**
279 </span> * The page that the Store has most recently loaded (see {@link #loadPage})
280 * @property currentPage
285 <span id='Ext-data.Store-cfg-clearOnPageLoad'> /**
286 </span> * @cfg {Boolean} clearOnPageLoad True to empty the store when loading another page via {@link #loadPage},
287 * {@link #nextPage} or {@link #previousPage} (defaults to true). Setting to false keeps existing records, allowing
288 * large data sets to be loaded one page at a time but rendered all together.
290 clearOnPageLoad: true,
292 <span id='Ext-data.Store-property-loading'> /**
293 </span> * True if the Store is currently loading via its Proxy
300 <span id='Ext-data.Store-cfg-sortOnFilter'> /**
301 </span> * @cfg {Boolean} sortOnFilter For local filtering only, causes {@link #sort} to be called whenever {@link #filter} is called,
302 * causing the sorters to be reapplied after filtering. Defaults to true
306 <span id='Ext-data.Store-cfg-buffered'> /**
307 </span> * @cfg {Boolean} buffered
308 * Allow the store to buffer and pre-fetch pages of records. This is to be used in conjunction with a view will
309 * tell the store to pre-fetch records ahead of a time.
313 <span id='Ext-data.Store-cfg-purgePageCount'> /**
314 </span> * @cfg {Number} purgePageCount
315 * The number of pages to keep in the cache before purging additional records. A value of 0 indicates to never purge the prefetched data.
316 * This option is only relevant when the {@link #buffered} option is set to true.
323 constructor: function(config) {
324 config = config || {};
327 groupers = config.groupers,
331 if (config.buffered || me.buffered) {
332 me.prefetchData = Ext.create('Ext.util.MixedCollection', false, function(record) {
335 me.pendingRequests = [];
336 me.pagesRequested = [];
338 me.sortOnLoad = false;
339 me.filterOnLoad = false;
343 <span id='Ext-data.Store-event-beforeprefetch'> /**
344 </span> * @event beforeprefetch
345 * Fires before a prefetch occurs. Return false to cancel.
346 * @param {Ext.data.store} this
347 * @param {Ext.data.Operation} operation The associated operation
350 <span id='Ext-data.Store-event-groupchange'> /**
351 </span> * @event groupchange
352 * Fired whenever the grouping in the grid changes
353 * @param {Ext.data.Store} store The store
354 * @param {Array} groupers The array of grouper objects
357 <span id='Ext-data.Store-event-load'> /**
358 </span> * @event load
359 * Fires whenever records have been prefetched
360 * @param {Ext.data.store} this
361 * @param {Array} records An array of records
362 * @param {Boolean} successful True if the operation was successful.
363 * @param {Ext.data.Operation} operation The associated operation
367 data = config.data || me.data;
369 <span id='Ext-data.Store-property-data'> /**
370 </span> * The MixedCollection that holds this store's local cache of records
372 * @type Ext.util.MixedCollection
374 me.data = Ext.create('Ext.util.MixedCollection', false, function(record) {
375 return record.internalId;
379 me.inlineData = data;
383 if (!groupers && config.groupField) {
385 property : config.groupField,
386 direction: config.groupDir
389 delete config.groupers;
391 <span id='Ext-data.Store-property-groupers'> /**
392 </span> * The collection of {@link Ext.util.Grouper Groupers} currently applied to this Store
394 * @type Ext.util.MixedCollection
396 me.groupers = Ext.create('Ext.util.MixedCollection');
397 me.groupers.addAll(me.decodeGroupers(groupers));
399 this.callParent([config]);
401 if (me.groupers.items.length) {
402 me.sort(me.groupers.items, 'prepend', false);
406 data = me.inlineData;
409 if (proxy instanceof Ext.data.proxy.Memory) {
413 me.add.apply(me, data);
417 delete me.inlineData;
418 } else if (me.autoLoad) {
419 Ext.defer(me.load, 10, me, [typeof me.autoLoad === 'object' ? me.autoLoad: undefined]);
420 // Remove the defer call, we may need reinstate this at some point, but currently it's not obvious why it's here.
421 // this.load(typeof this.autoLoad == 'object' ? this.autoLoad : undefined);
425 onBeforeSort: function() {
426 this.sort(this.groupers.items, 'prepend', false);
429 <span id='Ext-data.Store-method-decodeGroupers'> /**
431 * Normalizes an array of grouper objects, ensuring that they are all Ext.util.Grouper instances
432 * @param {Array} groupers The groupers array
433 * @return {Array} Array of Ext.util.Grouper objects
435 decodeGroupers: function(groupers) {
436 if (!Ext.isArray(groupers)) {
437 if (groupers === undefined) {
440 groupers = [groupers];
444 var length = groupers.length,
445 Grouper = Ext.util.Grouper,
448 for (i = 0; i < length; i++) {
449 config = groupers[i];
451 if (!(config instanceof Grouper)) {
452 if (Ext.isString(config)) {
458 Ext.applyIf(config, {
460 direction: "ASC"
463 //support for 3.x style sorters where a function can be defined as 'fn'
465 config.sorterFn = config.fn;
468 //support a function to be passed as a sorter definition
469 if (typeof config == 'function') {
475 groupers[i] = new Grouper(config);
482 <span id='Ext-data.Store-method-group'> /**
483 </span> * Group data in the store
484 * @param {String|Array} groupers Either a string name of one of the fields in this Store's configured {@link Ext.data.Model Model},
485 * or an Array of grouper configurations.
486 * @param {String} direction The overall direction to group the data by. Defaults to "ASC".
488 group: function(groupers, direction) {
493 if (Ext.isArray(groupers)) {
494 newGroupers = groupers;
495 } else if (Ext.isObject(groupers)) {
496 newGroupers = [groupers];
497 } else if (Ext.isString(groupers)) {
498 grouper = me.groupers.get(groupers);
505 newGroupers = [grouper];
506 } else if (direction === undefined) {
509 grouper.setDirection(direction);
513 if (newGroupers && newGroupers.length) {
514 newGroupers = me.decodeGroupers(newGroupers);
516 me.groupers.addAll(newGroupers);
519 if (me.remoteGroup) {
522 callback: me.fireGroupChange
526 me.fireEvent('groupchange', me, me.groupers);
530 <span id='Ext-data.Store-method-clearGrouping'> /**
531 </span> * Clear any groupers in the store
533 clearGrouping: function(){
535 // Clear any groupers we pushed on to the sorters
536 me.groupers.each(function(grouper){
537 me.sorters.remove(grouper);
540 if (me.remoteGroup) {
543 callback: me.fireGroupChange
547 me.fireEvent('groupchange', me, me.groupers);
551 <span id='Ext-data.Store-method-isGrouped'> /**
552 </span> * Checks if the store is currently grouped
553 * @return {Boolean} True if the store is grouped.
555 isGrouped: function() {
556 return this.groupers.getCount() > 0;
559 <span id='Ext-data.Store-method-fireGroupChange'> /**
560 </span> * Fires the groupchange event. Abstracted out so we can use it
564 fireGroupChange: function(){
565 this.fireEvent('groupchange', this, this.groupers);
568 <span id='Ext-data.Store-method-getGroups'> /**
569 </span> * Returns an object containing the result of applying grouping to the records in this store. See {@link #groupField},
570 * {@link #groupDir} and {@link #getGroupString}. Example for a store containing records with a color field:
571 <pre><code>
572 var myStore = new Ext.data.Store({
577 myStore.getGroups(); //returns:
582 //all records where the color field is 'yellow'
588 //all records where the color field is 'red'
592 </code></pre>
593 * @param {String} groupName (Optional) Pass in an optional groupName argument to access a specific group as defined by {@link #getGroupString}
594 * @return {Array} The grouped data
596 getGroups: function(requestGroupString) {
597 var records = this.data.items,
598 length = records.length,
606 for (i = 0; i < length; i++) {
608 groupStr = this.getGroupString(record);
609 group = pointers[groupStr];
611 if (group === undefined) {
618 pointers[groupStr] = group;
621 group.children.push(record);
624 return requestGroupString ? pointers[requestGroupString] : groups;
627 <span id='Ext-data.Store-method-getGroupsForGrouper'> /**
629 * For a given set of records and a Grouper, returns an array of arrays - each of which is the set of records
630 * matching a certain group.
632 getGroupsForGrouper: function(records, grouper) {
633 var length = records.length,
641 for (i = 0; i < length; i++) {
643 newValue = grouper.getGroupString(record);
645 if (newValue !== oldValue) {
654 group.records.push(record);
662 <span id='Ext-data.Store-method-getGroupsForGrouperIndex'> /**
664 * This is used recursively to gather the records into the configured Groupers. The data MUST have been sorted for
665 * this to work properly (see {@link #getGroupData} and {@link #getGroupsForGrouper}) Most of the work is done by
666 * {@link #getGroupsForGrouper} - this function largely just handles the recursion.
667 * @param {Array} records The set or subset of records to group
668 * @param {Number} grouperIndex The grouper index to retrieve
669 * @return {Array} The grouped records
671 getGroupsForGrouperIndex: function(records, grouperIndex) {
673 groupers = me.groupers,
674 grouper = groupers.getAt(grouperIndex),
675 groups = me.getGroupsForGrouper(records, grouper),
676 length = groups.length,
679 if (grouperIndex + 1 < groupers.length) {
680 for (i = 0; i < length; i++) {
681 groups[i].children = me.getGroupsForGrouperIndex(groups[i].records, grouperIndex + 1);
685 for (i = 0; i < length; i++) {
686 groups[i].depth = grouperIndex;
692 <span id='Ext-data.Store-method-getGroupData'> /**
694 * <p>Returns records grouped by the configured {@link #groupers grouper} configuration. Sample return value (in
695 * this case grouping by genre and then author in a fictional books dataset):</p>
696 <pre><code>
702 //book1, book2, book3, book4
722 </code></pre>
723 * @param {Boolean} sort True to call {@link #sort} before finding groups. Sorting is required to make grouping
724 * function correctly so this should only be set to false if the Store is known to already be sorted correctly
726 * @return {Array} The group data
728 getGroupData: function(sort) {
730 if (sort !== false) {
734 return me.getGroupsForGrouperIndex(me.data.items, 0);
737 <span id='Ext-data.Store-method-getGroupString'> /**
738 </span> * <p>Returns the string to group on for a given model instance. The default implementation of this method returns
739 * the model's {@link #groupField}, but this can be overridden to group by an arbitrary string. For example, to
740 * group by the first letter of a model's 'name' field, use the following code:</p>
741 <pre><code>
744 getGroupString: function(instance) {
745 return instance.get('name')[0];
748 </code></pre>
749 * @param {Ext.data.Model} instance The model instance
750 * @return {String} The string to compare when forming groups
752 getGroupString: function(instance) {
753 var group = this.groupers.first();
755 return instance.get(group.property);
759 <span id='Ext-data.Store-method-insert'> /**
760 </span> * Inserts Model instances into the Store at the given index and fires the {@link #add} event.
761 * See also <code>{@link #add}</code>.
762 * @param {Number} index The start index at which to insert the passed Records.
763 * @param {Ext.data.Model[]} records An Array of Ext.data.Model objects to add to the cache.
765 insert: function(index, records) {
772 records = [].concat(records);
773 for (i = 0, len = records.length; i < len; i++) {
774 record = me.createModel(records[i]);
775 record.set(me.modelDefaults);
776 // reassign the model in the array in case it wasn't created yet
779 me.data.insert(index + i, record);
782 sync = sync || record.phantom === true;
786 me.snapshot.addAll(records);
789 me.fireEvent('add', me, records, index);
790 me.fireEvent('datachanged', me);
791 if (me.autoSync && sync) {
796 <span id='Ext-data.Store-method-add'> /**
797 </span> * Adds Model instances to the Store by instantiating them based on a JavaScript object. When adding already-
798 * instantiated Models, use {@link #insert} instead. The instances will be added at the end of the existing collection.
799 * This method accepts either a single argument array of Model instances or any number of model instance arguments.
802 <pre><code>
803 myStore.add({some: 'data'}, {some: 'other data'});
804 </code></pre>
806 * @param {Object} data The data for each model
807 * @return {Array} The array of newly created model instances
809 add: function(records) {
810 //accept both a single-argument array of records, or any number of record arguments
811 if (!Ext.isArray(records)) {
812 records = Array.prototype.slice.apply(arguments);
817 length = records.length,
820 for (; i < length; i++) {
821 record = me.createModel(records[i]);
822 // reassign the model in the array in case it wasn't created yet
826 me.insert(me.data.length, records);
831 <span id='Ext-data.Store-method-createModel'> /**
832 </span> * Converts a literal to a model, if it's not a model already
834 * @param record {Ext.data.Model/Object} The record to create
835 * @return {Ext.data.Model}
837 createModel: function(record) {
838 if (!record.isModel) {
839 record = Ext.ModelManager.create(record, this.model);
845 <span id='Ext-data.Store-method-each'> /**
846 </span> * Calls the specified function for each of the {@link Ext.data.Model Records} in the cache.
847 * @param {Function} fn The function to call. The {@link Ext.data.Model Record} is passed as the first parameter.
848 * Returning <tt>false</tt> aborts and exits the iteration.
849 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed.
850 * Defaults to the current {@link Ext.data.Model Record} in the iteration.
852 each: function(fn, scope) {
853 this.data.each(fn, scope);
856 <span id='Ext-data.Store-method-remove'> /**
857 </span> * Removes the given record from the Store, firing the 'remove' event for each instance that is removed, plus a single
858 * 'datachanged' event after removal.
859 * @param {Ext.data.Model/Array} records The Ext.data.Model instance or array of instances to remove
861 remove: function(records, /* private */ isMove) {
862 if (!Ext.isArray(records)) {
867 * Pass the isMove parameter if we know we're going to be re-inserting this record
869 isMove = isMove === true;
873 length = records.length,
878 for (; i < length; i++) {
880 index = me.data.indexOf(record);
883 me.snapshot.remove(record);
887 isPhantom = record.phantom === true;
888 if (!isMove && !isPhantom) {
889 // don't push phantom records onto removed
890 me.removed.push(record);
894 me.data.remove(record);
895 sync = sync || !isPhantom;
897 me.fireEvent('remove', me, record, index);
901 me.fireEvent('datachanged', me);
902 if (!isMove && me.autoSync && sync) {
907 <span id='Ext-data.Store-method-removeAt'> /**
908 </span> * Removes the model instance at the given index
909 * @param {Number} index The record index
911 removeAt: function(index) {
912 var record = this.getAt(index);
919 <span id='Ext-data.Store-method-load'> /**
920 </span> * <p>Loads data into the Store via the configured {@link #proxy}. This uses the Proxy to make an
921 * asynchronous call to whatever storage backend the Proxy uses, automatically adding the retrieved
922 * instances into the Store and calling an optional callback if required. Example usage:</p>
924 <pre><code>
927 callback: function(records, operation, success) {
928 //the {@link Ext.data.Operation operation} object contains all of the details of the load operation
929 console.log(records);
932 </code></pre>
934 * <p>If the callback scope does not need to be set, a function can simply be passed:</p>
936 <pre><code>
937 store.load(function(records, operation, success) {
938 console.log('loaded records');
940 </code></pre>
942 * @param {Object/Function} options Optional config object, passed into the Ext.data.Operation object before loading.
944 load: function(options) {
947 options = options || {};
949 if (Ext.isFunction(options)) {
955 Ext.applyIf(options, {
956 groupers: me.groupers.items,
957 page: me.currentPage,
958 start: (me.currentPage - 1) * me.pageSize,
963 return me.callParent([options]);
966 <span id='Ext-data.Store-method-onProxyLoad'> /**
968 * Called internally when a Proxy has completed a load request
970 onProxyLoad: function(operation) {
972 resultSet = operation.getResultSet(),
973 records = operation.getRecords(),
974 successful = operation.wasSuccessful();
977 me.totalCount = resultSet.total;
981 me.loadRecords(records, operation);
985 me.fireEvent('load', me, records, successful);
987 //TODO: deprecate this event, it should always have been 'load' instead. 'load' is now documented, 'read' is not.
988 //People are definitely using this so can't deprecate safely until 2.x
989 me.fireEvent('read', me, records, operation.wasSuccessful());
991 //this is a callback that would have been passed to the 'read' function and is optional
992 Ext.callback(operation.callback, operation.scope || me, [records, operation, successful]);
995 <span id='Ext-data.Store-method-onCreateRecords'> /**
996 </span> * Create any new records when a write is returned from the server.
998 * @param {Array} records The array of new records
999 * @param {Ext.data.Operation} operation The operation that just completed
1000 * @param {Boolean} success True if the operation was successful
1002 onCreateRecords: function(records, operation, success) {
1006 snapshot = this.snapshot,
1007 length = records.length,
1008 originalRecords = operation.records,
1013 <span id='Ext-data.Store-property-'> /**
1014 </span> * Loop over each record returned from the server. Assume they are
1015 * returned in order of how they were sent. If we find a matching
1016 * record, replace it with the newly created one.
1018 for (; i < length; ++i) {
1019 record = records[i];
1020 original = originalRecords[i];
1022 index = data.indexOf(original);
1023 if (index > -1) {
1024 data.removeAt(index);
1025 data.insert(index, record);
1028 index = snapshot.indexOf(original);
1029 if (index > -1) {
1030 snapshot.removeAt(index);
1031 snapshot.insert(index, record);
1034 record.phantom = false;
1041 <span id='Ext-data.Store-method-onUpdateRecords'> /**
1042 </span> * Update any records when a write is returned from the server.
1044 * @param {Array} records The array of updated records
1045 * @param {Ext.data.Operation} operation The operation that just completed
1046 * @param {Boolean} success True if the operation was successful
1048 onUpdateRecords: function(records, operation, success){
1051 length = records.length,
1053 snapshot = this.snapshot,
1056 for (; i < length; ++i) {
1057 record = records[i];
1058 data.replace(record);
1060 snapshot.replace(record);
1067 <span id='Ext-data.Store-method-onDestroyRecords'> /**
1068 </span> * Remove any records when a write is returned from the server.
1070 * @param {Array} records The array of removed records
1071 * @param {Ext.data.Operation} operation The operation that just completed
1072 * @param {Boolean} success True if the operation was successful
1074 onDestroyRecords: function(records, operation, success){
1078 length = records.length,
1080 snapshot = me.snapshot,
1083 for (; i < length; ++i) {
1084 record = records[i];
1086 data.remove(record);
1088 snapshot.remove(record);
1096 getNewRecords: function() {
1097 return this.data.filterBy(this.filterNew).items;
1101 getUpdatedRecords: function() {
1102 return this.data.filterBy(this.filterUpdated).items;
1105 <span id='Ext-data.Store-method-filter'> /**
1106 </span> * Filters the loaded set of records by a given set of filters.
1107 * @param {Mixed} filters The set of filters to apply to the data. These are stored internally on the store,
1108 * but the filtering itself is done on the Store's {@link Ext.util.MixedCollection MixedCollection}. See
1109 * MixedCollection's {@link Ext.util.MixedCollection#filter filter} method for filter syntax. Alternatively,
1110 * pass in a property string
1111 * @param {String} value Optional value to filter by (only if using a property string as the first argument)
1113 filter: function(filters, value) {
1114 if (Ext.isString(filters)) {
1122 decoded = me.decodeFilters(filters),
1124 doLocalSort = me.sortOnFilter && !me.remoteSort,
1125 length = decoded.length;
1127 for (; i < length; i++) {
1128 me.filters.replace(decoded[i]);
1131 if (me.remoteFilter) {
1132 //the load function will pick up the new filters and request the filtered data from the proxy
1135 <span id='Ext-data.Store-property-snapshot'> /**
1136 </span> * A pristine (unfiltered) collection of the records in this store. This is used to reinstate
1137 * records when a filter is removed or changed
1138 * @property snapshot
1139 * @type Ext.util.MixedCollection
1141 if (me.filters.getCount()) {
1142 me.snapshot = me.snapshot || me.data.clone();
1143 me.data = me.data.filter(me.filters.items);
1148 // fire datachanged event if it hasn't already been fired by doSort
1149 if (!doLocalSort || me.sorters.length < 1) {
1150 me.fireEvent('datachanged', me);
1156 <span id='Ext-data.Store-method-clearFilter'> /**
1157 </span> * Revert to a view of the Record cache with no filtering applied.
1158 * @param {Boolean} suppressEvent If <tt>true</tt> the filter is cleared silently without firing the
1159 * {@link #datachanged} event.
1161 clearFilter: function(suppressEvent) {
1166 if (me.remoteFilter) {
1168 } else if (me.isFiltered()) {
1169 me.data = me.snapshot.clone();
1172 if (suppressEvent !== true) {
1173 me.fireEvent('datachanged', me);
1178 <span id='Ext-data.Store-method-isFiltered'> /**
1179 </span> * Returns true if this store is currently filtered
1182 isFiltered: function() {
1183 var snapshot = this.snapshot;
1184 return !! snapshot && snapshot !== this.data;
1187 <span id='Ext-data.Store-method-filterBy'> /**
1188 </span> * Filter by a function. The specified function will be called for each
1189 * Record in this Store. If the function returns <tt>true</tt> the Record is included,
1190 * otherwise it is filtered out.
1191 * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
1192 * <li><b>record</b> : Ext.data.Model<p class="sub-desc">The {@link Ext.data.Model record}
1193 * to test for filtering. Access field values using {@link Ext.data.Model#get}.</p></li>
1194 * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
1196 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
1198 filterBy: function(fn, scope) {
1201 me.snapshot = me.snapshot || me.data.clone();
1202 me.data = me.queryBy(fn, scope || me);
1203 me.fireEvent('datachanged', me);
1206 <span id='Ext-data.Store-method-queryBy'> /**
1207 </span> * Query the cached records in this Store using a filtering function. The specified function
1208 * will be called with each record in this Store. If the function returns <tt>true</tt> the record is
1209 * included in the results.
1210 * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
1211 * <li><b>record</b> : Ext.data.Model<p class="sub-desc">The {@link Ext.data.Model record}
1212 * to test for filtering. Access field values using {@link Ext.data.Model#get}.</p></li>
1213 * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
1215 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
1216 * @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records
1218 queryBy: function(fn, scope) {
1220 data = me.snapshot || me.data;
1221 return data.filterBy(fn, scope || me);
1224 <span id='Ext-data.Store-method-loadData'> /**
1225 </span> * Loads an array of data straight into the Store
1226 * @param {Array} data Array of data to load. Any non-model instances will be cast into model instances first
1227 * @param {Boolean} append True to add the records to the existing records in the store, false to remove the old ones first
1229 loadData: function(data, append) {
1230 var model = this.model,
1231 length = data.length,
1235 //make sure each data element is an Ext.data.Model instance
1236 for (i = 0; i < length; i++) {
1239 if (! (record instanceof Ext.data.Model)) {
1240 data[i] = Ext.ModelManager.create(record, model);
1244 this.loadRecords(data, {addRecords: append});
1247 <span id='Ext-data.Store-method-loadRecords'> /**
1248 </span> * Loads an array of {@Ext.data.Model model} instances into the store, fires the datachanged event. This should only usually
1249 * be called internally when loading from the {@link Ext.data.proxy.Proxy Proxy}, when adding records manually use {@link #add} instead
1250 * @param {Array} records The array of records to load
1251 * @param {Object} options {addRecords: true} to add these records to the existing records, false to remove the Store's existing records first
1253 loadRecords: function(records, options) {
1256 length = records.length;
1258 options = options || {};
1261 if (!options.addRecords) {
1266 me.data.addAll(records);
1268 //FIXME: this is not a good solution. Ed Spencer is totally responsible for this and should be forced to fix it immediately.
1269 for (; i < length; i++) {
1270 if (options.start !== undefined) {
1271 records[i].index = options.start + i;
1274 records[i].join(me);
1278 * this rather inelegant suspension and resumption of events is required because both the filter and sort functions
1279 * fire an additional datachanged event, which is not wanted. Ideally we would do this a different way. The first
1280 * datachanged event is fired by the call to this.add, above.
1284 if (me.filterOnLoad && !me.remoteFilter) {
1288 if (me.sortOnLoad && !me.remoteSort) {
1293 me.fireEvent('datachanged', me, records);
1297 <span id='Ext-data.Store-method-loadPage'> /**
1298 </span> * Loads a given 'page' of data by setting the start and limit values appropriately. Internally this just causes a normal
1299 * load operation, passing in calculated 'start' and 'limit' params
1300 * @param {Number} page The number of the page to load
1302 loadPage: function(page) {
1305 me.currentPage = page;
1309 start: (page - 1) * me.pageSize,
1311 addRecords: !me.clearOnPageLoad
1315 <span id='Ext-data.Store-method-nextPage'> /**
1316 </span> * Loads the next 'page' in the current data set
1318 nextPage: function() {
1319 this.loadPage(this.currentPage + 1);
1322 <span id='Ext-data.Store-method-previousPage'> /**
1323 </span> * Loads the previous 'page' in the current data set
1325 previousPage: function() {
1326 this.loadPage(this.currentPage - 1);
1330 clearData: function() {
1331 this.data.each(function(record) {
1339 <span id='Ext-data.Store-method-prefetch'> /**
1340 </span> * Prefetches data the Store using its configured {@link #proxy}.
1341 * @param {Object} options Optional config object, passed into the Ext.data.Operation object before loading.
1344 prefetch: function(options) {
1347 requestId = me.getRequestId();
1349 options = options || {};
1351 Ext.applyIf(options, {
1353 filters: me.filters.items,
1354 sorters: me.sorters.items,
1355 requestId: requestId
1357 me.pendingRequests.push(requestId);
1359 operation = Ext.create('Ext.data.Operation', options);
1361 // HACK to implement loadMask support.
1362 //if (operation.blocking) {
1363 // me.fireEvent('beforeload', me, operation);
1365 if (me.fireEvent('beforeprefetch', me, operation) !== false) {
1367 me.proxy.read(operation, me.onProxyPrefetch, me);
1373 <span id='Ext-data.Store-method-prefetchPage'> /**
1374 </span> * Prefetches a page of data.
1375 * @param {Number} page The page to prefetch
1376 * @param {Object} options Optional config object, passed into the Ext.data.Operation object before loading.
1380 prefetchPage: function(page, options) {
1382 pageSize = me.pageSize,
1383 start = (page - 1) * me.pageSize,
1384 end = start + pageSize;
1386 // Currently not requesting this page and range isn't already satisified
1387 if (Ext.Array.indexOf(me.pagesRequested, page) === -1 && !me.rangeSatisfied(start, end)) {
1388 options = options || {};
1389 me.pagesRequested.push(page);
1390 Ext.applyIf(options, {
1394 callback: me.onWaitForGuarantee,
1398 me.prefetch(options);
1403 <span id='Ext-data.Store-method-getRequestId'> /**
1404 </span> * Returns a unique requestId to track requests.
1407 getRequestId: function() {
1408 this.requestSeed = this.requestSeed || 1;
1409 return this.requestSeed++;
1412 <span id='Ext-data.Store-method-onProxyPrefetch'> /**
1413 </span> * Handles a success pre-fetch
1415 * @param {Ext.data.Operation} operation The operation that completed
1417 onProxyPrefetch: function(operation) {
1419 resultSet = operation.getResultSet(),
1420 records = operation.getRecords(),
1422 successful = operation.wasSuccessful();
1425 me.totalCount = resultSet.total;
1426 me.fireEvent('totalcountchange', me.totalCount);
1430 me.cacheRecords(records, operation);
1432 Ext.Array.remove(me.pendingRequests, operation.requestId);
1433 if (operation.page) {
1434 Ext.Array.remove(me.pagesRequested, operation.page);
1438 me.fireEvent('prefetch', me, records, successful, operation);
1440 // HACK to support loadMask
1441 if (operation.blocking) {
1442 me.fireEvent('load', me, records, successful);
1445 //this is a callback that would have been passed to the 'read' function and is optional
1446 Ext.callback(operation.callback, operation.scope || me, [records, operation, successful]);
1449 <span id='Ext-data.Store-method-cacheRecords'> /**
1450 </span> * Caches the records in the prefetch and stripes them with their server-side
1453 * @param {Array} records The records to cache
1454 * @param {Ext.data.Operation} The associated operation
1456 cacheRecords: function(records, operation) {
1459 length = records.length,
1460 start = operation ? operation.start : 0;
1462 if (!Ext.isDefined(me.totalCount)) {
1463 me.totalCount = records.length;
1464 me.fireEvent('totalcountchange', me.totalCount);
1467 for (; i < length; i++) {
1468 // this is the true index, not the viewIndex
1469 records[i].index = start + i;
1472 me.prefetchData.addAll(records);
1473 if (me.purgePageCount) {
1480 <span id='Ext-data.Store-method-purgeRecords'> /**
1481 </span> * Purge the least recently used records in the prefetch if the purgeCount
1482 * has been exceeded.
1484 purgeRecords: function() {
1486 prefetchCount = me.prefetchData.getCount(),
1487 purgeCount = me.purgePageCount * me.pageSize,
1488 numRecordsToPurge = prefetchCount - purgeCount - 1,
1491 for (; i <= numRecordsToPurge; i++) {
1492 me.prefetchData.removeAt(0);
1496 <span id='Ext-data.Store-method-rangeSatisfied'> /**
1497 </span> * Determines if the range has already been satisfied in the prefetchData.
1499 * @param {Number} start The start index
1500 * @param {Number} end The end index in the range
1502 rangeSatisfied: function(start, end) {
1507 for (; i < end; i++) {
1508 if (!me.prefetchData.getByKey(i)) {
1511 if (end - i > me.pageSize) {
1512 Ext.Error.raise("A single page prefetch could never satisfy this request.");
1521 <span id='Ext-data.Store-method-getPageFromRecordIndex'> /**
1522 </span> * Determines the page from a record index
1523 * @param {Number} index The record index
1524 * @return {Number} The page the record belongs to
1526 getPageFromRecordIndex: function(index) {
1527 return Math.floor(index / this.pageSize) + 1;
1530 <span id='Ext-data.Store-method-onGuaranteedRange'> /**
1531 </span> * Handles a guaranteed range being loaded
1534 onGuaranteedRange: function() {
1536 totalCount = me.getTotalCount(),
1537 start = me.requestStart,
1538 end = ((totalCount - 1) < me.requestEnd) ? totalCount - 1 : me.requestEnd,
1544 if (start > end) {
1545 Ext.Error.raise("Start (" + start + ") was greater than end (" + end + ")");
1549 if (start !== me.guaranteedStart && end !== me.guaranteedEnd) {
1550 me.guaranteedStart = start;
1551 me.guaranteedEnd = end;
1553 for (; i <= end; i++) {
1554 record = me.prefetchData.getByKey(i);
1557 Ext.Error.raise("Record was not found and store said it was guaranteed");
1562 me.fireEvent('guaranteedrange', range, start, end);
1564 me.cb.call(me.scope || me, range);
1571 // hack to support loadmask
1574 this.fireEvent('beforeload');
1577 // hack to support loadmask
1578 unmask: function() {
1580 this.fireEvent('load');
1584 <span id='Ext-data.Store-method-hasPendingRequests'> /**
1585 </span> * Returns the number of pending requests out.
1587 hasPendingRequests: function() {
1588 return this.pendingRequests.length;
1592 // wait until all requests finish, until guaranteeing the range.
1593 onWaitForGuarantee: function() {
1594 if (!this.hasPendingRequests()) {
1595 this.onGuaranteedRange();
1599 <span id='Ext-data.Store-method-guaranteeRange'> /**
1600 </span> * Guarantee a specific range, this will load the store with a range (that
1601 * must be the pageSize or smaller) and take care of any loading that may
1604 guaranteeRange: function(start, end, cb, scope) {
1606 if (start && end) {
1607 if (end - start > this.pageSize) {
1611 pageSize: this.pageSize,
1612 msg: "Requested a bigger range than the specified pageSize"
1618 end = (end > this.totalCount) ? this.totalCount - 1 : end;
1622 prefetchData = me.prefetchData,
1624 startLoaded = !!prefetchData.getByKey(start),
1625 endLoaded = !!prefetchData.getByKey(end),
1626 startPage = me.getPageFromRecordIndex(start),
1627 endPage = me.getPageFromRecordIndex(end);
1632 me.requestStart = start;
1633 me.requestEnd = end;
1634 // neither beginning or end are loaded
1635 if (!startLoaded || !endLoaded) {
1636 // same page, lets load it
1637 if (startPage === endPage) {
1639 me.prefetchPage(startPage, {
1641 callback: me.onWaitForGuarantee,
1644 // need to load two pages
1647 me.prefetchPage(startPage, {
1649 callback: me.onWaitForGuarantee,
1652 me.prefetchPage(endPage, {
1654 callback: me.onWaitForGuarantee,
1658 // Request was already satisfied via the prefetch
1660 me.onGuaranteedRange();
1664 // because prefetchData is stored by index
1665 // this invalidates all of the prefetchedData
1668 prefetchData = me.prefetchData,
1675 if (me.remoteSort) {
1676 prefetchData.clear();
1677 me.callParent(arguments);
1679 sorters = me.getSorters();
1680 start = me.guaranteedStart;
1681 end = me.guaranteedEnd;
1684 if (sorters.length) {
1685 prefetchData.sort(sorters);
1686 range = prefetchData.getRange();
1687 prefetchData.clear();
1688 me.cacheRecords(range);
1689 delete me.guaranteedStart;
1690 delete me.guaranteedEnd;
1691 me.guaranteeRange(start, end);
1693 me.callParent(arguments);
1696 me.callParent(arguments);
1700 // overriden to provide striping of the indexes as sorting occurs.
1701 // this cannot be done inside of sort because datachanged has already
1702 // fired and will trigger a repaint of the bound view.
1703 doSort: function(sorterFn) {
1705 if (me.remoteSort) {
1706 //the load function will pick up the new sorters and request the sorted data from the proxy
1709 me.data.sortBy(sorterFn);
1711 var range = me.getRange(),
1714 for (; i < ln; i++) {
1718 me.fireEvent('datachanged', me);
1722 <span id='Ext-data.Store-method-find'> /**
1723 </span> * Finds the index of the first matching Record in this store by a specific field value.
1724 * @param {String} fieldName The name of the Record field to test.
1725 * @param {String/RegExp} value Either a string that the field value
1726 * should begin with, or a RegExp to test against the field.
1727 * @param {Number} startIndex (optional) The index to start searching at
1728 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
1729 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison
1730 * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false.
1731 * @return {Number} The matched index or -1
1733 find: function(property, value, start, anyMatch, caseSensitive, exactMatch) {
1734 var fn = this.createFilterFn(property, value, anyMatch, caseSensitive, exactMatch);
1735 return fn ? this.data.findIndexBy(fn, null, start) : -1;
1738 <span id='Ext-data.Store-method-findRecord'> /**
1739 </span> * Finds the first matching Record in this store by a specific field value.
1740 * @param {String} fieldName The name of the Record field to test.
1741 * @param {String/RegExp} value Either a string that the field value
1742 * should begin with, or a RegExp to test against the field.
1743 * @param {Number} startIndex (optional) The index to start searching at
1744 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
1745 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison
1746 * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false.
1747 * @return {Ext.data.Model} The matched record or null
1749 findRecord: function() {
1751 index = me.find.apply(me, arguments);
1752 return index !== -1 ? me.getAt(index) : null;
1755 <span id='Ext-data.Store-method-createFilterFn'> /**
1757 * Returns a filter function used to test a the given property's value. Defers most of the work to
1758 * Ext.util.MixedCollection's createValueMatcher function
1759 * @param {String} property The property to create the filter function for
1760 * @param {String/RegExp} value The string/regex to compare the property value to
1761 * @param {Boolean} anyMatch True if we don't care if the filter value is not the full value (defaults to false)
1762 * @param {Boolean} caseSensitive True to create a case-sensitive regex (defaults to false)
1763 * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false.
1764 * Ignored if anyMatch is true.
1766 createFilterFn: function(property, value, anyMatch, caseSensitive, exactMatch) {
1767 if (Ext.isEmpty(value)) {
1770 value = this.data.createValueMatcher(value, anyMatch, caseSensitive, exactMatch);
1771 return function(r) {
1772 return value.test(r.data[property]);
1776 <span id='Ext-data.Store-method-findExact'> /**
1777 </span> * Finds the index of the first matching Record in this store by a specific field value.
1778 * @param {String} fieldName The name of the Record field to test.
1779 * @param {Mixed} value The value to match the field against.
1780 * @param {Number} startIndex (optional) The index to start searching at
1781 * @return {Number} The matched index or -1
1783 findExact: function(property, value, start) {
1784 return this.data.findIndexBy(function(rec) {
1785 return rec.get(property) === value;
1790 <span id='Ext-data.Store-method-findBy'> /**
1791 </span> * Find the index of the first matching Record in this Store by a function.
1792 * If the function returns <tt>true</tt> it is considered a match.
1793 * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
1794 * <li><b>record</b> : Ext.data.Model<p class="sub-desc">The {@link Ext.data.Model record}
1795 * to test for filtering. Access field values using {@link Ext.data.Model#get}.</p></li>
1796 * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
1798 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
1799 * @param {Number} startIndex (optional) The index to start searching at
1800 * @return {Number} The matched index or -1
1802 findBy: function(fn, scope, start) {
1803 return this.data.findIndexBy(fn, scope, start);
1806 <span id='Ext-data.Store-method-collect'> /**
1807 </span> * Collects unique values for a particular dataIndex from this store.
1808 * @param {String} dataIndex The property to collect
1809 * @param {Boolean} allowNull (optional) Pass true to allow null, undefined or empty string values
1810 * @param {Boolean} bypassFilter (optional) Pass true to collect from all records, even ones which are filtered
1811 * @return {Array} An array of the unique values
1813 collect: function(dataIndex, allowNull, bypassFilter) {
1815 data = (bypassFilter === true && me.snapshot) ? me.snapshot: me.data;
1817 return data.collect(dataIndex, 'data', allowNull);
1820 <span id='Ext-data.Store-method-getCount'> /**
1821 </span> * Gets the number of cached records.
1822 * <p>If using paging, this may not be the total size of the dataset. If the data object
1823 * used by the Reader contains the dataset size, then the {@link #getTotalCount} function returns
1824 * the dataset size. <b>Note</b>: see the Important note in {@link #load}.</p>
1825 * @return {Number} The number of Records in the Store's cache.
1827 getCount: function() {
1828 return this.data.length || 0;
1831 <span id='Ext-data.Store-method-getTotalCount'> /**
1832 </span> * Returns the total number of {@link Ext.data.Model Model} instances that the {@link Ext.data.proxy.Proxy Proxy}
1833 * indicates exist. This will usually differ from {@link #getCount} when using paging - getCount returns the
1834 * number of records loaded into the Store at the moment, getTotalCount returns the number of records that
1835 * could be loaded into the Store if the Store contained all data
1836 * @return {Number} The total number of Model instances available via the Proxy
1838 getTotalCount: function() {
1839 return this.totalCount;
1842 <span id='Ext-data.Store-method-getAt'> /**
1843 </span> * Get the Record at the specified index.
1844 * @param {Number} index The index of the Record to find.
1845 * @return {Ext.data.Model} The Record at the passed index. Returns undefined if not found.
1847 getAt: function(index) {
1848 return this.data.getAt(index);
1851 <span id='Ext-data.Store-method-getRange'> /**
1852 </span> * Returns a range of Records between specified indices.
1853 * @param {Number} startIndex (optional) The starting index (defaults to 0)
1854 * @param {Number} endIndex (optional) The ending index (defaults to the last Record in the Store)
1855 * @return {Ext.data.Model[]} An array of Records
1857 getRange: function(start, end) {
1858 return this.data.getRange(start, end);
1861 <span id='Ext-data.Store-method-getById'> /**
1862 </span> * Get the Record with the specified id.
1863 * @param {String} id The id of the Record to find.
1864 * @return {Ext.data.Model} The Record with the passed id. Returns undefined if not found.
1866 getById: function(id) {
1867 return (this.snapshot || this.data).findBy(function(record) {
1868 return record.getId() === id;
1872 <span id='Ext-data.Store-method-indexOf'> /**
1873 </span> * Get the index within the cache of the passed Record.
1874 * @param {Ext.data.Model} record The Ext.data.Model object to find.
1875 * @return {Number} The index of the passed Record. Returns -1 if not found.
1877 indexOf: function(record) {
1878 return this.data.indexOf(record);
1882 <span id='Ext-data.Store-method-indexOfTotal'> /**
1883 </span> * Get the index within the entire dataset. From 0 to the totalCount.
1884 * @param {Ext.data.Model} record The Ext.data.Model object to find.
1885 * @return {Number} The index of the passed Record. Returns -1 if not found.
1887 indexOfTotal: function(record) {
1888 return record.index || this.indexOf(record);
1891 <span id='Ext-data.Store-method-indexOfId'> /**
1892 </span> * Get the index within the cache of the Record with the passed id.
1893 * @param {String} id The id of the Record to find.
1894 * @return {Number} The index of the Record. Returns -1 if not found.
1896 indexOfId: function(id) {
1897 return this.data.indexOfKey(id);
1900 <span id='Ext-data.Store-method-removeAll'> /**
1901 </span> * Remove all items from the store.
1902 * @param {Boolean} silent Prevent the `clear` event from being fired.
1904 removeAll: function(silent) {
1909 me.snapshot.clear();
1911 if (silent !== true) {
1912 me.fireEvent('clear', me);
1917 * Aggregation methods
1920 <span id='Ext-data.Store-method-first'> /**
1921 </span> * Convenience function for getting the first model instance in the store
1922 * @param {Boolean} grouped (Optional) True to perform the operation for each group
1923 * in the store. The value returned will be an object literal with the key being the group
1924 * name and the first record being the value. The grouped parameter is only honored if
1925 * the store has a groupField.
1926 * @return {Ext.data.Model/undefined} The first model instance in the store, or undefined
1928 first: function(grouped) {
1931 if (grouped && me.isGrouped()) {
1932 return me.aggregate(function(records) {
1933 return records.length ? records[0] : undefined;
1936 return me.data.first();
1940 <span id='Ext-data.Store-method-last'> /**
1941 </span> * Convenience function for getting the last model instance in the store
1942 * @param {Boolean} grouped (Optional) True to perform the operation for each group
1943 * in the store. The value returned will be an object literal with the key being the group
1944 * name and the last record being the value. The grouped parameter is only honored if
1945 * the store has a groupField.
1946 * @return {Ext.data.Model/undefined} The last model instance in the store, or undefined
1948 last: function(grouped) {
1951 if (grouped && me.isGrouped()) {
1952 return me.aggregate(function(records) {
1953 var len = records.length;
1954 return len ? records[len - 1] : undefined;
1957 return me.data.last();
1961 <span id='Ext-data.Store-method-sum'> /**
1962 </span> * Sums the value of <tt>property</tt> for each {@link Ext.data.Model record} between <tt>start</tt>
1963 * and <tt>end</tt> and returns the result.
1964 * @param {String} field A field in each record
1965 * @param {Boolean} grouped (Optional) True to perform the operation for each group
1966 * in the store. The value returned will be an object literal with the key being the group
1967 * name and the sum for that group being the value. The grouped parameter is only honored if
1968 * the store has a groupField.
1969 * @return {Number} The sum
1971 sum: function(field, grouped) {
1974 if (grouped && me.isGrouped()) {
1975 return me.aggregate(me.getSum, me, true, [field]);
1977 return me.getSum(me.data.items, field);
1981 // @private, see sum
1982 getSum: function(records, field) {
1985 len = records.length;
1987 for (; i < len; ++i) {
1988 total += records[i].get(field);
1994 <span id='Ext-data.Store-method-count'> /**
1995 </span> * Gets the count of items in the store.
1996 * @param {Boolean} grouped (Optional) True to perform the operation for each group
1997 * in the store. The value returned will be an object literal with the key being the group
1998 * name and the count for each group being the value. The grouped parameter is only honored if
1999 * the store has a groupField.
2000 * @return {Number} the count
2002 count: function(grouped) {
2005 if (grouped && me.isGrouped()) {
2006 return me.aggregate(function(records) {
2007 return records.length;
2010 return me.getCount();
2014 <span id='Ext-data.Store-method-min'> /**
2015 </span> * Gets the minimum value in the store.
2016 * @param {String} field The field in each record
2017 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2018 * in the store. The value returned will be an object literal with the key being the group
2019 * name and the minimum in the group being the value. The grouped parameter is only honored if
2020 * the store has a groupField.
2021 * @return {Mixed/undefined} The minimum value, if no items exist, undefined.
2023 min: function(field, grouped) {
2026 if (grouped && me.isGrouped()) {
2027 return me.aggregate(me.getMin, me, true, [field]);
2029 return me.getMin(me.data.items, field);
2033 // @private, see min
2034 getMin: function(records, field){
2036 len = records.length,
2040 min = records[0].get(field);
2043 for (; i < len; ++i) {
2044 value = records[i].get(field);
2045 if (value < min) {
2052 <span id='Ext-data.Store-method-max'> /**
2053 </span> * Gets the maximum value in the store.
2054 * @param {String} field The field in each record
2055 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2056 * in the store. The value returned will be an object literal with the key being the group
2057 * name and the maximum in the group being the value. The grouped parameter is only honored if
2058 * the store has a groupField.
2059 * @return {Mixed/undefined} The maximum value, if no items exist, undefined.
2061 max: function(field, grouped) {
2064 if (grouped && me.isGrouped()) {
2065 return me.aggregate(me.getMax, me, true, [field]);
2067 return me.getMax(me.data.items, field);
2071 // @private, see max
2072 getMax: function(records, field) {
2074 len = records.length,
2079 max = records[0].get(field);
2082 for (; i < len; ++i) {
2083 value = records[i].get(field);
2084 if (value > max) {
2091 <span id='Ext-data.Store-method-average'> /**
2092 </span> * Gets the average value in the store.
2093 * @param {String} field The field in each record
2094 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2095 * in the store. The value returned will be an object literal with the key being the group
2096 * name and the group average being the value. The grouped parameter is only honored if
2097 * the store has a groupField.
2098 * @return {Mixed/undefined} The average value, if no items exist, 0.
2100 average: function(field, grouped) {
2102 if (grouped && me.isGrouped()) {
2103 return me.aggregate(me.getAverage, me, true, [field]);
2105 return me.getAverage(me.data.items, field);
2109 // @private, see average
2110 getAverage: function(records, field) {
2112 len = records.length,
2115 if (records.length > 0) {
2116 for (; i < len; ++i) {
2117 sum += records[i].get(field);
2124 <span id='Ext-data.Store-method-aggregate'> /**
2125 </span> * Runs the aggregate function for all the records in the store.
2126 * @param {Function} fn The function to execute. The function is called with a single parameter,
2127 * an array of records for that group.
2128 * @param {Object} scope (optional) The scope to execute the function in. Defaults to the store.
2129 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2130 * in the store. The value returned will be an object literal with the key being the group
2131 * name and the group average being the value. The grouped parameter is only honored if
2132 * the store has a groupField.
2133 * @param {Array} args (optional) Any arguments to append to the function call
2134 * @return {Object} An object literal with the group names and their appropriate values.
2136 aggregate: function(fn, scope, grouped, args) {
2138 if (grouped && this.isGrouped()) {
2139 var groups = this.getGroups(),
2141 len = groups.length,
2145 for (; i < len; ++i) {
2147 out[group.name] = fn.apply(scope || this, [group.children].concat(args));
2151 return fn.apply(scope || this, [this.data.items].concat(args));
2155 </pre></pre></body></html>