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-Store-method-constructor'><span id='Ext-data-Store'>/**
19 </span></span> * @author Ed Spencer
20 * @class Ext.data.Store
21 * @extends Ext.data.AbstractStore
23 * <p>The Store class encapsulates a client side cache of {@link Ext.data.Model Model} objects. Stores load
24 * data via a {@link Ext.data.proxy.Proxy Proxy}, and also provide functions for {@link #sort sorting},
25 * {@link #filter filtering} and querying the {@link Ext.data.Model model} instances contained within it.</p>
27 * <p>Creating a Store is easy - we just tell it the Model and the Proxy to use to load and save its data:</p>
29 <pre><code>
30 // Set up a {@link Ext.data.Model model} to use in our Store
32 extend: 'Ext.data.Model',
34 {name: 'firstName', type: 'string'},
35 {name: 'lastName', type: 'string'},
36 {name: 'age', type: 'int'},
37 {name: 'eyeColor', type: 'string'}
41 var myStore = new Ext.data.Store({
53 </code></pre>
55 * <p>In the example above we configured an AJAX proxy to load data from the url '/users.json'. We told our Proxy
56 * to use a {@link Ext.data.reader.Json JsonReader} to parse the response from the server into Model object -
57 * {@link Ext.data.reader.Json see the docs on JsonReader} for details.</p>
59 * <p><u>Inline data</u></p>
61 * <p>Stores can also load data inline. Internally, Store converts each of the objects we pass in as {@link #data}
62 * into Model instances:</p>
64 <pre><code>
68 {firstName: 'Ed', lastName: 'Spencer'},
69 {firstName: 'Tommy', lastName: 'Maintz'},
70 {firstName: 'Aaron', lastName: 'Conran'},
71 {firstName: 'Jamie', lastName: 'Avins'}
74 </code></pre>
76 * <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
77 * to be processed by a {@link Ext.data.reader.Reader reader}). If your inline data requires processing to decode the data structure,
78 * use a {@link Ext.data.proxy.Memory MemoryProxy} instead (see the {@link Ext.data.proxy.Memory MemoryProxy} docs for an example).</p>
80 * <p>Additional data can also be loaded locally using {@link #add}.</p>
82 * <p><u>Loading Nested Data</u></p>
84 * <p>Applications often need to load sets of associated data - for example a CRM system might load a User and her Orders.
85 * 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
86 * and allow the Reader to automatically populate the associated models. Below is a brief example, see the {@link Ext.data.reader.Reader} intro
87 * docs for a full explanation:</p>
89 <pre><code>
90 var store = new Ext.data.Store({
92 model: "User",
102 </code></pre>
104 * <p>Which would consume a response like this:</p>
106 <pre><code>
111 "name": "Ed",
112 "orders": [
115 "total": 10.76,
116 "status": "invoiced"
120 "total": 13.45,
121 "status": "shipped"
127 </code></pre>
129 * <p>See the {@link Ext.data.reader.Reader} intro docs for a full explanation.</p>
131 * <p><u>Filtering and Sorting</u></p>
133 * <p>Stores can be sorted and filtered - in both cases either remotely or locally. The {@link #sorters} and {@link #filters} are
134 * held inside {@link Ext.util.MixedCollection MixedCollection} instances to make them easy to manage. Usually it is sufficient to
135 * either just specify sorters and filters in the Store configuration or call {@link #sort} or {@link #filter}:
137 <pre><code>
138 var store = new Ext.data.Store({
146 property : 'firstName',
153 property: 'firstName',
158 </code></pre>
160 * <p>The new Store will keep the configured sorters and filters in the MixedCollection instances mentioned above. By default, sorting
161 * and filtering are both performed locally by the Store - see {@link #remoteSort} and {@link #remoteFilter} to allow the server to
162 * perform these operations instead.</p>
164 * <p>Filtering and sorting after the Store has been instantiated is also easy. Calling {@link #filter} adds another filter to the Store
165 * and automatically filters the dataset (calling {@link #filter} with no arguments simply re-applies all existing filters). Note that by
166 * default {@link #sortOnFilter} is set to true, which means that your sorters are automatically reapplied if using local sorting.</p>
168 <pre><code>
169 store.filter('eyeColor', 'Brown');
170 </code></pre>
172 * <p>Change the sorting at any time by calling {@link #sort}:</p>
174 <pre><code>
175 store.sort('height', 'ASC');
176 </code></pre>
178 * <p>Note that all existing sorters will be removed in favor of the new sorter data (if {@link #sort} is called with no arguments,
179 * the existing sorters are just reapplied instead of being removed). To keep existing sorters and add new ones, just add them
180 * to the MixedCollection:</p>
182 <pre><code>
183 store.sorters.add(new Ext.util.Sorter({
184 property : 'shoeSize',
189 </code></pre>
191 * <p><u>Registering with StoreManager</u></p>
193 * <p>Any Store that is instantiated with a {@link #storeId} will automatically be registed with the {@link Ext.data.StoreManager StoreManager}.
194 * This makes it easy to reuse the same store in multiple views:</p>
196 <pre><code>
197 //this store can be used several times
200 storeId: 'usersStore'
206 //other config goes here
212 //other config goes here
214 </code></pre>
216 * <p><u>Further Reading</u></p>
218 * <p>Stores are backed up by an ecosystem of classes that enables their operation. To gain a full understanding of these
219 * pieces and how they fit together, see:</p>
221 * <ul style="list-style-type: disc; padding-left: 25px">
222 * <li>{@link Ext.data.proxy.Proxy Proxy} - overview of what Proxies are and how they are used</li>
223 * <li>{@link Ext.data.Model Model} - the core class in the data package</li>
224 * <li>{@link Ext.data.reader.Reader Reader} - used by any subclass of {@link Ext.data.proxy.Server ServerProxy} to read a response</li>
228 * @param {Object} config Optional config object
230 Ext.define('Ext.data.Store', {
231 extend: 'Ext.data.AbstractStore',
233 alias: 'store.store',
235 requires: ['Ext.ModelManager', 'Ext.data.Model', 'Ext.util.Grouper'],
236 uses: ['Ext.data.proxy.Memory'],
238 <span id='Ext-data-Store-cfg-remoteSort'> /**
239 </span> * @cfg {Boolean} remoteSort
240 * True to defer any sorting operation to the server. If false, sorting is done locally on the client. Defaults to <tt>false</tt>.
244 <span id='Ext-data-Store-cfg-remoteFilter'> /**
245 </span> * @cfg {Boolean} remoteFilter
246 * True to defer any filtering operation to the server. If false, filtering is done locally on the client. Defaults to <tt>false</tt>.
250 <span id='Ext-data-Store-cfg-remoteGroup'> /**
251 </span> * @cfg {Boolean} remoteGroup
252 * True if the grouping should apply on the server side, false if it is local only (defaults to false). If the
253 * grouping is local, it can be applied immediately to the data. If it is remote, then it will simply act as a
254 * helper, automatically sending the grouping information to the server.
258 <span id='Ext-data-Store-cfg-proxy'> /**
259 </span> * @cfg {String/Ext.data.proxy.Proxy/Object} proxy The Proxy to use for this Store. This can be either a string, a config
260 * object or a Proxy instance - see {@link #setProxy} for details.
263 <span id='Ext-data-Store-cfg-data'> /**
264 </span> * @cfg {Array} data Optional array of Model instances or data objects to load locally. See "Inline data" above for details.
267 <span id='Ext-data-Store-cfg-model'> /**
268 </span> * @cfg {String} model The {@link Ext.data.Model} associated with this store
271 <span id='Ext-data-Store-property-groupField'> /**
272 </span> * The (optional) field by which to group data in the store. Internally, grouping is very similar to sorting - the
273 * groupField and {@link #groupDir} are injected as the first sorter (see {@link #sort}). Stores support a single
274 * level of grouping, and groups can be fetched via the {@link #getGroups} method.
275 * @property groupField
278 groupField: undefined,
280 <span id='Ext-data-Store-property-groupDir'> /**
281 </span> * The direction in which sorting should be applied when grouping. Defaults to "ASC" - the other supported value is "DESC"
285 groupDir: "ASC",
287 <span id='Ext-data-Store-cfg-pageSize'> /**
288 </span> * @cfg {Number} pageSize
289 * The number of records considered to form a 'page'. This is used to power the built-in
290 * paging using the nextPage and previousPage functions. Defaults to 25.
294 <span id='Ext-data-Store-property-currentPage'> /**
295 </span> * The page that the Store has most recently loaded (see {@link #loadPage})
296 * @property currentPage
301 <span id='Ext-data-Store-cfg-clearOnPageLoad'> /**
302 </span> * @cfg {Boolean} clearOnPageLoad True to empty the store when loading another page via {@link #loadPage},
303 * {@link #nextPage} or {@link #previousPage} (defaults to true). Setting to false keeps existing records, allowing
304 * large data sets to be loaded one page at a time but rendered all together.
306 clearOnPageLoad: true,
308 <span id='Ext-data-Store-property-loading'> /**
309 </span> * True if the Store is currently loading via its Proxy
316 <span id='Ext-data-Store-cfg-sortOnFilter'> /**
317 </span> * @cfg {Boolean} sortOnFilter For local filtering only, causes {@link #sort} to be called whenever {@link #filter} is called,
318 * causing the sorters to be reapplied after filtering. Defaults to true
322 <span id='Ext-data-Store-cfg-buffered'> /**
323 </span> * @cfg {Boolean} buffered
324 * Allow the store to buffer and pre-fetch pages of records. This is to be used in conjunction with a view will
325 * tell the store to pre-fetch records ahead of a time.
329 <span id='Ext-data-Store-cfg-purgePageCount'> /**
330 </span> * @cfg {Number} purgePageCount
331 * The number of pages to keep in the cache before purging additional records. A value of 0 indicates to never purge the prefetched data.
332 * This option is only relevant when the {@link #buffered} option is set to true.
339 constructor: function(config) {
340 config = config || {};
343 groupers = config.groupers || me.groupers,
344 groupField = config.groupField || me.groupField,
348 if (config.buffered || me.buffered) {
349 me.prefetchData = Ext.create('Ext.util.MixedCollection', false, function(record) {
352 me.pendingRequests = [];
353 me.pagesRequested = [];
355 me.sortOnLoad = false;
356 me.filterOnLoad = false;
360 <span id='Ext-data-Store-event-beforeprefetch'> /**
361 </span> * @event beforeprefetch
362 * Fires before a prefetch occurs. Return false to cancel.
363 * @param {Ext.data.store} this
364 * @param {Ext.data.Operation} operation The associated operation
367 <span id='Ext-data-Store-event-groupchange'> /**
368 </span> * @event groupchange
369 * Fired whenever the grouping in the grid changes
370 * @param {Ext.data.Store} store The store
371 * @param {Array} groupers The array of grouper objects
374 <span id='Ext-data-Store-event-load'> /**
375 </span> * @event load
376 * Fires whenever records have been prefetched
377 * @param {Ext.data.store} this
378 * @param {Array} records An array of records
379 * @param {Boolean} successful True if the operation was successful.
380 * @param {Ext.data.Operation} operation The associated operation
384 data = config.data || me.data;
386 <span id='Ext-data-Store-property-data'> /**
387 </span> * The MixedCollection that holds this store's local cache of records
389 * @type Ext.util.MixedCollection
391 me.data = Ext.create('Ext.util.MixedCollection', false, function(record) {
392 return record.internalId;
396 me.inlineData = data;
400 if (!groupers && groupField) {
402 property : groupField,
403 direction: config.groupDir || me.groupDir
406 delete config.groupers;
408 <span id='Ext-data-Store-property-groupers'> /**
409 </span> * The collection of {@link Ext.util.Grouper Groupers} currently applied to this Store
411 * @type Ext.util.MixedCollection
413 me.groupers = Ext.create('Ext.util.MixedCollection');
414 me.groupers.addAll(me.decodeGroupers(groupers));
416 this.callParent([config]);
417 // don't use *config* anymore from here on... use *me* instead...
419 if (me.groupers.items.length) {
420 me.sort(me.groupers.items, 'prepend', false);
424 data = me.inlineData;
427 if (proxy instanceof Ext.data.proxy.Memory) {
431 me.add.apply(me, data);
435 delete me.inlineData;
436 } else if (me.autoLoad) {
437 Ext.defer(me.load, 10, me, [typeof me.autoLoad === 'object' ? me.autoLoad: undefined]);
438 // Remove the defer call, we may need reinstate this at some point, but currently it's not obvious why it's here.
439 // this.load(typeof this.autoLoad == 'object' ? this.autoLoad : undefined);
443 onBeforeSort: function() {
444 this.sort(this.groupers.items, 'prepend', false);
447 <span id='Ext-data-Store-method-decodeGroupers'> /**
449 * Normalizes an array of grouper objects, ensuring that they are all Ext.util.Grouper instances
450 * @param {Array} groupers The groupers array
451 * @return {Array} Array of Ext.util.Grouper objects
453 decodeGroupers: function(groupers) {
454 if (!Ext.isArray(groupers)) {
455 if (groupers === undefined) {
458 groupers = [groupers];
462 var length = groupers.length,
463 Grouper = Ext.util.Grouper,
466 for (i = 0; i < length; i++) {
467 config = groupers[i];
469 if (!(config instanceof Grouper)) {
470 if (Ext.isString(config)) {
476 Ext.applyIf(config, {
478 direction: "ASC"
481 //support for 3.x style sorters where a function can be defined as 'fn'
483 config.sorterFn = config.fn;
486 //support a function to be passed as a sorter definition
487 if (typeof config == 'function') {
493 groupers[i] = new Grouper(config);
500 <span id='Ext-data-Store-method-group'> /**
501 </span> * Group data in the store
502 * @param {String|Array} groupers Either a string name of one of the fields in this Store's configured {@link Ext.data.Model Model},
503 * or an Array of grouper configurations.
504 * @param {String} direction The overall direction to group the data by. Defaults to "ASC".
506 group: function(groupers, direction) {
511 if (Ext.isArray(groupers)) {
512 newGroupers = groupers;
513 } else if (Ext.isObject(groupers)) {
514 newGroupers = [groupers];
515 } else if (Ext.isString(groupers)) {
516 grouper = me.groupers.get(groupers);
523 newGroupers = [grouper];
524 } else if (direction === undefined) {
527 grouper.setDirection(direction);
531 if (newGroupers && newGroupers.length) {
532 newGroupers = me.decodeGroupers(newGroupers);
534 me.groupers.addAll(newGroupers);
537 if (me.remoteGroup) {
540 callback: me.fireGroupChange
544 me.fireEvent('groupchange', me, me.groupers);
548 <span id='Ext-data-Store-method-clearGrouping'> /**
549 </span> * Clear any groupers in the store
551 clearGrouping: function(){
553 // Clear any groupers we pushed on to the sorters
554 me.groupers.each(function(grouper){
555 me.sorters.remove(grouper);
558 if (me.remoteGroup) {
561 callback: me.fireGroupChange
565 me.fireEvent('groupchange', me, me.groupers);
569 <span id='Ext-data-Store-method-isGrouped'> /**
570 </span> * Checks if the store is currently grouped
571 * @return {Boolean} True if the store is grouped.
573 isGrouped: function() {
574 return this.groupers.getCount() > 0;
577 <span id='Ext-data-Store-method-fireGroupChange'> /**
578 </span> * Fires the groupchange event. Abstracted out so we can use it
582 fireGroupChange: function(){
583 this.fireEvent('groupchange', this, this.groupers);
586 <span id='Ext-data-Store-method-getGroups'> /**
587 </span> * Returns an object containing the result of applying grouping to the records in this store. See {@link #groupField},
588 * {@link #groupDir} and {@link #getGroupString}. Example for a store containing records with a color field:
589 <pre><code>
590 var myStore = new Ext.data.Store({
595 myStore.getGroups(); //returns:
600 //all records where the color field is 'yellow'
606 //all records where the color field is 'red'
610 </code></pre>
611 * @param {String} groupName (Optional) Pass in an optional groupName argument to access a specific group as defined by {@link #getGroupString}
612 * @return {Array} The grouped data
614 getGroups: function(requestGroupString) {
615 var records = this.data.items,
616 length = records.length,
624 for (i = 0; i < length; i++) {
626 groupStr = this.getGroupString(record);
627 group = pointers[groupStr];
629 if (group === undefined) {
636 pointers[groupStr] = group;
639 group.children.push(record);
642 return requestGroupString ? pointers[requestGroupString] : groups;
645 <span id='Ext-data-Store-method-getGroupsForGrouper'> /**
647 * For a given set of records and a Grouper, returns an array of arrays - each of which is the set of records
648 * matching a certain group.
650 getGroupsForGrouper: function(records, grouper) {
651 var length = records.length,
659 for (i = 0; i < length; i++) {
661 newValue = grouper.getGroupString(record);
663 if (newValue !== oldValue) {
672 group.records.push(record);
680 <span id='Ext-data-Store-method-getGroupsForGrouperIndex'> /**
682 * This is used recursively to gather the records into the configured Groupers. The data MUST have been sorted for
683 * this to work properly (see {@link #getGroupData} and {@link #getGroupsForGrouper}) Most of the work is done by
684 * {@link #getGroupsForGrouper} - this function largely just handles the recursion.
685 * @param {Array} records The set or subset of records to group
686 * @param {Number} grouperIndex The grouper index to retrieve
687 * @return {Array} The grouped records
689 getGroupsForGrouperIndex: function(records, grouperIndex) {
691 groupers = me.groupers,
692 grouper = groupers.getAt(grouperIndex),
693 groups = me.getGroupsForGrouper(records, grouper),
694 length = groups.length,
697 if (grouperIndex + 1 < groupers.length) {
698 for (i = 0; i < length; i++) {
699 groups[i].children = me.getGroupsForGrouperIndex(groups[i].records, grouperIndex + 1);
703 for (i = 0; i < length; i++) {
704 groups[i].depth = grouperIndex;
710 <span id='Ext-data-Store-method-getGroupData'> /**
712 * <p>Returns records grouped by the configured {@link #groupers grouper} configuration. Sample return value (in
713 * this case grouping by genre and then author in a fictional books dataset):</p>
714 <pre><code>
720 //book1, book2, book3, book4
740 </code></pre>
741 * @param {Boolean} sort True to call {@link #sort} before finding groups. Sorting is required to make grouping
742 * function correctly so this should only be set to false if the Store is known to already be sorted correctly
744 * @return {Array} The group data
746 getGroupData: function(sort) {
748 if (sort !== false) {
752 return me.getGroupsForGrouperIndex(me.data.items, 0);
755 <span id='Ext-data-Store-method-getGroupString'> /**
756 </span> * <p>Returns the string to group on for a given model instance. The default implementation of this method returns
757 * the model's {@link #groupField}, but this can be overridden to group by an arbitrary string. For example, to
758 * group by the first letter of a model's 'name' field, use the following code:</p>
759 <pre><code>
762 getGroupString: function(instance) {
763 return instance.get('name')[0];
766 </code></pre>
767 * @param {Ext.data.Model} instance The model instance
768 * @return {String} The string to compare when forming groups
770 getGroupString: function(instance) {
771 var group = this.groupers.first();
773 return instance.get(group.property);
777 <span id='Ext-data-Store-method-insert'> /**
778 </span> * Inserts Model instances into the Store at the given index and fires the {@link #add} event.
779 * See also <code>{@link #add}</code>.
780 * @param {Number} index The start index at which to insert the passed Records.
781 * @param {Ext.data.Model[]} records An Array of Ext.data.Model objects to add to the cache.
783 insert: function(index, records) {
790 records = [].concat(records);
791 for (i = 0, len = records.length; i < len; i++) {
792 record = me.createModel(records[i]);
793 record.set(me.modelDefaults);
794 // reassign the model in the array in case it wasn't created yet
797 me.data.insert(index + i, record);
800 sync = sync || record.phantom === true;
804 me.snapshot.addAll(records);
807 me.fireEvent('add', me, records, index);
808 me.fireEvent('datachanged', me);
809 if (me.autoSync && sync) {
814 <span id='Ext-data-Store-method-add'> /**
815 </span> * Adds Model instances to the Store by instantiating them based on a JavaScript object. When adding already-
816 * instantiated Models, use {@link #insert} instead. The instances will be added at the end of the existing collection.
817 * This method accepts either a single argument array of Model instances or any number of model instance arguments.
820 <pre><code>
821 myStore.add({some: 'data'}, {some: 'other data'});
822 </code></pre>
824 * @param {Object} data The data for each model
825 * @return {Array} The array of newly created model instances
827 add: function(records) {
828 //accept both a single-argument array of records, or any number of record arguments
829 if (!Ext.isArray(records)) {
830 records = Array.prototype.slice.apply(arguments);
835 length = records.length,
838 for (; i < length; i++) {
839 record = me.createModel(records[i]);
840 // reassign the model in the array in case it wasn't created yet
844 me.insert(me.data.length, records);
849 <span id='Ext-data-Store-method-createModel'> /**
850 </span> * Converts a literal to a model, if it's not a model already
852 * @param record {Ext.data.Model/Object} The record to create
853 * @return {Ext.data.Model}
855 createModel: function(record) {
856 if (!record.isModel) {
857 record = Ext.ModelManager.create(record, this.model);
863 <span id='Ext-data-Store-method-each'> /**
864 </span> * Calls the specified function for each of the {@link Ext.data.Model Records} in the cache.
865 * @param {Function} fn The function to call. The {@link Ext.data.Model Record} is passed as the first parameter.
866 * Returning <tt>false</tt> aborts and exits the iteration.
867 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed.
868 * Defaults to the current {@link Ext.data.Model Record} in the iteration.
870 each: function(fn, scope) {
871 this.data.each(fn, scope);
874 <span id='Ext-data-Store-method-remove'> /**
875 </span> * Removes the given record from the Store, firing the 'remove' event for each instance that is removed, plus a single
876 * 'datachanged' event after removal.
877 * @param {Ext.data.Model/Array} records The Ext.data.Model instance or array of instances to remove
879 remove: function(records, /* private */ isMove) {
880 if (!Ext.isArray(records)) {
885 * Pass the isMove parameter if we know we're going to be re-inserting this record
887 isMove = isMove === true;
891 length = records.length,
896 for (; i < length; i++) {
898 index = me.data.indexOf(record);
901 me.snapshot.remove(record);
905 isPhantom = record.phantom === true;
906 if (!isMove && !isPhantom) {
907 // don't push phantom records onto removed
908 me.removed.push(record);
912 me.data.remove(record);
913 sync = sync || !isPhantom;
915 me.fireEvent('remove', me, record, index);
919 me.fireEvent('datachanged', me);
920 if (!isMove && me.autoSync && sync) {
925 <span id='Ext-data-Store-method-removeAt'> /**
926 </span> * Removes the model instance at the given index
927 * @param {Number} index The record index
929 removeAt: function(index) {
930 var record = this.getAt(index);
937 <span id='Ext-data-Store-method-load'> /**
938 </span> * <p>Loads data into the Store via the configured {@link #proxy}. This uses the Proxy to make an
939 * asynchronous call to whatever storage backend the Proxy uses, automatically adding the retrieved
940 * instances into the Store and calling an optional callback if required. Example usage:</p>
942 <pre><code>
945 callback: function(records, operation, success) {
946 //the {@link Ext.data.Operation operation} object contains all of the details of the load operation
947 console.log(records);
950 </code></pre>
952 * <p>If the callback scope does not need to be set, a function can simply be passed:</p>
954 <pre><code>
955 store.load(function(records, operation, success) {
956 console.log('loaded records');
958 </code></pre>
960 * @param {Object/Function} options Optional config object, passed into the Ext.data.Operation object before loading.
962 load: function(options) {
965 options = options || {};
967 if (Ext.isFunction(options)) {
973 Ext.applyIf(options, {
974 groupers: me.groupers.items,
975 page: me.currentPage,
976 start: (me.currentPage - 1) * me.pageSize,
981 return me.callParent([options]);
984 <span id='Ext-data-Store-method-onProxyLoad'> /**
986 * Called internally when a Proxy has completed a load request
988 onProxyLoad: function(operation) {
990 resultSet = operation.getResultSet(),
991 records = operation.getRecords(),
992 successful = operation.wasSuccessful();
995 me.totalCount = resultSet.total;
999 me.loadRecords(records, operation);
1003 me.fireEvent('load', me, records, successful);
1005 //TODO: deprecate this event, it should always have been 'load' instead. 'load' is now documented, 'read' is not.
1006 //People are definitely using this so can't deprecate safely until 2.x
1007 me.fireEvent('read', me, records, operation.wasSuccessful());
1009 //this is a callback that would have been passed to the 'read' function and is optional
1010 Ext.callback(operation.callback, operation.scope || me, [records, operation, successful]);
1013 <span id='Ext-data-Store-method-onCreateRecords'> /**
1014 </span> * Create any new records when a write is returned from the server.
1016 * @param {Array} records The array of new records
1017 * @param {Ext.data.Operation} operation The operation that just completed
1018 * @param {Boolean} success True if the operation was successful
1020 onCreateRecords: function(records, operation, success) {
1024 snapshot = this.snapshot,
1025 length = records.length,
1026 originalRecords = operation.records,
1032 * Loop over each record returned from the server. Assume they are
1033 * returned in order of how they were sent. If we find a matching
1034 * record, replace it with the newly created one.
1036 for (; i < length; ++i) {
1037 record = records[i];
1038 original = originalRecords[i];
1040 index = data.indexOf(original);
1041 if (index > -1) {
1042 data.removeAt(index);
1043 data.insert(index, record);
1046 index = snapshot.indexOf(original);
1047 if (index > -1) {
1048 snapshot.removeAt(index);
1049 snapshot.insert(index, record);
1052 record.phantom = false;
1059 <span id='Ext-data-Store-method-onUpdateRecords'> /**
1060 </span> * Update any records when a write is returned from the server.
1062 * @param {Array} records The array of updated records
1063 * @param {Ext.data.Operation} operation The operation that just completed
1064 * @param {Boolean} success True if the operation was successful
1066 onUpdateRecords: function(records, operation, success){
1069 length = records.length,
1071 snapshot = this.snapshot,
1074 for (; i < length; ++i) {
1075 record = records[i];
1076 data.replace(record);
1078 snapshot.replace(record);
1085 <span id='Ext-data-Store-method-onDestroyRecords'> /**
1086 </span> * Remove any records when a write is returned from the server.
1088 * @param {Array} records The array of removed records
1089 * @param {Ext.data.Operation} operation The operation that just completed
1090 * @param {Boolean} success True if the operation was successful
1092 onDestroyRecords: function(records, operation, success){
1096 length = records.length,
1098 snapshot = me.snapshot,
1101 for (; i < length; ++i) {
1102 record = records[i];
1104 data.remove(record);
1106 snapshot.remove(record);
1114 getNewRecords: function() {
1115 return this.data.filterBy(this.filterNew).items;
1119 getUpdatedRecords: function() {
1120 return this.data.filterBy(this.filterUpdated).items;
1123 <span id='Ext-data-Store-method-filter'> /**
1124 </span> * Filters the loaded set of records by a given set of filters.
1125 * @param {Mixed} filters The set of filters to apply to the data. These are stored internally on the store,
1126 * but the filtering itself is done on the Store's {@link Ext.util.MixedCollection MixedCollection}. See
1127 * MixedCollection's {@link Ext.util.MixedCollection#filter filter} method for filter syntax. Alternatively,
1128 * pass in a property string
1129 * @param {String} value Optional value to filter by (only if using a property string as the first argument)
1131 filter: function(filters, value) {
1132 if (Ext.isString(filters)) {
1140 decoded = me.decodeFilters(filters),
1142 doLocalSort = me.sortOnFilter && !me.remoteSort,
1143 length = decoded.length;
1145 for (; i < length; i++) {
1146 me.filters.replace(decoded[i]);
1149 if (me.remoteFilter) {
1150 //the load function will pick up the new filters and request the filtered data from the proxy
1153 <span id='Ext-data-Store-property-snapshot'> /**
1154 </span> * A pristine (unfiltered) collection of the records in this store. This is used to reinstate
1155 * records when a filter is removed or changed
1156 * @property snapshot
1157 * @type Ext.util.MixedCollection
1159 if (me.filters.getCount()) {
1160 me.snapshot = me.snapshot || me.data.clone();
1161 me.data = me.data.filter(me.filters.items);
1166 // fire datachanged event if it hasn't already been fired by doSort
1167 if (!doLocalSort || me.sorters.length < 1) {
1168 me.fireEvent('datachanged', me);
1174 <span id='Ext-data-Store-method-clearFilter'> /**
1175 </span> * Revert to a view of the Record cache with no filtering applied.
1176 * @param {Boolean} suppressEvent If <tt>true</tt> the filter is cleared silently without firing the
1177 * {@link #datachanged} event.
1179 clearFilter: function(suppressEvent) {
1184 if (me.remoteFilter) {
1186 } else if (me.isFiltered()) {
1187 me.data = me.snapshot.clone();
1190 if (suppressEvent !== true) {
1191 me.fireEvent('datachanged', me);
1196 <span id='Ext-data-Store-method-isFiltered'> /**
1197 </span> * Returns true if this store is currently filtered
1200 isFiltered: function() {
1201 var snapshot = this.snapshot;
1202 return !! snapshot && snapshot !== this.data;
1205 <span id='Ext-data-Store-method-filterBy'> /**
1206 </span> * Filter by a function. The specified function will be called for each
1207 * Record in this Store. If the function returns <tt>true</tt> the Record is included,
1208 * otherwise it is filtered out.
1209 * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
1210 * <li><b>record</b> : Ext.data.Model<p class="sub-desc">The {@link Ext.data.Model record}
1211 * to test for filtering. Access field values using {@link Ext.data.Model#get}.</p></li>
1212 * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
1214 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
1216 filterBy: function(fn, scope) {
1219 me.snapshot = me.snapshot || me.data.clone();
1220 me.data = me.queryBy(fn, scope || me);
1221 me.fireEvent('datachanged', me);
1224 <span id='Ext-data-Store-method-queryBy'> /**
1225 </span> * Query the cached records in this Store using a filtering function. The specified function
1226 * will be called with each record in this Store. If the function returns <tt>true</tt> the record is
1227 * included in the results.
1228 * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
1229 * <li><b>record</b> : Ext.data.Model<p class="sub-desc">The {@link Ext.data.Model record}
1230 * to test for filtering. Access field values using {@link Ext.data.Model#get}.</p></li>
1231 * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
1233 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
1234 * @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records
1236 queryBy: function(fn, scope) {
1238 data = me.snapshot || me.data;
1239 return data.filterBy(fn, scope || me);
1242 <span id='Ext-data-Store-method-loadData'> /**
1243 </span> * Loads an array of data straight into the Store
1244 * @param {Array} data Array of data to load. Any non-model instances will be cast into model instances first
1245 * @param {Boolean} append True to add the records to the existing records in the store, false to remove the old ones first
1247 loadData: function(data, append) {
1248 var model = this.model,
1249 length = data.length,
1253 //make sure each data element is an Ext.data.Model instance
1254 for (i = 0; i < length; i++) {
1257 if (! (record instanceof Ext.data.Model)) {
1258 data[i] = Ext.ModelManager.create(record, model);
1262 this.loadRecords(data, {addRecords: append});
1265 <span id='Ext-data-Store-method-loadRecords'> /**
1266 </span> * Loads an array of {@Ext.data.Model model} instances into the store, fires the datachanged event. This should only usually
1267 * be called internally when loading from the {@link Ext.data.proxy.Proxy Proxy}, when adding records manually use {@link #add} instead
1268 * @param {Array} records The array of records to load
1269 * @param {Object} options {addRecords: true} to add these records to the existing records, false to remove the Store's existing records first
1271 loadRecords: function(records, options) {
1274 length = records.length;
1276 options = options || {};
1279 if (!options.addRecords) {
1284 me.data.addAll(records);
1286 //FIXME: this is not a good solution. Ed Spencer is totally responsible for this and should be forced to fix it immediately.
1287 for (; i < length; i++) {
1288 if (options.start !== undefined) {
1289 records[i].index = options.start + i;
1292 records[i].join(me);
1296 * this rather inelegant suspension and resumption of events is required because both the filter and sort functions
1297 * fire an additional datachanged event, which is not wanted. Ideally we would do this a different way. The first
1298 * datachanged event is fired by the call to this.add, above.
1302 if (me.filterOnLoad && !me.remoteFilter) {
1306 if (me.sortOnLoad && !me.remoteSort) {
1311 me.fireEvent('datachanged', me, records);
1315 <span id='Ext-data-Store-method-loadPage'> /**
1316 </span> * Loads a given 'page' of data by setting the start and limit values appropriately. Internally this just causes a normal
1317 * load operation, passing in calculated 'start' and 'limit' params
1318 * @param {Number} page The number of the page to load
1320 loadPage: function(page) {
1323 me.currentPage = page;
1327 start: (page - 1) * me.pageSize,
1329 addRecords: !me.clearOnPageLoad
1333 <span id='Ext-data-Store-method-nextPage'> /**
1334 </span> * Loads the next 'page' in the current data set
1336 nextPage: function() {
1337 this.loadPage(this.currentPage + 1);
1340 <span id='Ext-data-Store-method-previousPage'> /**
1341 </span> * Loads the previous 'page' in the current data set
1343 previousPage: function() {
1344 this.loadPage(this.currentPage - 1);
1348 clearData: function() {
1349 this.data.each(function(record) {
1357 <span id='Ext-data-Store-method-prefetch'> /**
1358 </span> * Prefetches data the Store using its configured {@link #proxy}.
1359 * @param {Object} options Optional config object, passed into the Ext.data.Operation object before loading.
1362 prefetch: function(options) {
1365 requestId = me.getRequestId();
1367 options = options || {};
1369 Ext.applyIf(options, {
1371 filters: me.filters.items,
1372 sorters: me.sorters.items,
1373 requestId: requestId
1375 me.pendingRequests.push(requestId);
1377 operation = Ext.create('Ext.data.Operation', options);
1379 // HACK to implement loadMask support.
1380 //if (operation.blocking) {
1381 // me.fireEvent('beforeload', me, operation);
1383 if (me.fireEvent('beforeprefetch', me, operation) !== false) {
1385 me.proxy.read(operation, me.onProxyPrefetch, me);
1391 <span id='Ext-data-Store-method-prefetchPage'> /**
1392 </span> * Prefetches a page of data.
1393 * @param {Number} page The page to prefetch
1394 * @param {Object} options Optional config object, passed into the Ext.data.Operation object before loading.
1398 prefetchPage: function(page, options) {
1400 pageSize = me.pageSize,
1401 start = (page - 1) * me.pageSize,
1402 end = start + pageSize;
1404 // Currently not requesting this page and range isn't already satisified
1405 if (Ext.Array.indexOf(me.pagesRequested, page) === -1 && !me.rangeSatisfied(start, end)) {
1406 options = options || {};
1407 me.pagesRequested.push(page);
1408 Ext.applyIf(options, {
1412 callback: me.onWaitForGuarantee,
1416 me.prefetch(options);
1421 <span id='Ext-data-Store-method-getRequestId'> /**
1422 </span> * Returns a unique requestId to track requests.
1425 getRequestId: function() {
1426 this.requestSeed = this.requestSeed || 1;
1427 return this.requestSeed++;
1430 <span id='Ext-data-Store-method-onProxyPrefetch'> /**
1431 </span> * Handles a success pre-fetch
1433 * @param {Ext.data.Operation} operation The operation that completed
1435 onProxyPrefetch: function(operation) {
1437 resultSet = operation.getResultSet(),
1438 records = operation.getRecords(),
1440 successful = operation.wasSuccessful();
1443 me.totalCount = resultSet.total;
1444 me.fireEvent('totalcountchange', me.totalCount);
1448 me.cacheRecords(records, operation);
1450 Ext.Array.remove(me.pendingRequests, operation.requestId);
1451 if (operation.page) {
1452 Ext.Array.remove(me.pagesRequested, operation.page);
1456 me.fireEvent('prefetch', me, records, successful, operation);
1458 // HACK to support loadMask
1459 if (operation.blocking) {
1460 me.fireEvent('load', me, records, successful);
1463 //this is a callback that would have been passed to the 'read' function and is optional
1464 Ext.callback(operation.callback, operation.scope || me, [records, operation, successful]);
1467 <span id='Ext-data-Store-method-cacheRecords'> /**
1468 </span> * Caches the records in the prefetch and stripes them with their server-side
1471 * @param {Array} records The records to cache
1472 * @param {Ext.data.Operation} The associated operation
1474 cacheRecords: function(records, operation) {
1477 length = records.length,
1478 start = operation ? operation.start : 0;
1480 if (!Ext.isDefined(me.totalCount)) {
1481 me.totalCount = records.length;
1482 me.fireEvent('totalcountchange', me.totalCount);
1485 for (; i < length; i++) {
1486 // this is the true index, not the viewIndex
1487 records[i].index = start + i;
1490 me.prefetchData.addAll(records);
1491 if (me.purgePageCount) {
1498 <span id='Ext-data-Store-method-purgeRecords'> /**
1499 </span> * Purge the least recently used records in the prefetch if the purgeCount
1500 * has been exceeded.
1502 purgeRecords: function() {
1504 prefetchCount = me.prefetchData.getCount(),
1505 purgeCount = me.purgePageCount * me.pageSize,
1506 numRecordsToPurge = prefetchCount - purgeCount - 1,
1509 for (; i <= numRecordsToPurge; i++) {
1510 me.prefetchData.removeAt(0);
1514 <span id='Ext-data-Store-method-rangeSatisfied'> /**
1515 </span> * Determines if the range has already been satisfied in the prefetchData.
1517 * @param {Number} start The start index
1518 * @param {Number} end The end index in the range
1520 rangeSatisfied: function(start, end) {
1525 for (; i < end; i++) {
1526 if (!me.prefetchData.getByKey(i)) {
1529 if (end - i > me.pageSize) {
1530 Ext.Error.raise("A single page prefetch could never satisfy this request.");
1539 <span id='Ext-data-Store-method-getPageFromRecordIndex'> /**
1540 </span> * Determines the page from a record index
1541 * @param {Number} index The record index
1542 * @return {Number} The page the record belongs to
1544 getPageFromRecordIndex: function(index) {
1545 return Math.floor(index / this.pageSize) + 1;
1548 <span id='Ext-data-Store-method-onGuaranteedRange'> /**
1549 </span> * Handles a guaranteed range being loaded
1552 onGuaranteedRange: function() {
1554 totalCount = me.getTotalCount(),
1555 start = me.requestStart,
1556 end = ((totalCount - 1) < me.requestEnd) ? totalCount - 1 : me.requestEnd,
1562 if (start > end) {
1563 Ext.Error.raise("Start (" + start + ") was greater than end (" + end + ")");
1567 if (start !== me.guaranteedStart && end !== me.guaranteedEnd) {
1568 me.guaranteedStart = start;
1569 me.guaranteedEnd = end;
1571 for (; i <= end; i++) {
1572 record = me.prefetchData.getByKey(i);
1575 Ext.Error.raise("Record was not found and store said it was guaranteed");
1580 me.fireEvent('guaranteedrange', range, start, end);
1582 me.cb.call(me.scope || me, range);
1589 // hack to support loadmask
1592 this.fireEvent('beforeload');
1595 // hack to support loadmask
1596 unmask: function() {
1598 this.fireEvent('load');
1602 <span id='Ext-data-Store-method-hasPendingRequests'> /**
1603 </span> * Returns the number of pending requests out.
1605 hasPendingRequests: function() {
1606 return this.pendingRequests.length;
1610 // wait until all requests finish, until guaranteeing the range.
1611 onWaitForGuarantee: function() {
1612 if (!this.hasPendingRequests()) {
1613 this.onGuaranteedRange();
1617 <span id='Ext-data-Store-method-guaranteeRange'> /**
1618 </span> * Guarantee a specific range, this will load the store with a range (that
1619 * must be the pageSize or smaller) and take care of any loading that may
1622 guaranteeRange: function(start, end, cb, scope) {
1624 if (start && end) {
1625 if (end - start > this.pageSize) {
1629 pageSize: this.pageSize,
1630 msg: "Requested a bigger range than the specified pageSize"
1636 end = (end > this.totalCount) ? this.totalCount - 1 : end;
1640 prefetchData = me.prefetchData,
1642 startLoaded = !!prefetchData.getByKey(start),
1643 endLoaded = !!prefetchData.getByKey(end),
1644 startPage = me.getPageFromRecordIndex(start),
1645 endPage = me.getPageFromRecordIndex(end);
1650 me.requestStart = start;
1651 me.requestEnd = end;
1652 // neither beginning or end are loaded
1653 if (!startLoaded || !endLoaded) {
1654 // same page, lets load it
1655 if (startPage === endPage) {
1657 me.prefetchPage(startPage, {
1659 callback: me.onWaitForGuarantee,
1662 // need to load two pages
1665 me.prefetchPage(startPage, {
1667 callback: me.onWaitForGuarantee,
1670 me.prefetchPage(endPage, {
1672 callback: me.onWaitForGuarantee,
1676 // Request was already satisfied via the prefetch
1678 me.onGuaranteedRange();
1682 // because prefetchData is stored by index
1683 // this invalidates all of the prefetchedData
1686 prefetchData = me.prefetchData,
1693 if (me.remoteSort) {
1694 prefetchData.clear();
1695 me.callParent(arguments);
1697 sorters = me.getSorters();
1698 start = me.guaranteedStart;
1699 end = me.guaranteedEnd;
1701 if (sorters.length) {
1702 prefetchData.sort(sorters);
1703 range = prefetchData.getRange();
1704 prefetchData.clear();
1705 me.cacheRecords(range);
1706 delete me.guaranteedStart;
1707 delete me.guaranteedEnd;
1708 me.guaranteeRange(start, end);
1710 me.callParent(arguments);
1713 me.callParent(arguments);
1717 // overriden to provide striping of the indexes as sorting occurs.
1718 // this cannot be done inside of sort because datachanged has already
1719 // fired and will trigger a repaint of the bound view.
1720 doSort: function(sorterFn) {
1722 if (me.remoteSort) {
1723 //the load function will pick up the new sorters and request the sorted data from the proxy
1726 me.data.sortBy(sorterFn);
1728 var range = me.getRange(),
1731 for (; i < ln; i++) {
1735 me.fireEvent('datachanged', me);
1739 <span id='Ext-data-Store-method-find'> /**
1740 </span> * Finds the index of the first matching Record in this store by a specific field value.
1741 * @param {String} fieldName The name of the Record field to test.
1742 * @param {String/RegExp} value Either a string that the field value
1743 * should begin with, or a RegExp to test against the field.
1744 * @param {Number} startIndex (optional) The index to start searching at
1745 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
1746 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison
1747 * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false.
1748 * @return {Number} The matched index or -1
1750 find: function(property, value, start, anyMatch, caseSensitive, exactMatch) {
1751 var fn = this.createFilterFn(property, value, anyMatch, caseSensitive, exactMatch);
1752 return fn ? this.data.findIndexBy(fn, null, start) : -1;
1755 <span id='Ext-data-Store-method-findRecord'> /**
1756 </span> * Finds the first matching Record in this store by a specific field value.
1757 * @param {String} fieldName The name of the Record field to test.
1758 * @param {String/RegExp} value Either a string that the field value
1759 * should begin with, or a RegExp to test against the field.
1760 * @param {Number} startIndex (optional) The index to start searching at
1761 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
1762 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison
1763 * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false.
1764 * @return {Ext.data.Model} The matched record or null
1766 findRecord: function() {
1768 index = me.find.apply(me, arguments);
1769 return index !== -1 ? me.getAt(index) : null;
1772 <span id='Ext-data-Store-method-createFilterFn'> /**
1774 * Returns a filter function used to test a the given property's value. Defers most of the work to
1775 * Ext.util.MixedCollection's createValueMatcher function
1776 * @param {String} property The property to create the filter function for
1777 * @param {String/RegExp} value The string/regex to compare the property value to
1778 * @param {Boolean} anyMatch True if we don't care if the filter value is not the full value (defaults to false)
1779 * @param {Boolean} caseSensitive True to create a case-sensitive regex (defaults to false)
1780 * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false.
1781 * Ignored if anyMatch is true.
1783 createFilterFn: function(property, value, anyMatch, caseSensitive, exactMatch) {
1784 if (Ext.isEmpty(value)) {
1787 value = this.data.createValueMatcher(value, anyMatch, caseSensitive, exactMatch);
1788 return function(r) {
1789 return value.test(r.data[property]);
1793 <span id='Ext-data-Store-method-findExact'> /**
1794 </span> * Finds the index of the first matching Record in this store by a specific field value.
1795 * @param {String} fieldName The name of the Record field to test.
1796 * @param {Mixed} value The value to match the field against.
1797 * @param {Number} startIndex (optional) The index to start searching at
1798 * @return {Number} The matched index or -1
1800 findExact: function(property, value, start) {
1801 return this.data.findIndexBy(function(rec) {
1802 return rec.get(property) === value;
1807 <span id='Ext-data-Store-method-findBy'> /**
1808 </span> * Find the index of the first matching Record in this Store by a function.
1809 * If the function returns <tt>true</tt> it is considered a match.
1810 * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
1811 * <li><b>record</b> : Ext.data.Model<p class="sub-desc">The {@link Ext.data.Model record}
1812 * to test for filtering. Access field values using {@link Ext.data.Model#get}.</p></li>
1813 * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
1815 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
1816 * @param {Number} startIndex (optional) The index to start searching at
1817 * @return {Number} The matched index or -1
1819 findBy: function(fn, scope, start) {
1820 return this.data.findIndexBy(fn, scope, start);
1823 <span id='Ext-data-Store-method-collect'> /**
1824 </span> * Collects unique values for a particular dataIndex from this store.
1825 * @param {String} dataIndex The property to collect
1826 * @param {Boolean} allowNull (optional) Pass true to allow null, undefined or empty string values
1827 * @param {Boolean} bypassFilter (optional) Pass true to collect from all records, even ones which are filtered
1828 * @return {Array} An array of the unique values
1830 collect: function(dataIndex, allowNull, bypassFilter) {
1832 data = (bypassFilter === true && me.snapshot) ? me.snapshot: me.data;
1834 return data.collect(dataIndex, 'data', allowNull);
1837 <span id='Ext-data-Store-method-getCount'> /**
1838 </span> * Gets the number of cached records.
1839 * <p>If using paging, this may not be the total size of the dataset. If the data object
1840 * used by the Reader contains the dataset size, then the {@link #getTotalCount} function returns
1841 * the dataset size. <b>Note</b>: see the Important note in {@link #load}.</p>
1842 * @return {Number} The number of Records in the Store's cache.
1844 getCount: function() {
1845 return this.data.length || 0;
1848 <span id='Ext-data-Store-method-getTotalCount'> /**
1849 </span> * Returns the total number of {@link Ext.data.Model Model} instances that the {@link Ext.data.proxy.Proxy Proxy}
1850 * indicates exist. This will usually differ from {@link #getCount} when using paging - getCount returns the
1851 * number of records loaded into the Store at the moment, getTotalCount returns the number of records that
1852 * could be loaded into the Store if the Store contained all data
1853 * @return {Number} The total number of Model instances available via the Proxy
1855 getTotalCount: function() {
1856 return this.totalCount;
1859 <span id='Ext-data-Store-method-getAt'> /**
1860 </span> * Get the Record at the specified index.
1861 * @param {Number} index The index of the Record to find.
1862 * @return {Ext.data.Model} The Record at the passed index. Returns undefined if not found.
1864 getAt: function(index) {
1865 return this.data.getAt(index);
1868 <span id='Ext-data-Store-method-getRange'> /**
1869 </span> * Returns a range of Records between specified indices.
1870 * @param {Number} startIndex (optional) The starting index (defaults to 0)
1871 * @param {Number} endIndex (optional) The ending index (defaults to the last Record in the Store)
1872 * @return {Ext.data.Model[]} An array of Records
1874 getRange: function(start, end) {
1875 return this.data.getRange(start, end);
1878 <span id='Ext-data-Store-method-getById'> /**
1879 </span> * Get the Record with the specified id.
1880 * @param {String} id The id of the Record to find.
1881 * @return {Ext.data.Model} The Record with the passed id. Returns undefined if not found.
1883 getById: function(id) {
1884 return (this.snapshot || this.data).findBy(function(record) {
1885 return record.getId() === id;
1889 <span id='Ext-data-Store-method-indexOf'> /**
1890 </span> * Get the index within the cache of the passed Record.
1891 * @param {Ext.data.Model} record The Ext.data.Model object to find.
1892 * @return {Number} The index of the passed Record. Returns -1 if not found.
1894 indexOf: function(record) {
1895 return this.data.indexOf(record);
1899 <span id='Ext-data-Store-method-indexOfTotal'> /**
1900 </span> * Get the index within the entire dataset. From 0 to the totalCount.
1901 * @param {Ext.data.Model} record The Ext.data.Model object to find.
1902 * @return {Number} The index of the passed Record. Returns -1 if not found.
1904 indexOfTotal: function(record) {
1905 return record.index || this.indexOf(record);
1908 <span id='Ext-data-Store-method-indexOfId'> /**
1909 </span> * Get the index within the cache of the Record with the passed id.
1910 * @param {String} id The id of the Record to find.
1911 * @return {Number} The index of the Record. Returns -1 if not found.
1913 indexOfId: function(id) {
1914 return this.data.indexOfKey(id);
1917 <span id='Ext-data-Store-method-removeAll'> /**
1918 </span> * Remove all items from the store.
1919 * @param {Boolean} silent Prevent the `clear` event from being fired.
1921 removeAll: function(silent) {
1926 me.snapshot.clear();
1928 if (silent !== true) {
1929 me.fireEvent('clear', me);
1934 * Aggregation methods
1937 <span id='Ext-data-Store-method-first'> /**
1938 </span> * Convenience function for getting the first model instance in the store
1939 * @param {Boolean} grouped (Optional) True to perform the operation for each group
1940 * in the store. The value returned will be an object literal with the key being the group
1941 * name and the first record being the value. The grouped parameter is only honored if
1942 * the store has a groupField.
1943 * @return {Ext.data.Model/undefined} The first model instance in the store, or undefined
1945 first: function(grouped) {
1948 if (grouped && me.isGrouped()) {
1949 return me.aggregate(function(records) {
1950 return records.length ? records[0] : undefined;
1953 return me.data.first();
1957 <span id='Ext-data-Store-method-last'> /**
1958 </span> * Convenience function for getting the last model instance in the store
1959 * @param {Boolean} grouped (Optional) True to perform the operation for each group
1960 * in the store. The value returned will be an object literal with the key being the group
1961 * name and the last record being the value. The grouped parameter is only honored if
1962 * the store has a groupField.
1963 * @return {Ext.data.Model/undefined} The last model instance in the store, or undefined
1965 last: function(grouped) {
1968 if (grouped && me.isGrouped()) {
1969 return me.aggregate(function(records) {
1970 var len = records.length;
1971 return len ? records[len - 1] : undefined;
1974 return me.data.last();
1978 <span id='Ext-data-Store-method-sum'> /**
1979 </span> * Sums the value of <tt>property</tt> for each {@link Ext.data.Model record} between <tt>start</tt>
1980 * and <tt>end</tt> and returns the result.
1981 * @param {String} field A field in each record
1982 * @param {Boolean} grouped (Optional) True to perform the operation for each group
1983 * in the store. The value returned will be an object literal with the key being the group
1984 * name and the sum for that group being the value. The grouped parameter is only honored if
1985 * the store has a groupField.
1986 * @return {Number} The sum
1988 sum: function(field, grouped) {
1991 if (grouped && me.isGrouped()) {
1992 return me.aggregate(me.getSum, me, true, [field]);
1994 return me.getSum(me.data.items, field);
1998 // @private, see sum
1999 getSum: function(records, field) {
2002 len = records.length;
2004 for (; i < len; ++i) {
2005 total += records[i].get(field);
2011 <span id='Ext-data-Store-method-count'> /**
2012 </span> * Gets the count of items in the store.
2013 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2014 * in the store. The value returned will be an object literal with the key being the group
2015 * name and the count for each group being the value. The grouped parameter is only honored if
2016 * the store has a groupField.
2017 * @return {Number} the count
2019 count: function(grouped) {
2022 if (grouped && me.isGrouped()) {
2023 return me.aggregate(function(records) {
2024 return records.length;
2027 return me.getCount();
2031 <span id='Ext-data-Store-method-min'> /**
2032 </span> * Gets the minimum value in the store.
2033 * @param {String} field The field in each record
2034 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2035 * in the store. The value returned will be an object literal with the key being the group
2036 * name and the minimum in the group being the value. The grouped parameter is only honored if
2037 * the store has a groupField.
2038 * @return {Mixed/undefined} The minimum value, if no items exist, undefined.
2040 min: function(field, grouped) {
2043 if (grouped && me.isGrouped()) {
2044 return me.aggregate(me.getMin, me, true, [field]);
2046 return me.getMin(me.data.items, field);
2050 // @private, see min
2051 getMin: function(records, field){
2053 len = records.length,
2057 min = records[0].get(field);
2060 for (; i < len; ++i) {
2061 value = records[i].get(field);
2062 if (value < min) {
2069 <span id='Ext-data-Store-method-max'> /**
2070 </span> * Gets the maximum value in the store.
2071 * @param {String} field The field in each record
2072 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2073 * in the store. The value returned will be an object literal with the key being the group
2074 * name and the maximum in the group being the value. The grouped parameter is only honored if
2075 * the store has a groupField.
2076 * @return {Mixed/undefined} The maximum value, if no items exist, undefined.
2078 max: function(field, grouped) {
2081 if (grouped && me.isGrouped()) {
2082 return me.aggregate(me.getMax, me, true, [field]);
2084 return me.getMax(me.data.items, field);
2088 // @private, see max
2089 getMax: function(records, field) {
2091 len = records.length,
2096 max = records[0].get(field);
2099 for (; i < len; ++i) {
2100 value = records[i].get(field);
2101 if (value > max) {
2108 <span id='Ext-data-Store-method-average'> /**
2109 </span> * Gets the average value in the store.
2110 * @param {String} field The field in each record
2111 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2112 * in the store. The value returned will be an object literal with the key being the group
2113 * name and the group average being the value. The grouped parameter is only honored if
2114 * the store has a groupField.
2115 * @return {Mixed/undefined} The average value, if no items exist, 0.
2117 average: function(field, grouped) {
2119 if (grouped && me.isGrouped()) {
2120 return me.aggregate(me.getAverage, me, true, [field]);
2122 return me.getAverage(me.data.items, field);
2126 // @private, see average
2127 getAverage: function(records, field) {
2129 len = records.length,
2132 if (records.length > 0) {
2133 for (; i < len; ++i) {
2134 sum += records[i].get(field);
2141 <span id='Ext-data-Store-method-aggregate'> /**
2142 </span> * Runs the aggregate function for all the records in the store.
2143 * @param {Function} fn The function to execute. The function is called with a single parameter,
2144 * an array of records for that group.
2145 * @param {Object} scope (optional) The scope to execute the function in. Defaults to the store.
2146 * @param {Boolean} grouped (Optional) True to perform the operation for each group
2147 * in the store. The value returned will be an object literal with the key being the group
2148 * name and the group average being the value. The grouped parameter is only honored if
2149 * the store has a groupField.
2150 * @param {Array} args (optional) Any arguments to append to the function call
2151 * @return {Object} An object literal with the group names and their appropriate values.
2153 aggregate: function(fn, scope, grouped, args) {
2155 if (grouped && this.isGrouped()) {
2156 var groups = this.getGroups(),
2158 len = groups.length,
2162 for (; i < len; ++i) {
2164 out[group.name] = fn.apply(scope || this, [group.children].concat(args));
2168 return fn.apply(scope || this, [this.data.items].concat(args));