📄 store.js
字号:
// private
// Called as a callback by the Reader during a load operation.
loadRecords : function(o, options, success){
if(!o || success === false){
if(success !== false){
this.fireEvent("load", this, [], options);
}
if(options.callback){
options.callback.call(options.scope || this, [], options, false);
}
return;
}
var r = o.records, t = o.totalRecords || r.length;
if(!options || options.add !== true){
for(var i = 0, len = r.length; i < len; i++){
r[i].join(this);
}
this.data.clear();
this.data.addAll(r);
this.totalLength = t;
this.applySort();
this.fireEvent("datachanged", this);
}else{
this.totalLength = Math.max(t, this.data.length+r.length);
this.add(r);
}
this.fireEvent("load", this, r, options);
if(options.callback){
options.callback.call(options.scope || this, r, options, true);
}
},
/**
* Loads data from a passed data block. A Reader which understands the format of the data
* must have been configured in the constructor.
* @param {Object} data The data block from which to read the Records. The format of the data expected
* is dependent on the type of Reader that is configured and should correspond to that Reader's readRecords parameter.
* @param {Boolean} append (Optional) True to append the new Records rather than replace the existing cache.
*/
loadData : function(o, append){
var r = this.reader.readRecords(o);
this.loadRecords(r, {add: append}, true);
},
/**
* Gets the number of cached records.
* <p>
* <em>If using paging, this may not be the total size of the dataset. If the data object
* used by the Reader contains the dataset size, then the getTotalCount() function returns
* the data set size</em>
*/
getCount : function(){
return this.data.length || 0;
},
/**
* Gets the total number of records in the dataset.
* <p>
* <em>If using paging, for this to be accurate, the data object used by the Reader must contain
* the dataset size</em>
*/
getTotalCount : function(){
return this.totalLength || 0;
},
/**
* Returns the sort state of the Store as an object with two properties:
* <pre><code>
field {String} The name of the field by which the Records are sorted
direction {String} The sort order, "ASC" or "DESC"
* </code></pre>
*/
getSortState : function(){
return this.sortInfo;
},
// private
applySort : function(){
if(this.sortInfo && !this.remoteSort){
var s = this.sortInfo, f = s.field;
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);
};
this.data.sort(s.direction, fn);
if(this.snapshot && this.snapshot != this.data){
this.snapshot.sort(s.direction, fn);
}
}
},
/**
* Sets the default sort column and order to be used by the next load operation.
* @param {String} fieldName The name of the field to sort by.
* @param {String} dir (optional) The sort order, "ASC" or "DESC" (defaults to "ASC")
*/
setDefaultSort : function(field, dir){
this.sortInfo = {field: field, direction: dir ? dir.toUpperCase() : "ASC"};
},
/**
* 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.
* @param {String} fieldName The name of the field to sort by.
* @param {String} dir (optional) The sort order, "ASC" or "DESC" (defaults to "ASC")
*/
sort : function(fieldName, dir){
var f = this.fields.get(fieldName);
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;
}
}
this.sortToggle[f.name] = dir;
this.sortInfo = {field: f.name, direction: dir};
if(!this.remoteSort){
this.applySort();
this.fireEvent("datachanged", this);
}else{
this.load(this.lastOptions);
}
},
/**
* Calls the specified function for each of the Records in the cache.
* @param {Function} fn The function to call. The Record is passed as the first parameter.
* Returning <em>false</em> aborts and exits the iteration.
* @param {Object} scope (optional) The scope in which to call the function (defaults to the Record).
*/
each : function(fn, scope){
this.data.each(fn, scope);
},
/**
* Get all records modified since the last load, or since the last commit.
* @return {Ext.data.Record[]} An array of Records containing outstanding modifications.
*/
getModifiedRecords : function(){
return this.modified;
},
// private
createFilterFn : function(property, value, anyMatch){
if(!value.exec){ // not a regex
value = String(value);
if(value.length == 0){
return false;
}
value = new RegExp((anyMatch === true ? '' : '^') + Ext.escapeRe(value), "i");
}
return function(r){
return value.test(r.data[property]);
};
},
/**
* Sums the value of <i>property</i> for each record between start and end and returns the result.
* @param {String} property A field on your records
* @param {Number} start The record index to start at (defaults to 0)
* @param {Number} end The last record index to include (defaults to length - 1)
* @return {Number} The sum
*/
sum : function(property, start, end){
var rs = this.data.items, v = 0;
start = start || 0;
end = (end || end === 0) ? end : rs.length-1;
for(var i = start; i <= end; i++){
v += (rs[i].data[property] || 0);
}
return v;
},
/**
* Filter the records by a specified property.
* @param {String} field A field on your records
* @param {String/RegExp} value Either a string that the field
* should start with or a RegExp to test against the field
* @param {Boolean} anyMatch True to match any part not just the beginning
*/
filter : function(property, value, anyMatch){
var fn = this.createFilterFn(property, value, anyMatch);
return fn ? this.filterBy(fn) : this.clearFilter();
},
/**
* Filter by a function. The specified function will be called with each
* record in this data source. If the function returns true the record is included,
* otherwise it is filtered.
* @param {Function} fn The function to be called, it will receive 2 args (record, id)
* @param {Object} scope (optional) The scope of the function (defaults to this)
*/
filterBy : function(fn, scope){
this.snapshot = this.snapshot || this.data;
this.data = this.queryBy(fn, scope||this);
this.fireEvent("datachanged", this);
},
/**
* Query the records by a specified property.
* @param {String} field A field on your records
* @param {String/RegExp} value Either a string that the field
* should start with or a RegExp to test against the field
* @param {Boolean} anyMatch True to match any part not just the beginning
* @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records
*/
query : function(property, value, anyMatch){
var fn = this.createFilterFn(property, value, anyMatch);
return fn ? this.queryBy(fn) : this.data.clone();
},
/**
* Query by a function. The specified function will be called with each
* record in this data source. If the function returns true the record is included
* in the results.
* @param {Function} fn The function to be called, it will receive 2 args (record, id)
* @param {Object} scope (optional) The scope of the function (defaults to this)
@return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records
**/
queryBy : function(fn, scope){
var data = this.snapshot || this.data;
return data.filterBy(fn, scope||this);
},
/**
* Collects unique values for a particular dataIndex from this store.
* @param {String} dataIndex The property to collect
* @param {Boolean} allowNull (optional) Pass true to allow null, undefined or empty string values
* @param {Boolean} bypassFilter (optional) Pass true to collect from all records, even ones which are filtered
* @return {Array} An array of the unique values
**/
collect : function(dataIndex, allowNull, bypassFilter){
var d = (bypassFilter === true && this.snapshot) ?
this.snapshot.items : this.data.items;
var v, sv, r = [], l = {};
for(var i = 0, len = d.length; i < len; i++){
v = d[i].data[dataIndex];
sv = String(v);
if((allowNull || !Ext.isEmpty(v)) && !l[sv]){
l[sv] = true;
r[r.length] = v;
}
}
return r;
},
/**
* Revert to a view of the Record cache with no filtering applied.
* @param {Boolean} suppressEvent If true the filter is cleared silently without notifying listeners
*/
clearFilter : function(suppressEvent){
if(this.snapshot && this.snapshot != this.data){
this.data = this.snapshot;
delete this.snapshot;
if(suppressEvent !== true){
this.fireEvent("datachanged", this);
}
}
},
// private
afterEdit : function(record){
if(this.modified.indexOf(record) == -1){
this.modified.push(record);
}
this.fireEvent("update", this, record, Ext.data.Record.EDIT);
},
// private
afterReject : function(record){
this.modified.remove(record);
this.fireEvent("update", this, record, Ext.data.Record.REJECT);
},
// private
afterCommit : function(record){
this.modified.remove(record);
this.fireEvent("update", this, record, Ext.data.Record.COMMIT);
},
/**
* Commit all Records with outstanding changes. To handle updates for changes, subscribe to the
* Store's "update" event, and perform updating when the third parameter is Ext.data.Record.COMMIT.
*/
commitChanges : function(){
var m = this.modified.slice(0);
this.modified = [];
for(var i = 0, len = m.length; i < len; i++){
m[i].commit();
}
},
/**
* Cancel outstanding changes on all changed records.
*/
rejectChanges : function(){
var m = this.modified.slice(0);
this.modified = [];
for(var i = 0, len = m.length; i < len; i++){
m[i].reject();
}
},
onMetaChange : function(meta, rtype, o){
this.recordType = rtype;
this.fields = rtype.prototype.fields;
delete this.snapshot;
this.sortInfo = meta.sortInfo;
this.modified = [];
this.fireEvent('metachange', this, this.reader.meta);
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -