webgui/www/extras/extjs/docs/output/Store.jss.html

523 lines
No EOL
22 KiB
HTML

<html><head><title>Store.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>Store.js</h1><pre class="highlighted"><code><i>/**
* @class Ext.data.Store
* @extends Ext.util.Observable
* The Store class encapsulates a client side cache of {@link Ext.data.Record} objects which provide input data
* <b>for</b> widgets such as the Ext.grid.Grid, or the Ext.form.ComboBox.
* A Store object uses an implementation of {@link Ext.data.DataProxy} to access a data object unless you call loadData() directly and pass <b>in</b> your data. The Store object
* has no knowledge of the format of the data returned by the Proxy.
* The Store object uses its configured implementation of Ext.data.DataReader to create Ext.data.Record
* instances from the data object. These records are cached and made available through accessor functions.
* @constructor
* Creates a <b>new</b> Store
* @param {Object} config A config object containing the objects needed <b>for</b> the Store to access data,
* and read the data into Records.
*/</i>
Ext.data.Store = <b>function</b>(config){
<b>this</b>.data = <b>new</b> Ext.util.MixedCollection(false);
<b>this</b>.data.getKey = <b>function</b>(o){
<b>return</b> o.id;
};
<b>this</b>.baseParams = {};
<b>this</b>.paramNames = {
&quot;start&quot; : &quot;start&quot;,
&quot;limit&quot; : &quot;limit&quot;,
&quot;sort&quot; : &quot;sort&quot;,
&quot;dir&quot; : &quot;dir&quot;
};
Ext.apply(<b>this</b>, config);
<b>if</b>(this.reader &amp;&amp; !<b>this</b>.recordType){ <i>// reader passed</i>
<b>this</b>.recordType = <b>this</b>.reader.recordType;
}
<b>this</b>.fields = <b>this</b>.recordType.prototype.fields;
<b>this</b>.modified = [];
<b>this</b>.addEvents({
<i>/**
* @event datachanged
* Fires when the data cache has changed, and a widget which is using <b>this</b> Store
* as a Record cache should refresh its view.
* @param {Store} <b>this</b>
*/</i>
datachanged : true,
<i>/**
* @event add
* Fires when Records have been added to the Store
* @param {Store} <b>this</b>
* @param {Ext.data.Record[]} records The array of Records added
* @param {Number} index The index at which the record(s) were added
*/</i>
add : true,
<i>/**
* @event remove
* Fires when Records have been removed from the Store
* @param {Store} <b>this</b>
* @param {Ext.data.Record} record The Record that was removed
* @param {Number} index The index at which the record was removed
*/</i>
remove : true,
<i>/**
* @event update
* Fires when Records have been updated
* @param {Store} <b>this</b>
* @param {Ext.data.Record} record The Record that was updated
* @param {String} operation The update operation being performed. Value may be one of:
* &lt;pre&gt;&lt;code&gt;
Ext.data.Record.EDIT
Ext.data.Record.REJECT
Ext.data.Record.COMMIT
* &lt;/code&gt;&lt;/pre&gt;
*/</i>
update : true,
<i>/**
* @event clear
* Fires when the data cache has been cleared.
* @param {Store} <b>this</b>
*/</i>
clear : true,
<i>/**
* @event beforeload
* Fires before a request is made <b>for</b> a <b>new</b> data object. If the beforeload handler returns false
* the load action will be canceled.
* @param {Store} <b>this</b>
* @param {Object} options The loading options that were specified (see {@link #load} <b>for</b> details)
*/</i>
beforeload : true,
<i>/**
* @event load
* Fires after a <b>new</b> set of Records has been loaded.
* @param {Store} <b>this</b>
* @param {Ext.data.Record[]} records The Records that were loaded
* @param {Object} options The loading options that were specified (see {@link #load} <b>for</b> details)
*/</i>
load : true,
<i>/**
* @event loadexception
* Fires <b>if</b> an exception occurs <b>in</b> the Proxy during loading.
* Called <b>with</b> the signature of the Proxy's &quot;loadexception&quot; event.
*/</i>
loadexception : true
});
<b>if</b>(this.proxy){
<b>this</b>.relayEvents(<b>this</b>.proxy, [&quot;loadexception&quot;]);
}
<b>this</b>.sortToggle = {};
Ext.data.Store.superclass.constructor.call(<b>this</b>);
};
Ext.extend(Ext.data.Store, Ext.util.Observable, {
<i>/**
* @cfg {Ext.data.DataProxy} proxy The Proxy object which provides access to a data object.
*/</i>
<i>// holder</i>
<i>/***
* @cfg {Ext.data.Reader} reader The Reader object which processes the data object and returns
* an Array of Ext.data.record objects which are cached keyed by their &lt;em&gt;id&lt;/em&gt; property.
*/</i>
<i>// holder</i>
<i>/***
* @cfg {Object} baseParams An object containing properties which are to be sent as parameters
* on any HTTP request
*/</i>
<i>// holder</i>
<i>/***
* @cfg {Object} sortInfo A config object <b>in</b> the format: {field: &quot;fieldName&quot;, direction: &quot;ASC|DESC&quot;}
*/</i>
<i>// holder</i>
<i>/***
* @cfg {boolean} remoteSort True <b>if</b> sorting is to be handled by requesting the Proxy to provide a refreshed
* version of the data object <b>in</b> sorted order, as opposed to sorting the Record cache <b>in</b> place (defaults to false).
*/</i>
remoteSort : false,
<i>// private</i>
lastOptions : null,
<i>/**
* Add Records to the Store and fires the add event.
* @param {Ext.data.Record[]} records An Array of Ext.data.Record objects to add to the cache.
*/</i>
add : <b>function</b>(records){
records = [].concat(records);
<b>for</b>(var i = 0, len = records.length; i &lt; len; i++){
records[i].join(<b>this</b>);
}
<b>var</b> index = <b>this</b>.data.length;
<b>this</b>.data.addAll(records);
<b>this</b>.fireEvent(&quot;add&quot;, <b>this</b>, records, index);
},
<i>/**
* Remove a Record from the Store and fires the remove event.
* @param {Ext.data.Record} record Th Ext.data.Record object to remove from the cache.
*/</i>
remove : <b>function</b>(record){
<b>var</b> index = <b>this</b>.data.indexOf(record);
<b>this</b>.data.removeAt(index);
<b>this</b>.fireEvent(&quot;remove&quot;, <b>this</b>, record, index);
},
<i>/**
* Remove all Records from the Store and fires the clear event.
*/</i>
removeAll : <b>function</b>(){
<b>this</b>.data.clear();
<b>this</b>.fireEvent(&quot;clear&quot;, <b>this</b>);
},
<i>/**
* Inserts Records to the Store at the given index and fires the add event.
* @param {Number} index The start index at which to insert the passed Records.
* @param {Ext.data.Record[]} records An Array of Ext.data.Record objects to add to the cache.
*/</i>
insert : <b>function</b>(index, records){
records = [].concat(records);
<b>for</b>(var i = 0, len = records.length; i &lt; len; i++){
<b>this</b>.data.insert(index, records[i]);
records[i].join(<b>this</b>);
}
<b>this</b>.fireEvent(&quot;add&quot;, <b>this</b>, records, index);
},
<i>/**
* Get the index within the cache of the passed Record.
* @param {Ext.data.Record[]} records An Array of Ext.data.Record objects to add to the cache.
* @<b>return</b> {Number} The index of the passed Record. Returns -1 <b>if</b> not found.
*/</i>
indexOf : <b>function</b>(record){
<b>return</b> this.data.indexOf(record);
},
<i>/**
* Get the index within the cache of the Record <b>with</b> the passed id.
* @param {String} id The id of the Record to find.
* @<b>return</b> {Number} The index of the Record. Returns -1 <b>if</b> not found.
*/</i>
indexOfId : <b>function</b>(id){
<b>return</b> this.data.indexOfKey(id);
},
<i>/**
* Get the Record <b>with</b> the specified id.
* @param {String} id The id of the Record to find.
* @<b>return</b> {Ext.data.Record} The Record <b>with</b> the passed id. Returns undefined <b>if</b> not found.
*/</i>
getById : <b>function</b>(id){
<b>return</b> this.data.key(id);
},
<i>/**
* Get the Record at the specified index.
* @param {String} index The index of the Record to find.
* @<b>return</b> {Ext.data.Record} The Record at the passed index. Returns undefined <b>if</b> not found.
*/</i>
getAt : <b>function</b>(index){
<b>return</b> this.data.itemAt(index);
},
<i>/**
* Returns a range of Records between specified indices.
* @param {Number} startIndex (optional) The starting index (defaults to 0)
* @param {Number} endIndex (optional) The ending index (defaults to the last Record <b>in</b> the Store)
* @<b>return</b> {Ext.data.Record[]} An array of Records
*/</i>
getRange : <b>function</b>(start, end){
<b>return</b> this.data.getRange(start, end);
},
<i>// private</i>
storeOptions : <b>function</b>(o){
o = Ext.apply({}, o);
<b>delete</b> o.callback;
<b>delete</b> o.scope;
<b>this</b>.lastOptions = o;
},
<i>/**
* Loads the Record cache from the configured Proxy using the configured Reader.
* &lt;p&gt;
* If using remote paging, then the first load call must specify the &lt;em&gt;start&lt;/em&gt;
* and &lt;em&gt;limit&lt;/em&gt; properties <b>in</b> the options.params property to establish the initial
* position within the dataset, and the number of Records to cache on each read from the Proxy.
* &lt;p&gt;
* &lt;strong&gt;It is important to note that <b>for</b> remote data sources, loading is asynchronous,
* and <b>this</b> call will <b>return</b> before the <b>new</b> data has been loaded. Perform any post-processing
* <b>in</b> a callback <b>function</b>, or <b>in</b> a &quot;load&quot; event handler.&lt;/strong&gt;
* &lt;p&gt;
* @param {Object} options An object containing properties which control loading options:
* &lt;pre&gt;&lt;code&gt;
params {Object} An object containing properties to pass as HTTP parameters to a remote data source.
callback {Function} A <b>function</b> to be called after the Records have been loaded. The callback is
passed the following arguments:
r : Ext.data.Record[]
options: Options object from the load call
success: Boolean success indicator
scope {Object} Scope <b>with</b> which to call the callback (defaults to the Store object)
append {Boolean} indicator to append loaded records rather than replace the current cache.
* &lt;/code&gt;&lt;/pre&gt;
*/</i>
load : <b>function</b>(options){
options = options || {};
<b>if</b>(this.fireEvent(&quot;beforeload&quot;, <b>this</b>, options) !== false){
<b>this</b>.storeOptions(options);
<b>var</b> p = Ext.apply(options.params || {}, <b>this</b>.baseParams);
<b>if</b>(this.sortInfo &amp;&amp; <b>this</b>.remoteSort){
<b>var</b> pn = <b>this</b>.paramNames;
p[pn[&quot;sort&quot;]] = <b>this</b>.sortInfo.field;
p[pn[&quot;dir&quot;]] = <b>this</b>.sortInfo.direction;
}
<b>this</b>.proxy.load(p, <b>this</b>.reader, <b>this</b>.loadRecords, <b>this</b>, options);
}
},
<i>/**
* Reloads the Record cache from the configured Proxy using the configured Reader and
* the options from the last load operation performed.
* @param {Object} options (optional) An object containing properties which may override the options
* used <b>in</b> the last load operation. See {@link #load} <b>for</b> details (defaults to null, <b>in</b> which <b>case</b>
* the most recently used options are reused).
*/</i>
reload : <b>function</b>(options){
<b>this</b>.load(Ext.applyIf(options||{}, <b>this</b>.lastOptions));
},
<i>// private</i>
<i>// Called as a callback by the Reader during a load operation.</i>
loadRecords : <b>function</b>(o, options, success){
<b>if</b>(!o || success === false){
<b>if</b>(success !== false){
<b>this</b>.fireEvent(&quot;load&quot;, <b>this</b>, [], options);
}
<b>if</b>(options.callback){
options.callback.call(options.scope || <b>this</b>, [], options, false);
}
<b>return</b>;
}
<b>var</b> r = o.records, t = o.totalRecords || r.length;
<b>for</b>(var i = 0, len = r.length; i &lt; len; i++){
r[i].join(<b>this</b>);
}
<b>if</b>(!options || options.add !== true){
<b>this</b>.data.clear();
<b>this</b>.data.addAll(r);
<b>this</b>.totalLength = t;
<b>this</b>.applySort();
<b>this</b>.fireEvent(&quot;datachanged&quot;, <b>this</b>);
}<b>else</b>{
<b>this</b>.totalLength = Math.max(t, <b>this</b>.data.length+r.length);
<b>this</b>.data.addAll(r);
}
<b>this</b>.fireEvent(&quot;load&quot;, <b>this</b>, r, options);
<b>if</b>(options.callback){
options.callback.call(options.scope || <b>this</b>, r, options, true);
}
},
<i>/**
* Loads data from a passed data block. A Reader which understands the format of the data
* must have been configured <b>in</b> 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 <b>new</b> Records rather than replace the existing cache.
*/</i>
loadData : <b>function</b>(o, append){
<b>var</b> r = <b>this</b>.reader.readRecords(o);
<b>this</b>.loadRecords(r, {add: append}, true);
},
<i>/**
* Gets the number of cached records.
* &lt;p&gt;
* &lt;em&gt;If using paging, <b>this</b> may not be the total size of the dataset. If the data object
* used by the Reader contains the dataset size, then the getTotalCount() <b>function</b> returns
* the data set size&lt;/em&gt;
*/</i>
getCount : <b>function</b>(){
<b>return</b> this.data.length || 0;
},
<i>/**
* Gets the total number of records <b>in</b> the dataset.
* &lt;p&gt;
* &lt;em&gt;If using paging, <b>for</b> this to be accurate, the data object used by the Reader must contain
* the dataset size&lt;/em&gt;
*/</i>
getTotalCount : <b>function</b>(){
<b>return</b> this.totalLength || 0;
},
<i>/**
* Returns the sort state of the Store as an object <b>with</b> two properties:
* &lt;pre&gt;&lt;code&gt;
field {String} The name of the field by which the Records are sorted
direction {String} The sort order, &quot;ASC&quot; or &quot;DESC&quot;
* &lt;/code&gt;&lt;/pre&gt;
*/</i>
getSortState : <b>function</b>(){
<b>return</b> this.sortInfo;
},
<i>// private</i>
applySort : <b>function</b>(){
<b>if</b>(this.sortInfo &amp;&amp; !<b>this</b>.remoteSort){
<b>var</b> s = <b>this</b>.sortInfo, f = s.field;
<b>var</b> st = <b>this</b>.fields.get(f).sortType;
<b>var</b> fn = <b>function</b>(r1, r2){
<b>var</b> v1 = st(r1.data[f]), v2 = st(r2.data[f]);
<b>return</b> v1 &gt; v2 ? 1 : (v1 &lt; v2 ? -1 : 0);
};
<b>this</b>.data.sort(s.direction, fn);
<b>if</b>(this.snapshot &amp;&amp; <b>this</b>.snapshot != <b>this</b>.data){
<b>this</b>.snapshot.sort(s.direction, fn);
}
}
},
<i>/**
* Sets the <b>default</b> 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, &quot;ASC&quot; or &quot;DESC&quot; (defaults to &quot;ASC&quot;)
*/</i>
setDefaultSort : <b>function</b>(field, dir){
<b>this</b>.sortInfo = {field: field, direction: dir ? dir.toUpperCase() : &quot;ASC&quot;};
},
<i>/**
* 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, &quot;ASC&quot; or &quot;DESC&quot; (defaults to &quot;ASC&quot;)
*/</i>
sort : <b>function</b>(fieldName, dir){
<b>var</b> f = <b>this</b>.fields.get(fieldName);
<b>if</b>(!dir){
<b>if</b>(this.sortInfo &amp;&amp; <b>this</b>.sortInfo.field == f.name){ <i>// toggle sort dir</i>
dir = (<b>this</b>.sortToggle[f.name] || &quot;ASC&quot;).toggle(&quot;ASC&quot;, &quot;DESC&quot;);
}<b>else</b>{
dir = f.sortDir;
}
}
<b>this</b>.sortToggle[f.name] = dir;
<b>this</b>.sortInfo = {field: f.name, direction: dir};
<b>if</b>(!<b>this</b>.remoteSort){
<b>this</b>.applySort();
<b>this</b>.fireEvent(&quot;datachanged&quot;, <b>this</b>);
}<b>else</b>{
<b>this</b>.load(<b>this</b>.lastOptions);
}
},
<i>/**
* Calls the specified <b>function</b> for each of the Records <b>in</b> the cache.
* @param {Function} fn The <b>function</b> to call. The Record is passed as the first parameter.
* Returning &lt;em&gt;false&lt;/em&gt; aborts and exits the iteration.
* @param {Object} scope (optional) The scope <b>in</b> which to call the <b>function</b> (defaults to the Record).
*/</i>
each : <b>function</b>(fn, scope){
<b>this</b>.data.each(fn, scope);
},
<i>/**
* Get all records modified since the last load, or since the last commit.
* @<b>return</b> {Ext.data.Record[]} An array of Records containing outstanding modifications.
*/</i>
getModifiedRecords : <b>function</b>(){
<b>return</b> this.modified;
},
<i>/**
* 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 <b>with</b> or a RegExp to test against the field
* @<b>return</b> {Boolean} True <b>if</b> the filter matched at least one record, <b>else</b> false
*/</i>
filter : <b>function</b>(property, value){
<b>if</b>(!value.exec){ <i>// not a regex</i>
value = String(value);
<b>if</b>(value.length == 0){
<b>return</b> this.clearFilter();
}
value = <b>new</b> RegExp(&quot;^&quot; + Ext.escapeRe(value), &quot;i&quot;);
}
<b>this</b>.filterBy(<b>function</b>(r){
<b>return</b> value.test(r.data[property]);
});
},
<i>/**
* Filter by a <b>function</b>. The specified <b>function</b> will be called <b>with</b> each
* record <b>in</b> this data source. If the <b>function</b> returns true the record is included,
* otherwise it is filtered.
* @param {Function} fn The <b>function</b> to be called, it will receive 2 args (record, id)
* @param {Object} scope (optional) The scope of the <b>function</b> (defaults to <b>this</b>)
*/</i>
filterBy : <b>function</b>(fn, scope){
<b>var</b> data = <b>this</b>.snapshot || <b>this</b>.data;
<b>this</b>.snapshot = data;
<b>this</b>.data = data.filterBy(fn, scope);
<b>this</b>.fireEvent(&quot;datachanged&quot;, <b>this</b>);
},
<i>/**
* Revert to a view of the Record cache <b>with</b> no filtering applied.
* @param {Boolean} suppressEvent If true the filter is cleared silently without notifying listeners
*/</i>
clearFilter : <b>function</b>(suppressEvent){
<b>if</b>(this.snapshot &amp;&amp; <b>this</b>.snapshot != <b>this</b>.data){
<b>this</b>.data = <b>this</b>.snapshot;
<b>delete</b> this.snapshot;
<b>if</b>(suppressEvent !== true){
<b>this</b>.fireEvent(&quot;datachanged&quot;, <b>this</b>);
}
}
},
<i>// private</i>
afterEdit : <b>function</b>(record){
<b>if</b>(this.modified.indexOf(record) == -1){
<b>this</b>.modified.push(record);
}
<b>this</b>.fireEvent(&quot;update&quot;, <b>this</b>, record, Ext.data.Record.EDIT);
},
<i>// private</i>
afterReject : <b>function</b>(record){
<b>this</b>.modified.remove(record);
<b>this</b>.fireEvent(&quot;update&quot;, <b>this</b>, record, Ext.data.Record.REJECT);
},
<i>// private</i>
afterCommit : <b>function</b>(record){
<b>this</b>.modified.remove(record);
<b>this</b>.fireEvent(&quot;update&quot;, <b>this</b>, record, Ext.data.Record.COMMIT);
},
<i>/**
* Commit all Records <b>with</b> outstanding changes. To handle updates <b>for</b> changes, subscribe to the
* Store's &quot;update&quot; event, and perform updating when the third parameter is Ext.data.Record.COMMIT.
*/</i>
commitChanges : <b>function</b>(){
<b>var</b> m = <b>this</b>.modified.slice(0);
<b>this</b>.modified = [];
<b>for</b>(var i = 0, len = m.length; i &lt; len; i++){
m[i].commit();
}
},
<i>/**
* Cancel outstanding changes on all changed records.
*/</i>
rejectChanges : <b>function</b>(){
<b>var</b> m = <b>this</b>.modified.slice(0);
<b>this</b>.modified = [];
<b>for</b>(var i = 0, len = m.length; i &lt; len; i++){
m[i].reject();
}
}
});</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright &copy; 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
</body></html>