-<html>\r
-<head>\r
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> \r
- <title>The source code</title>\r
- <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
- <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
-</head>\r
-<body onload="prettyPrint();">\r
- <pre class="prettyprint lang-js"><div id="cls-Ext.data.Store"></div>/**
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>The source code</title>
+ <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
+ <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
+</head>
+<body onload="prettyPrint();">
+ <pre class="prettyprint lang-js">/*!
+ * Ext JS Library 3.2.0
+ * Copyright(c) 2006-2010 Ext JS, Inc.
+ * licensing@extjs.com
+ * http://www.extjs.com/license
+ */
+<div id="cls-Ext.data.Store"></div>/**
* @class Ext.data.Store
* @extends Ext.util.Observable
* <p>The Store class encapsulates a client side cache of {@link Ext.data.Record Record}
dir : 'dir'
},
+ <div id="prop-Ext.data.Store-isDestroyed"></div>/**
+ * @property isDestroyed
+ * @type Boolean
+ * True if the store has been destroyed already. Read only
+ */
+ isDestroyed: false,
+
+ <div id="prop-Ext.data.Store-hasMultiSort"></div>/**
+ * @property hasMultiSort
+ * @type Boolean
+ * True if this store is currently sorted by more than one field/direction combination.
+ */
+ hasMultiSort: false,
+
// private
batchKey : '_ext_batch_',
this.data.getKey = function(o){
return o.id;
};
- <div id="prop-Ext.data.Store-baseParams"></div>/**
- * See the <code>{@link #baseParams corresponding configuration option}</code>
- * for a description of this property.
- * To modify this property see <code>{@link #setBaseParam}</code>.
- * @property
- */
- this.baseParams = {};
+
// temporary removed-records cache
this.removed = [];
Ext.apply(this, config);
+ <div id="prop-Ext.data.Store-baseParams"></div>/**
+ * See the <code>{@link #baseParams corresponding configuration option}</code>
+ * for a description of this property.
+ * To modify this property see <code>{@link #setBaseParam}</code>.
+ * @property
+ */
+ this.baseParams = Ext.isObject(this.baseParams) ? this.baseParams : {};
+
this.paramNames = Ext.applyIf(this.paramNames || {}, this.defaultParamNames);
if((this.url || this.api) && !this.proxy){
* @event beforewrite
* @param {Ext.data.Store} store
* @param {String} action [Ext.data.Api.actions.create|update|destroy]
- * @param {Record/Array[Record]} rs
+ * @param {Record/Record[]} rs The Record(s) being written.
* @param {Object} options The loading options that were specified. Edit <code>options.params</code> to add Http parameters to the request. (see {@link #save} for details)
* @param {Object} arg The callback's arg object passed to the {@link #request} function
*/
* @private
*/
buildWriter : function(config) {
- var klass = undefined;
- type = (config.format || 'json').toLowerCase();
+ var klass = undefined,
+ type = (config.format || 'json').toLowerCase();
switch (type) {
case 'json':
klass = Ext.data.JsonWriter;
* <tt>false</tt>, the load call will abort and will return <tt>false</tt>; otherwise will return <tt>true</tt>.
*/
load : function(options) {
- options = options || {};
+ options = Ext.apply({}, options);
this.storeOptions(options);
if(this.sortInfo && this.remoteSort){
var pn = this.paramNames;
- options.params = options.params || {};
+ options.params = Ext.apply({}, options.params);
options.params[pn.sort] = this.sortInfo.field;
options.params[pn.dir] = this.sortInfo.direction;
}
},
/**
- * Destroys a record or records. Should not be used directly. It's called by Store#remove if a Writer is set.
- * @param {Store} this
- * @param {Ext.data.Record/Ext.data.Record[]}
+ * Destroys a Record. Should not be used directly. It's called by Store#remove if a Writer is set.
+ * @param {Store} store this
+ * @param {Ext.data.Record} record
* @param {Number} index
* @private
*/
var doRequest = true;
if (action === 'read') {
- Ext.applyIf(options.params, this.baseParams);
doRequest = this.fireEvent('beforeload', this, options);
+ Ext.applyIf(options.params, this.baseParams);
}
else {
// if Writer is configured as listful, force single-record rs to be [{}] instead of {}
id: batch,
count: 0,
data: {}
- }
+ };
}
++o.count;
},
return this.sortInfo;
},
- // private
+ /**
+ * @private
+ * Invokes sortData if we have sortInfo to sort on and are not sorting remotely
+ */
applySort : function(){
- if(this.sortInfo && !this.remoteSort){
- var s = this.sortInfo, f = s.field;
- this.sortData(f, s.direction);
+ if ((this.sortInfo || this.multiSortInfo) && !this.remoteSort) {
+ this.sortData();
}
},
- // private
- sortData : function(f, direction){
- direction = direction || 'ASC';
- var st = this.fields.get(f).sortType;
- var fn = function(r1, r2){
- var v1 = st(r1.data[f]), v2 = st(r2.data[f]);
- return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
+ /**
+ * @private
+ * Performs the actual sorting of data. This checks to see if we currently have a multi sort or not. It applies
+ * each sorter field/direction pair in turn by building an OR'ed master sorting function and running it against
+ * the full dataset
+ */
+ sortData : function() {
+ var sortInfo = this.hasMultiSort ? this.multiSortInfo : this.sortInfo,
+ direction = sortInfo.direction || "ASC",
+ sorters = sortInfo.sorters,
+ sortFns = [];
+
+ //if we just have a single sorter, pretend it's the first in an array
+ if (!this.hasMultiSort) {
+ sorters = [{direction: direction, field: sortInfo.field}];
+ }
+
+ //create a sorter function for each sorter field/direction combo
+ for (var i=0, j = sorters.length; i < j; i++) {
+ sortFns.push(this.createSortFunction(sorters[i].field, sorters[i].direction));
+ }
+
+ if (sortFns.length == 0) {
+ return;
+ }
+
+ //the direction modifier is multiplied with the result of the sorting functions to provide overall sort direction
+ //(as opposed to direction per field)
+ var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1;
+
+ //create a function which ORs each sorter together to enable multi-sort
+ var fn = function(r1, r2) {
+ var result = sortFns[0].call(this, r1, r2);
+
+ //if we have more than one sorter, OR any additional sorter functions together
+ if (sortFns.length > 1) {
+ for (var i=1, j = sortFns.length; i < j; i++) {
+ result = result || sortFns[i].call(this, r1, r2);
+ }
+ }
+
+ return directionModifier * result;
};
+
+ //sort the data
this.data.sort(direction, fn);
- if(this.snapshot && this.snapshot != this.data){
+ if (this.snapshot && this.snapshot != this.data) {
this.snapshot.sort(direction, fn);
}
},
+ <div id="method-Ext.data.Store-createSortFunction"></div>/**
+ * Creates and returns a function which sorts an array by the given field and direction
+ * @param {String} field The field to create the sorter for
+ * @param {String} direction The direction to sort by (defaults to "ASC")
+ * @return {Function} A function which sorts by the field/direction combination provided
+ */
+ createSortFunction: function(field, direction) {
+ direction = direction || "ASC";
+ var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1;
+
+ var sortType = this.fields.get(field).sortType;
+
+ //create a comparison function. Takes 2 records, returns 1 if record 1 is greater,
+ //-1 if record 2 is greater or 0 if they are equal
+ return function(r1, r2) {
+ var v1 = sortType(r1.data[field]),
+ v2 = sortType(r2.data[field]);
+
+ return directionModifier * (v1 > v2 ? 1 : (v1 < v2 ? -1 : 0));
+ };
+ },
+
<div id="method-Ext.data.Store-setDefaultSort"></div>/**
* Sets the default sort column and order to be used by the next {@link #load} operation.
* @param {String} fieldName The name of the field to sort by.
* @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>)
*/
- setDefaultSort : function(field, dir){
+ setDefaultSort : function(field, dir) {
dir = dir ? dir.toUpperCase() : 'ASC';
this.sortInfo = {field: field, direction: dir};
this.sortToggle[field] = dir;
* Sort the Records.
* If remote sorting is used, the sort is performed on the server, and the cache is reloaded. If local
* sorting is used, the cache is sorted internally. See also {@link #remoteSort} and {@link #paramNames}.
- * @param {String} fieldName The name of the field to sort by.
+ * This function accepts two call signatures - pass in a field name as the first argument to sort on a single
+ * field, or pass in an array of sort configuration objects to sort by multiple fields.
+ * Single sort example:
+ * store.sort('name', 'ASC');
+ * Multi sort example:
+ * store.sort([
+ * {
+ * field : 'name',
+ * direction: 'ASC'
+ * },
+ * {
+ * field : 'salary',
+ * direction: 'DESC'
+ * }
+ * ], 'ASC');
+ * In this second form, the sort configs are applied in order, with later sorters sorting within earlier sorters' results.
+ * For example, if two records with the same name are present they will also be sorted by salary if given the sort configs
+ * above. Any number of sort configs can be added.
+ * @param {String/Array} fieldName The name of the field to sort by, or an array of ordered sort configs
* @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>)
*/
- sort : function(fieldName, dir){
- var f = this.fields.get(fieldName);
- if(!f){
- return false;
+ sort : function(fieldName, dir) {
+ if (Ext.isArray(arguments[0])) {
+ return this.multiSort.call(this, fieldName, dir);
+ } else {
+ return this.singleSort(fieldName, dir);
}
- if(!dir){
- if(this.sortInfo && this.sortInfo.field == f.name){ // toggle sort dir
- dir = (this.sortToggle[f.name] || 'ASC').toggle('ASC', 'DESC');
- }else{
- dir = f.sortDir;
+ },
+
+ <div id="method-Ext.data.Store-singleSort"></div>/**
+ * Sorts the store contents by a single field and direction. This is called internally by {@link sort} and would
+ * not usually be called manually
+ * @param {String} fieldName The name of the field to sort by.
+ * @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>)
+ */
+ singleSort: function(fieldName, dir) {
+ var field = this.fields.get(fieldName);
+ if (!field) return false;
+
+ var name = field.name,
+ sortInfo = this.sortInfo || null,
+ sortToggle = this.sortToggle ? this.sortToggle[name] : null;
+
+ if (!dir) {
+ if (sortInfo && sortInfo.field == name) { // toggle sort dir
+ dir = (this.sortToggle[name] || 'ASC').toggle('ASC', 'DESC');
+ } else {
+ dir = field.sortDir;
}
}
- var st = (this.sortToggle) ? this.sortToggle[f.name] : null;
- var si = (this.sortInfo) ? this.sortInfo : null;
- this.sortToggle[f.name] = dir;
- this.sortInfo = {field: f.name, direction: dir};
- if(!this.remoteSort){
- this.applySort();
- this.fireEvent('datachanged', this);
- }else{
+ this.sortToggle[name] = dir;
+ this.sortInfo = {field: name, direction: dir};
+ this.hasMultiSort = false;
+
+ if (this.remoteSort) {
if (!this.load(this.lastOptions)) {
- if (st) {
- this.sortToggle[f.name] = st;
+ if (sortToggle) {
+ this.sortToggle[name] = sortToggle;
}
- if (si) {
- this.sortInfo = si;
+ if (sortInfo) {
+ this.sortInfo = sortInfo;
}
}
+ } else {
+ this.applySort();
+ this.fireEvent('datachanged', this);
+ }
+ },
+
+ <div id="method-Ext.data.Store-multiSort"></div>/**
+ * Sorts the contents of this store by multiple field/direction sorters. This is called internally by {@link sort}
+ * and would not usually be called manually.
+ * Multi sorting only currently applies to local datasets - multiple sort data is not currently sent to a proxy
+ * if remoteSort is used.
+ * @param {Array} sorters Array of sorter objects (field and direction)
+ * @param {String} direction Overall direction to sort the ordered results by (defaults to "ASC")
+ */
+ multiSort: function(sorters, direction) {
+ this.hasMultiSort = true;
+ direction = direction || "ASC";
+
+ //toggle sort direction
+ if (this.multiSortInfo && direction == this.multiSortInfo.direction) {
+ direction = direction.toggle("ASC", "DESC");
+ }
+
+ <div id="prop-Ext.data.Store-multiSortInfo"></div>/**
+ * @property multiSortInfo
+ * @type Object
+ * Object containing overall sort direction and an ordered array of sorter configs used when sorting on multiple fields
+ */
+ this.multiSortInfo = {
+ sorters : sorters,
+ direction: direction
+ };
+
+ if (this.remoteSort) {
+ this.singleSort(sorters[0].field, sorters[0].direction);
+
+ } else {
+ this.applySort();
+ this.fireEvent('datachanged', this);
}
},
return this.modified;
},
- // private
- createFilterFn : function(property, value, anyMatch, caseSensitive){
- if(Ext.isEmpty(value, false)){
- return false;
- }
- value = this.data.createValueMatcher(value, anyMatch, caseSensitive);
- return function(r){
- return value.test(r.data[property]);
- };
- },
-
<div id="method-Ext.data.Store-sum"></div>/**
* Sums the value of <tt>property</tt> for each {@link Ext.data.Record record} between <tt>start</tt>
* and <tt>end</tt> and returns the result.
return v;
},
+ /**
+ * @private
+ * Returns a filter function used to test a the given property's value. Defers most of the work to
+ * Ext.util.MixedCollection's createValueMatcher function
+ * @param {String} property The property to create the filter function for
+ * @param {String/RegExp} value The string/regex to compare the property value to
+ * @param {Boolean} anyMatch True if we don't care if the filter value is not the full value (defaults to false)
+ * @param {Boolean} caseSensitive True to create a case-sensitive regex (defaults to false)
+ * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
+ */
+ createFilterFn : function(property, value, anyMatch, caseSensitive, exactMatch){
+ if(Ext.isEmpty(value, false)){
+ return false;
+ }
+ value = this.data.createValueMatcher(value, anyMatch, caseSensitive, exactMatch);
+ return function(r) {
+ return value.test(r.data[property]);
+ };
+ },
+
+ <div id="method-Ext.data.Store-createMultipleFilterFn"></div>/**
+ * Given an array of filter functions (each with optional scope), constructs and returns a single function that returns
+ * the result of all of the filters ANDed together
+ * @param {Array} filters The array of filter objects (each object should contain an 'fn' and optional scope)
+ * @return {Function} The multiple filter function
+ */
+ createMultipleFilterFn: function(filters) {
+ return function(record) {
+ var isMatch = true;
+
+ for (var i=0, j = filters.length; i < j; i++) {
+ var filter = filters[i],
+ fn = filter.fn,
+ scope = filter.scope;
+
+ isMatch = isMatch && fn.call(scope, record);
+ }
+
+ return isMatch;
+ };
+ },
+
<div id="method-Ext.data.Store-filter"></div>/**
- * Filter the {@link Ext.data.Record records} by a specified property.
- * @param {String} field A field on your records
+ * Filter the {@link Ext.data.Record records} by a specified property. Alternatively, pass an array of filter
+ * options to filter by more than one property.
+ * Single filter example:
+ * store.filter('name', 'Ed', true, true); //finds all records containing the substring 'Ed'
+ * Multiple filter example:
+ * store.filter([
+ * {
+ * property : 'name',
+ * value : 'Ed',
+ * anyMatch : true, //optional, defaults to true
+ * caseSensitive: true //optional, defaults to true
+ * },
+ *
+ * //filter functions can also be passed
+ * {
+ * fn : function(record) {
+ * return record.get('age') == 24
+ * },
+ * scope: this
+ * }
+ * ]);
+ * @param {String|Array} field A field on your records, or an array containing multiple filter options
* @param {String/RegExp} value Either a string that the field should begin with, or a RegExp to test
* against the field.
* @param {Boolean} anyMatch (optional) <tt>true</tt> to match any part not just the beginning
* @param {Boolean} caseSensitive (optional) <tt>true</tt> for case sensitive comparison
+ * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
*/
- filter : function(property, value, anyMatch, caseSensitive){
- var fn = this.createFilterFn(property, value, anyMatch, caseSensitive);
+ filter : function(property, value, anyMatch, caseSensitive, exactMatch){
+ //we can accept an array of filter objects, or a single filter object - normalize them here
+ if (Ext.isObject(property)) {
+ property = [property];
+ }
+
+ if (Ext.isArray(property)) {
+ var filters = [];
+
+ //normalize the filters passed into an array of filter functions
+ for (var i=0, j = property.length; i < j; i++) {
+ var filter = property[i],
+ func = filter.fn,
+ scope = filter.scope || this;
+
+ //if we weren't given a filter function, construct one now
+ if (!Ext.isFunction(func)) {
+ func = this.createFilterFn(filter.property, filter.value, filter.anyMatch, filter.caseSensitive, filter.exactMatch);
+ }
+
+ filters.push({fn: func, scope: scope});
+ }
+
+ var fn = this.createMultipleFilterFn(filters);
+ } else {
+ //classic single property filter
+ var fn = this.createFilterFn(property, value, anyMatch, caseSensitive, exactMatch);
+ }
+
return fn ? this.filterBy(fn) : this.clearFilter();
},
this.fireEvent('datachanged', this);
},
+ <div id="method-Ext.data.Store-clearFilter"></div>/**
+ * Revert to a view of the Record cache with no filtering applied.
+ * @param {Boolean} suppressEvent If <tt>true</tt> the filter is cleared silently without firing the
+ * {@link #datachanged} event.
+ */
+ clearFilter : function(suppressEvent){
+ if(this.isFiltered()){
+ this.data = this.snapshot;
+ delete this.snapshot;
+ if(suppressEvent !== true){
+ this.fireEvent('datachanged', this);
+ }
+ }
+ },
+
+ <div id="method-Ext.data.Store-isFiltered"></div>/**
+ * Returns true if this store is currently filtered
+ * @return {Boolean}
+ */
+ isFiltered : function(){
+ return !!this.snapshot && this.snapshot != this.data;
+ },
+
<div id="method-Ext.data.Store-query"></div>/**
* Query the records by a specified property.
* @param {String} field A field on your records
return r;
},
- <div id="method-Ext.data.Store-clearFilter"></div>/**
- * Revert to a view of the Record cache with no filtering applied.
- * @param {Boolean} suppressEvent If <tt>true</tt> the filter is cleared silently without firing the
- * {@link #datachanged} event.
- */
- clearFilter : function(suppressEvent){
- if(this.isFiltered()){
- this.data = this.snapshot;
- delete this.snapshot;
- if(suppressEvent !== true){
- this.fireEvent('datachanged', this);
- }
- }
- },
-
- <div id="method-Ext.data.Store-isFiltered"></div>/**
- * Returns true if this store is currently filtered
- * @return {Boolean}
- */
- isFiltered : function(){
- return this.snapshot && this.snapshot != this.data;
- },
-
// private
afterEdit : function(record){
if(this.modified.indexOf(record) == -1){
'writer-undefined' : 'Attempted to execute a write-action without a DataWriter installed.'
}
});
-</pre> \r
-</body>\r
+</pre>
+</body>
</html>
\ No newline at end of file