[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 8 | * @class Ext.data.Store |
---|
| 9 | * @extends Ext.util.Observable |
---|
| 10 | * <p>The Store class encapsulates a client side cache of {@link Ext.data.Record Record} |
---|
| 11 | * objects which provide input data for Components such as the {@link Ext.grid.GridPanel GridPanel}, |
---|
| 12 | * the {@link Ext.form.ComboBox ComboBox}, or the {@link Ext.DataView DataView}.</p> |
---|
| 13 | * <p><u>Retrieving Data</u></p> |
---|
| 14 | * <p>A Store object may access a data object using:<div class="mdetail-params"><ul> |
---|
| 15 | * <li>{@link #proxy configured implementation} of {@link Ext.data.DataProxy DataProxy}</li> |
---|
| 16 | * <li>{@link #data} to automatically pass in data</li> |
---|
| 17 | * <li>{@link #loadData} to manually pass in data</li> |
---|
| 18 | * </ul></div></p> |
---|
| 19 | * <p><u>Reading Data</u></p> |
---|
| 20 | * <p>A Store object has no inherent knowledge of the format of the data object (it could be |
---|
| 21 | * an Array, XML, or JSON). A Store object uses an appropriate {@link #reader configured implementation} |
---|
| 22 | * of a {@link Ext.data.DataReader DataReader} to create {@link Ext.data.Record Record} instances from the data |
---|
| 23 | * object.</p> |
---|
| 24 | * <p><u>Store Types</u></p> |
---|
| 25 | * <p>There are several implementations of Store available which are customized for use with |
---|
| 26 | * a specific DataReader implementation. Here is an example using an ArrayStore which implicitly |
---|
| 27 | * creates a reader commensurate to an Array data object.</p> |
---|
| 28 | * <pre><code> |
---|
| 29 | var myStore = new Ext.data.ArrayStore({ |
---|
| 30 | fields: ['fullname', 'first'], |
---|
| 31 | idIndex: 0 // id for each record will be the first element |
---|
| 32 | }); |
---|
| 33 | * </code></pre> |
---|
| 34 | * <p>For custom implementations create a basic {@link Ext.data.Store} configured as needed:</p> |
---|
| 35 | * <pre><code> |
---|
| 36 | // create a {@link Ext.data.Record Record} constructor: |
---|
| 37 | var rt = Ext.data.Record.create([ |
---|
| 38 | {name: 'fullname'}, |
---|
| 39 | {name: 'first'} |
---|
| 40 | ]); |
---|
| 41 | var myStore = new Ext.data.Store({ |
---|
| 42 | // explicitly create reader |
---|
| 43 | reader: new Ext.data.ArrayReader( |
---|
| 44 | { |
---|
| 45 | idIndex: 0 // id for each record will be the first element |
---|
| 46 | }, |
---|
| 47 | rt // recordType |
---|
| 48 | ) |
---|
| 49 | }); |
---|
| 50 | * </code></pre> |
---|
| 51 | * <p>Load some data into store (note the data object is an array which corresponds to the reader):</p> |
---|
| 52 | * <pre><code> |
---|
| 53 | var myData = [ |
---|
| 54 | [1, 'Fred Flintstone', 'Fred'], // note that id for the record is the first element |
---|
| 55 | [2, 'Barney Rubble', 'Barney'] |
---|
| 56 | ]; |
---|
| 57 | myStore.loadData(myData); |
---|
| 58 | * </code></pre> |
---|
| 59 | * <p>Records are cached and made available through accessor functions. An example of adding |
---|
| 60 | * a record to the store:</p> |
---|
| 61 | * <pre><code> |
---|
| 62 | var defaultData = { |
---|
| 63 | fullname: 'Full Name', |
---|
| 64 | first: 'First Name' |
---|
| 65 | }; |
---|
| 66 | var recId = 100; // provide unique id for the record |
---|
| 67 | var r = new myStore.recordType(defaultData, ++recId); // create new record |
---|
| 68 | myStore.{@link #insert}(0, r); // insert a new record into the store (also see {@link #add}) |
---|
| 69 | * </code></pre> |
---|
| 70 | * <p><u>Writing Data</u></p> |
---|
| 71 | * <p>And <b>new in Ext version 3</b>, use the new {@link Ext.data.DataWriter DataWriter} to create an automated, <a href="http://extjs.com/deploy/dev/examples/writer/writer.html">Writable Store</a> |
---|
| 72 | * along with <a href="http://extjs.com/deploy/dev/examples/restful/restful.html">RESTful features.</a> |
---|
| 73 | * @constructor |
---|
| 74 | * Creates a new Store. |
---|
| 75 | * @param {Object} config A config object containing the objects needed for the Store to access data, |
---|
| 76 | * and read the data into Records. |
---|
| 77 | * @xtype store |
---|
| 78 | */ |
---|
| 79 | Ext.data.Store = Ext.extend(Ext.util.Observable, { |
---|
| 80 | /** |
---|
| 81 | * @cfg {String} storeId If passed, the id to use to register with the <b>{@link Ext.StoreMgr StoreMgr}</b>. |
---|
| 82 | * <p><b>Note</b>: if a (deprecated) <tt>{@link #id}</tt> is specified it will supersede the <tt>storeId</tt> |
---|
| 83 | * assignment.</p> |
---|
| 84 | */ |
---|
| 85 | /** |
---|
| 86 | * @cfg {String} url If a <tt>{@link #proxy}</tt> is not specified the <tt>url</tt> will be used to |
---|
| 87 | * implicitly configure a {@link Ext.data.HttpProxy HttpProxy} if an <tt>url</tt> is specified. |
---|
| 88 | * Typically this option, or the <code>{@link #data}</code> option will be specified. |
---|
| 89 | */ |
---|
| 90 | /** |
---|
| 91 | * @cfg {Boolean/Object} autoLoad If <tt>{@link #data}</tt> is not specified, and if <tt>autoLoad</tt> |
---|
| 92 | * is <tt>true</tt> or an <tt>Object</tt>, this store's {@link #load} method is automatically called |
---|
| 93 | * after creation. If the value of <tt>autoLoad</tt> is an <tt>Object</tt>, this <tt>Object</tt> will |
---|
| 94 | * be passed to the store's {@link #load} method. |
---|
| 95 | */ |
---|
| 96 | /** |
---|
| 97 | * @cfg {Ext.data.DataProxy} proxy The {@link Ext.data.DataProxy DataProxy} object which provides |
---|
| 98 | * access to a data object. See <code>{@link #url}</code>. |
---|
| 99 | */ |
---|
| 100 | /** |
---|
| 101 | * @cfg {Array} data An inline data object readable by the <code>{@link #reader}</code>. |
---|
| 102 | * Typically this option, or the <code>{@link #url}</code> option will be specified. |
---|
| 103 | */ |
---|
| 104 | /** |
---|
| 105 | * @cfg {Ext.data.DataReader} reader The {@link Ext.data.DataReader Reader} object which processes the |
---|
| 106 | * data object and returns an Array of {@link Ext.data.Record} objects which are cached keyed by their |
---|
| 107 | * <b><tt>{@link Ext.data.Record#id id}</tt></b> property. |
---|
| 108 | */ |
---|
| 109 | /** |
---|
| 110 | * @cfg {Ext.data.DataWriter} writer |
---|
| 111 | * <p>The {@link Ext.data.DataWriter Writer} object which processes a record object for being written |
---|
| 112 | * to the server-side database.</p> |
---|
| 113 | * <br><p>When a writer is installed into a Store the {@link #add}, {@link #remove}, and {@link #update} |
---|
| 114 | * events on the store are monitored in order to remotely {@link #createRecords create records}, |
---|
| 115 | * {@link #destroyRecord destroy records}, or {@link #updateRecord update records}.</p> |
---|
| 116 | * <br><p>The proxy for this store will relay any {@link #writexception} events to this store.</p> |
---|
| 117 | * <br><p>Sample implementation: |
---|
| 118 | * <pre><code> |
---|
| 119 | var writer = new {@link Ext.data.JsonWriter}({ |
---|
| 120 | encode: true, |
---|
| 121 | writeAllFields: true // write all fields, not just those that changed |
---|
| 122 | }); |
---|
| 123 | |
---|
| 124 | // Typical Store collecting the Proxy, Reader and Writer together. |
---|
| 125 | var store = new Ext.data.Store({ |
---|
| 126 | storeId: 'user', |
---|
| 127 | root: 'records', |
---|
| 128 | proxy: proxy, |
---|
| 129 | reader: reader, |
---|
| 130 | writer: writer, // <-- plug a DataWriter into the store just as you would a Reader |
---|
| 131 | paramsAsHash: true, |
---|
| 132 | autoSave: false // <-- false to delay executing create, update, destroy requests |
---|
| 133 | // until specifically told to do so. |
---|
| 134 | }); |
---|
| 135 | * </code></pre></p> |
---|
| 136 | */ |
---|
| 137 | writer : undefined, |
---|
| 138 | /** |
---|
| 139 | * @cfg {Object} baseParams |
---|
| 140 | * <p>An object containing properties which are to be sent as parameters |
---|
| 141 | * for <i>every</i> HTTP request.</p> |
---|
| 142 | * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p> |
---|
| 143 | * <p><b>Note</b>: <code>baseParams</code> may be superseded by any <code>params</code> |
---|
| 144 | * specified in a <code>{@link #load}</code> request, see <code>{@link #load}</code> |
---|
| 145 | * for more details.</p> |
---|
| 146 | * This property may be modified after creation using the <code>{@link #setBaseParam}</code> |
---|
| 147 | * method. |
---|
| 148 | * @property |
---|
| 149 | */ |
---|
| 150 | /** |
---|
| 151 | * @cfg {Object} sortInfo A config object to specify the sort order in the request of a Store's |
---|
| 152 | * {@link #load} operation. Note that for local sorting, the <tt>direction</tt> property is |
---|
| 153 | * case-sensitive. See also {@link #remoteSort} and {@link #paramNames}. |
---|
| 154 | * For example:<pre><code> |
---|
| 155 | sortInfo: { |
---|
| 156 | field: 'fieldName', |
---|
| 157 | direction: 'ASC' // or 'DESC' (case sensitive for local sorting) |
---|
| 158 | } |
---|
| 159 | </code></pre> |
---|
| 160 | */ |
---|
| 161 | /** |
---|
| 162 | * @cfg {boolean} remoteSort <tt>true</tt> if sorting is to be handled by requesting the <tt>{@link #proxy Proxy}</tt> |
---|
| 163 | * to provide a refreshed version of the data object in sorted order, as opposed to sorting the Record cache |
---|
| 164 | * in place (defaults to <tt>false</tt>). |
---|
| 165 | * <p>If <tt>remoteSort</tt> is <tt>true</tt>, then clicking on a {@link Ext.grid.Column Grid Column}'s |
---|
| 166 | * {@link Ext.grid.Column#header header} causes the current page to be requested from the server appending |
---|
| 167 | * the following two parameters to the <b><tt>{@link #load params}</tt></b>:<div class="mdetail-params"><ul> |
---|
| 168 | * <li><b><tt>sort</tt></b> : String<p class="sub-desc">The <tt>name</tt> (as specified in the Record's |
---|
| 169 | * {@link Ext.data.Field Field definition}) of the field to sort on.</p></li> |
---|
| 170 | * <li><b><tt>dir</tt></b> : String<p class="sub-desc">The direction of the sort, 'ASC' or 'DESC' (case-sensitive).</p></li> |
---|
| 171 | * </ul></div></p> |
---|
| 172 | */ |
---|
| 173 | remoteSort : false, |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * @cfg {Boolean} autoDestroy <tt>true</tt> to destroy the store when the component the store is bound |
---|
| 177 | * to is destroyed (defaults to <tt>false</tt>). |
---|
| 178 | * <p><b>Note</b>: this should be set to true when using stores that are bound to only 1 component.</p> |
---|
| 179 | */ |
---|
| 180 | autoDestroy : false, |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * @cfg {Boolean} pruneModifiedRecords <tt>true</tt> to clear all modified record information each time |
---|
| 184 | * the store is loaded or when a record is removed (defaults to <tt>false</tt>). See {@link #getModifiedRecords} |
---|
| 185 | * for the accessor method to retrieve the modified records. |
---|
| 186 | */ |
---|
| 187 | pruneModifiedRecords : false, |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | * Contains the last options object used as the parameter to the {@link #load} method. See {@link #load} |
---|
| 191 | * for the details of what this may contain. This may be useful for accessing any params which were used |
---|
| 192 | * to load the current Record cache. |
---|
| 193 | * @property |
---|
| 194 | */ |
---|
| 195 | lastOptions : null, |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * @cfg {Boolean} autoSave |
---|
| 199 | * <p>Defaults to <tt>true</tt> causing the store to automatically {@link #save} records to |
---|
| 200 | * the server when a record is modified (ie: becomes 'dirty'). Specify <tt>false</tt> to manually call {@link #save} |
---|
| 201 | * to send all modifiedRecords to the server.</p> |
---|
| 202 | * <br><p><b>Note</b>: each CRUD action will be sent as a separate request.</p> |
---|
| 203 | */ |
---|
| 204 | autoSave : true, |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * @cfg {Boolean} batch |
---|
| 208 | * <p>Defaults to <tt>true</tt> (unless <code>{@link #restful}:true</code>). Multiple |
---|
| 209 | * requests for each CRUD action (CREATE, READ, UPDATE and DESTROY) will be combined |
---|
| 210 | * and sent as one transaction. Only applies when <code>{@link #autoSave}</code> is set |
---|
| 211 | * to <tt>false</tt>.</p> |
---|
| 212 | * <br><p>If Store is RESTful, the DataProxy is also RESTful, and a unique transaction is |
---|
| 213 | * generated for each record.</p> |
---|
| 214 | */ |
---|
| 215 | batch : true, |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * @cfg {Boolean} restful |
---|
| 219 | * Defaults to <tt>false</tt>. Set to <tt>true</tt> to have the Store and the set |
---|
| 220 | * Proxy operate in a RESTful manner. The store will automatically generate GET, POST, |
---|
| 221 | * PUT and DELETE requests to the server. The HTTP method used for any given CRUD |
---|
| 222 | * action is described in {@link Ext.data.Api#restActions}. For additional information |
---|
| 223 | * see {@link Ext.data.DataProxy#restful}. |
---|
| 224 | * <p><b>Note</b>: if <code>{@link #restful}:true</code> <code>batch</code> will |
---|
| 225 | * internally be set to <tt>false</tt>.</p> |
---|
| 226 | */ |
---|
| 227 | restful: false, |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * @cfg {Object} paramNames |
---|
| 231 | * <p>An object containing properties which specify the names of the paging and |
---|
| 232 | * sorting parameters passed to remote servers when loading blocks of data. By default, this |
---|
| 233 | * object takes the following form:</p><pre><code> |
---|
| 234 | { |
---|
| 235 | start : 'start', // The parameter name which specifies the start row |
---|
| 236 | limit : 'limit', // The parameter name which specifies number of rows to return |
---|
| 237 | sort : 'sort', // The parameter name which specifies the column to sort on |
---|
| 238 | dir : 'dir' // The parameter name which specifies the sort direction |
---|
| 239 | } |
---|
| 240 | </code></pre> |
---|
| 241 | * <p>The server must produce the requested data block upon receipt of these parameter names. |
---|
| 242 | * If different parameter names are required, this property can be overriden using a configuration |
---|
| 243 | * property.</p> |
---|
| 244 | * <p>A {@link Ext.PagingToolbar PagingToolbar} bound to this Store uses this property to determine |
---|
| 245 | * the parameter names to use in its {@link #load requests}. |
---|
| 246 | */ |
---|
| 247 | paramNames : undefined, |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * @cfg {Object} defaultParamNames |
---|
| 251 | * Provides the default values for the {@link #paramNames} property. To globally modify the parameters |
---|
| 252 | * for all stores, this object should be changed on the store prototype. |
---|
| 253 | */ |
---|
| 254 | defaultParamNames : { |
---|
| 255 | start : 'start', |
---|
| 256 | limit : 'limit', |
---|
| 257 | sort : 'sort', |
---|
| 258 | dir : 'dir' |
---|
| 259 | }, |
---|
| 260 | |
---|
| 261 | isDestroyed: false, |
---|
| 262 | hasMultiSort: false, |
---|
| 263 | |
---|
| 264 | // private |
---|
| 265 | batchKey : '_ext_batch_', |
---|
| 266 | |
---|
| 267 | constructor : function(config){ |
---|
| 268 | /** |
---|
| 269 | * @property multiSort |
---|
| 270 | * @type Boolean |
---|
| 271 | * True if this store is currently sorted by more than one field/direction combination. |
---|
| 272 | */ |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | * @property isDestroyed |
---|
| 276 | * @type Boolean |
---|
| 277 | * True if the store has been destroyed already. Read only |
---|
| 278 | */ |
---|
| 279 | |
---|
| 280 | this.data = new Ext.util.MixedCollection(false); |
---|
| 281 | this.data.getKey = function(o){ |
---|
| 282 | return o.id; |
---|
| 283 | }; |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | // temporary removed-records cache |
---|
| 287 | this.removed = []; |
---|
| 288 | |
---|
| 289 | if(config && config.data){ |
---|
| 290 | this.inlineData = config.data; |
---|
| 291 | delete config.data; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | Ext.apply(this, config); |
---|
| 295 | |
---|
| 296 | /** |
---|
| 297 | * See the <code>{@link #baseParams corresponding configuration option}</code> |
---|
| 298 | * for a description of this property. |
---|
| 299 | * To modify this property see <code>{@link #setBaseParam}</code>. |
---|
| 300 | * @property |
---|
| 301 | */ |
---|
| 302 | this.baseParams = Ext.isObject(this.baseParams) ? this.baseParams : {}; |
---|
| 303 | |
---|
| 304 | this.paramNames = Ext.applyIf(this.paramNames || {}, this.defaultParamNames); |
---|
| 305 | |
---|
| 306 | if((this.url || this.api) && !this.proxy){ |
---|
| 307 | this.proxy = new Ext.data.HttpProxy({url: this.url, api: this.api}); |
---|
| 308 | } |
---|
| 309 | // If Store is RESTful, so too is the DataProxy |
---|
| 310 | if (this.restful === true && this.proxy) { |
---|
| 311 | // When operating RESTfully, a unique transaction is generated for each record. |
---|
| 312 | // TODO might want to allow implemention of faux REST where batch is possible using RESTful routes only. |
---|
| 313 | this.batch = false; |
---|
| 314 | Ext.data.Api.restify(this.proxy); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | if(this.reader){ // reader passed |
---|
| 318 | if(!this.recordType){ |
---|
| 319 | this.recordType = this.reader.recordType; |
---|
| 320 | } |
---|
| 321 | if(this.reader.onMetaChange){ |
---|
| 322 | this.reader.onMetaChange = this.reader.onMetaChange.createSequence(this.onMetaChange, this); |
---|
| 323 | } |
---|
| 324 | if (this.writer) { // writer passed |
---|
| 325 | if (this.writer instanceof(Ext.data.DataWriter) === false) { // <-- config-object instead of instance. |
---|
| 326 | this.writer = this.buildWriter(this.writer); |
---|
| 327 | } |
---|
| 328 | this.writer.meta = this.reader.meta; |
---|
| 329 | this.pruneModifiedRecords = true; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | * The {@link Ext.data.Record Record} constructor as supplied to (or created by) the |
---|
| 335 | * {@link Ext.data.DataReader Reader}. Read-only. |
---|
| 336 | * <p>If the Reader was constructed by passing in an Array of {@link Ext.data.Field} definition objects, |
---|
| 337 | * instead of a Record constructor, it will implicitly create a Record constructor from that Array (see |
---|
| 338 | * {@link Ext.data.Record}.{@link Ext.data.Record#create create} for additional details).</p> |
---|
| 339 | * <p>This property may be used to create new Records of the type held in this Store, for example:</p><pre><code> |
---|
| 340 | // create the data store |
---|
| 341 | var store = new Ext.data.ArrayStore({ |
---|
| 342 | autoDestroy: true, |
---|
| 343 | fields: [ |
---|
| 344 | {name: 'company'}, |
---|
| 345 | {name: 'price', type: 'float'}, |
---|
| 346 | {name: 'change', type: 'float'}, |
---|
| 347 | {name: 'pctChange', type: 'float'}, |
---|
| 348 | {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'} |
---|
| 349 | ] |
---|
| 350 | }); |
---|
| 351 | store.loadData(myData); |
---|
| 352 | |
---|
| 353 | // create the Grid |
---|
| 354 | var grid = new Ext.grid.EditorGridPanel({ |
---|
| 355 | store: store, |
---|
| 356 | colModel: new Ext.grid.ColumnModel({ |
---|
| 357 | columns: [ |
---|
| 358 | {id:'company', header: 'Company', width: 160, dataIndex: 'company'}, |
---|
| 359 | {header: 'Price', renderer: 'usMoney', dataIndex: 'price'}, |
---|
| 360 | {header: 'Change', renderer: change, dataIndex: 'change'}, |
---|
| 361 | {header: '% Change', renderer: pctChange, dataIndex: 'pctChange'}, |
---|
| 362 | {header: 'Last Updated', width: 85, |
---|
| 363 | renderer: Ext.util.Format.dateRenderer('m/d/Y'), |
---|
| 364 | dataIndex: 'lastChange'} |
---|
| 365 | ], |
---|
| 366 | defaults: { |
---|
| 367 | sortable: true, |
---|
| 368 | width: 75 |
---|
| 369 | } |
---|
| 370 | }), |
---|
| 371 | autoExpandColumn: 'company', // match the id specified in the column model |
---|
| 372 | height:350, |
---|
| 373 | width:600, |
---|
| 374 | title:'Array Grid', |
---|
| 375 | tbar: [{ |
---|
| 376 | text: 'Add Record', |
---|
| 377 | handler : function(){ |
---|
| 378 | var defaultData = { |
---|
| 379 | change: 0, |
---|
| 380 | company: 'New Company', |
---|
| 381 | lastChange: (new Date()).clearTime(), |
---|
| 382 | pctChange: 0, |
---|
| 383 | price: 10 |
---|
| 384 | }; |
---|
| 385 | var recId = 3; // provide unique id |
---|
| 386 | var p = new store.recordType(defaultData, recId); // create new record |
---|
| 387 | grid.stopEditing(); |
---|
| 388 | store.{@link #insert}(0, p); // insert a new record into the store (also see {@link #add}) |
---|
| 389 | grid.startEditing(0, 0); |
---|
| 390 | } |
---|
| 391 | }] |
---|
| 392 | }); |
---|
| 393 | * </code></pre> |
---|
| 394 | * @property recordType |
---|
| 395 | * @type Function |
---|
| 396 | */ |
---|
| 397 | |
---|
| 398 | if(this.recordType){ |
---|
| 399 | /** |
---|
| 400 | * A {@link Ext.util.MixedCollection MixedCollection} containing the defined {@link Ext.data.Field Field}s |
---|
| 401 | * for the {@link Ext.data.Record Records} stored in this Store. Read-only. |
---|
| 402 | * @property fields |
---|
| 403 | * @type Ext.util.MixedCollection |
---|
| 404 | */ |
---|
| 405 | this.fields = this.recordType.prototype.fields; |
---|
| 406 | } |
---|
| 407 | this.modified = []; |
---|
| 408 | |
---|
| 409 | this.addEvents( |
---|
| 410 | /** |
---|
| 411 | * @event datachanged |
---|
| 412 | * Fires when the data cache has changed in a bulk manner (e.g., it has been sorted, filtered, etc.) and a |
---|
| 413 | * widget that is using this Store as a Record cache should refresh its view. |
---|
| 414 | * @param {Store} this |
---|
| 415 | */ |
---|
| 416 | 'datachanged', |
---|
| 417 | /** |
---|
| 418 | * @event metachange |
---|
| 419 | * Fires when this store's reader provides new metadata (fields). This is currently only supported for JsonReaders. |
---|
| 420 | * @param {Store} this |
---|
| 421 | * @param {Object} meta The JSON metadata |
---|
| 422 | */ |
---|
| 423 | 'metachange', |
---|
| 424 | /** |
---|
| 425 | * @event add |
---|
| 426 | * Fires when Records have been {@link #add}ed to the Store |
---|
| 427 | * @param {Store} this |
---|
| 428 | * @param {Ext.data.Record[]} records The array of Records added |
---|
| 429 | * @param {Number} index The index at which the record(s) were added |
---|
| 430 | */ |
---|
| 431 | 'add', |
---|
| 432 | /** |
---|
| 433 | * @event remove |
---|
| 434 | * Fires when a Record has been {@link #remove}d from the Store |
---|
| 435 | * @param {Store} this |
---|
| 436 | * @param {Ext.data.Record} record The Record that was removed |
---|
| 437 | * @param {Number} index The index at which the record was removed |
---|
| 438 | */ |
---|
| 439 | 'remove', |
---|
| 440 | /** |
---|
| 441 | * @event update |
---|
| 442 | * Fires when a Record has been updated |
---|
| 443 | * @param {Store} this |
---|
| 444 | * @param {Ext.data.Record} record The Record that was updated |
---|
| 445 | * @param {String} operation The update operation being performed. Value may be one of: |
---|
| 446 | * <pre><code> |
---|
| 447 | Ext.data.Record.EDIT |
---|
| 448 | Ext.data.Record.REJECT |
---|
| 449 | Ext.data.Record.COMMIT |
---|
| 450 | * </code></pre> |
---|
| 451 | */ |
---|
| 452 | 'update', |
---|
| 453 | /** |
---|
| 454 | * @event clear |
---|
| 455 | * Fires when the data cache has been cleared. |
---|
| 456 | * @param {Store} this |
---|
| 457 | * @param {Record[]} records The records that were cleared. |
---|
| 458 | */ |
---|
| 459 | 'clear', |
---|
| 460 | /** |
---|
| 461 | * @event exception |
---|
| 462 | * <p>Fires if an exception occurs in the Proxy during a remote request. |
---|
| 463 | * This event is relayed through the corresponding {@link Ext.data.DataProxy}. |
---|
| 464 | * See {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#exception exception} |
---|
| 465 | * for additional details. |
---|
| 466 | * @param {misc} misc See {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#exception exception} |
---|
| 467 | * for description. |
---|
| 468 | */ |
---|
| 469 | 'exception', |
---|
| 470 | /** |
---|
| 471 | * @event beforeload |
---|
| 472 | * Fires before a request is made for a new data object. If the beforeload handler returns |
---|
| 473 | * <tt>false</tt> the {@link #load} action will be canceled. |
---|
| 474 | * @param {Store} this |
---|
| 475 | * @param {Object} options The loading options that were specified (see {@link #load} for details) |
---|
| 476 | */ |
---|
| 477 | 'beforeload', |
---|
| 478 | /** |
---|
| 479 | * @event load |
---|
| 480 | * Fires after a new set of Records has been loaded. |
---|
| 481 | * @param {Store} this |
---|
| 482 | * @param {Ext.data.Record[]} records The Records that were loaded |
---|
| 483 | * @param {Object} options The loading options that were specified (see {@link #load} for details) |
---|
| 484 | */ |
---|
| 485 | 'load', |
---|
| 486 | /** |
---|
| 487 | * @event loadexception |
---|
| 488 | * <p>This event is <b>deprecated</b> in favor of the catch-all <b><code>{@link #exception}</code></b> |
---|
| 489 | * event instead.</p> |
---|
| 490 | * <p>This event is relayed through the corresponding {@link Ext.data.DataProxy}. |
---|
| 491 | * See {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#loadexception loadexception} |
---|
| 492 | * for additional details. |
---|
| 493 | * @param {misc} misc See {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#loadexception loadexception} |
---|
| 494 | * for description. |
---|
| 495 | */ |
---|
| 496 | 'loadexception', |
---|
| 497 | /** |
---|
| 498 | * @event beforewrite |
---|
| 499 | * @param {Ext.data.Store} store |
---|
| 500 | * @param {String} action [Ext.data.Api.actions.create|update|destroy] |
---|
| 501 | * @param {Record/Record[]} rs The Record(s) being written. |
---|
| 502 | * @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) |
---|
| 503 | * @param {Object} arg The callback's arg object passed to the {@link #request} function |
---|
| 504 | */ |
---|
| 505 | 'beforewrite', |
---|
| 506 | /** |
---|
| 507 | * @event write |
---|
| 508 | * Fires if the server returns 200 after an Ext.data.Api.actions CRUD action. |
---|
| 509 | * Success of the action is determined in the <code>result['successProperty']</code>property (<b>NOTE</b> for RESTful stores, |
---|
| 510 | * a simple 20x response is sufficient for the actions "destroy" and "update". The "create" action should should return 200 along with a database pk). |
---|
| 511 | * @param {Ext.data.Store} store |
---|
| 512 | * @param {String} action [Ext.data.Api.actions.create|update|destroy] |
---|
| 513 | * @param {Object} result The 'data' picked-out out of the response for convenience. |
---|
| 514 | * @param {Ext.Direct.Transaction} res |
---|
| 515 | * @param {Record/Record[]} rs Store's records, the subject(s) of the write-action |
---|
| 516 | */ |
---|
| 517 | 'write', |
---|
| 518 | /** |
---|
| 519 | * @event beforesave |
---|
| 520 | * Fires before a save action is called. A save encompasses destroying records, updating records and creating records. |
---|
| 521 | * @param {Ext.data.Store} store |
---|
| 522 | * @param {Object} data An object containing the data that is to be saved. The object will contain a key for each appropriate action, |
---|
| 523 | * with an array of records for each action. |
---|
| 524 | */ |
---|
| 525 | 'beforesave', |
---|
| 526 | /** |
---|
| 527 | * @event save |
---|
| 528 | * Fires after a save is completed. A save encompasses destroying records, updating records and creating records. |
---|
| 529 | * @param {Ext.data.Store} store |
---|
| 530 | * @param {Number} batch The identifier for the batch that was saved. |
---|
| 531 | * @param {Object} data An object containing the data that is to be saved. The object will contain a key for each appropriate action, |
---|
| 532 | * with an array of records for each action. |
---|
| 533 | */ |
---|
| 534 | 'save' |
---|
| 535 | |
---|
| 536 | ); |
---|
| 537 | |
---|
| 538 | if(this.proxy){ |
---|
| 539 | // TODO remove deprecated loadexception with ext-3.0.1 |
---|
| 540 | this.relayEvents(this.proxy, ['loadexception', 'exception']); |
---|
| 541 | } |
---|
| 542 | // With a writer set for the Store, we want to listen to add/remove events to remotely create/destroy records. |
---|
| 543 | if (this.writer) { |
---|
| 544 | this.on({ |
---|
| 545 | scope: this, |
---|
| 546 | add: this.createRecords, |
---|
| 547 | remove: this.destroyRecord, |
---|
| 548 | update: this.updateRecord, |
---|
| 549 | clear: this.onClear |
---|
| 550 | }); |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | this.sortToggle = {}; |
---|
| 554 | if(this.sortField){ |
---|
| 555 | this.setDefaultSort(this.sortField, this.sortDir); |
---|
| 556 | }else if(this.sortInfo){ |
---|
| 557 | this.setDefaultSort(this.sortInfo.field, this.sortInfo.direction); |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | Ext.data.Store.superclass.constructor.call(this); |
---|
| 561 | |
---|
| 562 | if(this.id){ |
---|
| 563 | this.storeId = this.id; |
---|
| 564 | delete this.id; |
---|
| 565 | } |
---|
| 566 | if(this.storeId){ |
---|
| 567 | Ext.StoreMgr.register(this); |
---|
| 568 | } |
---|
| 569 | if(this.inlineData){ |
---|
| 570 | this.loadData(this.inlineData); |
---|
| 571 | delete this.inlineData; |
---|
| 572 | }else if(this.autoLoad){ |
---|
| 573 | this.load.defer(10, this, [ |
---|
| 574 | typeof this.autoLoad == 'object' ? |
---|
| 575 | this.autoLoad : undefined]); |
---|
| 576 | } |
---|
| 577 | // used internally to uniquely identify a batch |
---|
| 578 | this.batchCounter = 0; |
---|
| 579 | this.batches = {}; |
---|
| 580 | }, |
---|
| 581 | |
---|
| 582 | /** |
---|
| 583 | * builds a DataWriter instance when Store constructor is provided with a writer config-object instead of an instace. |
---|
| 584 | * @param {Object} config Writer configuration |
---|
| 585 | * @return {Ext.data.DataWriter} |
---|
| 586 | * @private |
---|
| 587 | */ |
---|
| 588 | buildWriter : function(config) { |
---|
| 589 | var klass = undefined, |
---|
| 590 | type = (config.format || 'json').toLowerCase(); |
---|
| 591 | switch (type) { |
---|
| 592 | case 'json': |
---|
| 593 | klass = Ext.data.JsonWriter; |
---|
| 594 | break; |
---|
| 595 | case 'xml': |
---|
| 596 | klass = Ext.data.XmlWriter; |
---|
| 597 | break; |
---|
| 598 | default: |
---|
| 599 | klass = Ext.data.JsonWriter; |
---|
| 600 | } |
---|
| 601 | return new klass(config); |
---|
| 602 | }, |
---|
| 603 | |
---|
| 604 | /** |
---|
| 605 | * Destroys the store. |
---|
| 606 | */ |
---|
| 607 | destroy : function(){ |
---|
| 608 | if(!this.isDestroyed){ |
---|
| 609 | if(this.storeId){ |
---|
| 610 | Ext.StoreMgr.unregister(this); |
---|
| 611 | } |
---|
| 612 | this.clearData(); |
---|
| 613 | this.data = null; |
---|
| 614 | Ext.destroy(this.proxy); |
---|
| 615 | this.reader = this.writer = null; |
---|
| 616 | this.purgeListeners(); |
---|
| 617 | this.isDestroyed = true; |
---|
| 618 | } |
---|
| 619 | }, |
---|
| 620 | |
---|
| 621 | /** |
---|
| 622 | * Add Records to the Store and fires the {@link #add} event. To add Records |
---|
| 623 | * to the store from a remote source use <code>{@link #load}({add:true})</code>. |
---|
| 624 | * See also <code>{@link #recordType}</code> and <code>{@link #insert}</code>. |
---|
| 625 | * @param {Ext.data.Record[]} records An Array of Ext.data.Record objects |
---|
| 626 | * to add to the cache. See {@link #recordType}. |
---|
| 627 | */ |
---|
| 628 | add : function(records) { |
---|
| 629 | var i, len, record, index; |
---|
| 630 | |
---|
| 631 | records = [].concat(records); |
---|
| 632 | if (records.length < 1) { |
---|
| 633 | return; |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | for (i = 0, len = records.length; i < len; i++) { |
---|
| 637 | record = records[i]; |
---|
| 638 | |
---|
| 639 | record.join(this); |
---|
| 640 | |
---|
| 641 | if (record.dirty || record.phantom) { |
---|
| 642 | this.modified.push(record); |
---|
| 643 | } |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | index = this.data.length; |
---|
| 647 | this.data.addAll(records); |
---|
| 648 | |
---|
| 649 | if (this.snapshot) { |
---|
| 650 | this.snapshot.addAll(records); |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | this.fireEvent('add', this, records, index); |
---|
| 654 | }, |
---|
| 655 | |
---|
| 656 | /** |
---|
| 657 | * (Local sort only) Inserts the passed Record into the Store at the index where it |
---|
| 658 | * should go based on the current sort information. |
---|
| 659 | * @param {Ext.data.Record} record |
---|
| 660 | */ |
---|
| 661 | addSorted : function(record){ |
---|
| 662 | var index = this.findInsertIndex(record); |
---|
| 663 | this.insert(index, record); |
---|
| 664 | }, |
---|
| 665 | |
---|
| 666 | /** |
---|
| 667 | * @private |
---|
| 668 | * Update a record within the store with a new reference |
---|
| 669 | */ |
---|
| 670 | doUpdate: function(rec){ |
---|
| 671 | var id = rec.id; |
---|
| 672 | // unjoin the old record |
---|
| 673 | this.getById(id).join(null); |
---|
| 674 | |
---|
| 675 | this.data.replace(id, rec); |
---|
| 676 | if (this.snapshot) { |
---|
| 677 | this.snapshot.replace(id, rec); |
---|
| 678 | } |
---|
| 679 | rec.join(this); |
---|
| 680 | this.fireEvent('update', this, rec, Ext.data.Record.COMMIT); |
---|
| 681 | }, |
---|
| 682 | |
---|
| 683 | /** |
---|
| 684 | * Remove Records from the Store and fires the {@link #remove} event. |
---|
| 685 | * @param {Ext.data.Record/Ext.data.Record[]} record The record object or array of records to remove from the cache. |
---|
| 686 | */ |
---|
| 687 | remove : function(record){ |
---|
| 688 | if(Ext.isArray(record)){ |
---|
| 689 | Ext.each(record, function(r){ |
---|
| 690 | this.remove(r); |
---|
| 691 | }, this); |
---|
| 692 | return; |
---|
| 693 | } |
---|
| 694 | var index = this.data.indexOf(record); |
---|
| 695 | if(index > -1){ |
---|
| 696 | record.join(null); |
---|
| 697 | this.data.removeAt(index); |
---|
| 698 | } |
---|
| 699 | if(this.pruneModifiedRecords){ |
---|
| 700 | this.modified.remove(record); |
---|
| 701 | } |
---|
| 702 | if(this.snapshot){ |
---|
| 703 | this.snapshot.remove(record); |
---|
| 704 | } |
---|
| 705 | if(index > -1){ |
---|
| 706 | this.fireEvent('remove', this, record, index); |
---|
| 707 | } |
---|
| 708 | }, |
---|
| 709 | |
---|
| 710 | /** |
---|
| 711 | * Remove a Record from the Store at the specified index. Fires the {@link #remove} event. |
---|
| 712 | * @param {Number} index The index of the record to remove. |
---|
| 713 | */ |
---|
| 714 | removeAt : function(index){ |
---|
| 715 | this.remove(this.getAt(index)); |
---|
| 716 | }, |
---|
| 717 | |
---|
| 718 | /** |
---|
| 719 | * Remove all Records from the Store and fires the {@link #clear} event. |
---|
| 720 | * @param {Boolean} silent [false] Defaults to <tt>false</tt>. Set <tt>true</tt> to not fire clear event. |
---|
| 721 | */ |
---|
| 722 | removeAll : function(silent){ |
---|
| 723 | var items = []; |
---|
| 724 | this.each(function(rec){ |
---|
| 725 | items.push(rec); |
---|
| 726 | }); |
---|
| 727 | this.clearData(); |
---|
| 728 | if(this.snapshot){ |
---|
| 729 | this.snapshot.clear(); |
---|
| 730 | } |
---|
| 731 | if(this.pruneModifiedRecords){ |
---|
| 732 | this.modified = []; |
---|
| 733 | } |
---|
| 734 | if (silent !== true) { // <-- prevents write-actions when we just want to clear a store. |
---|
| 735 | this.fireEvent('clear', this, items); |
---|
| 736 | } |
---|
| 737 | }, |
---|
| 738 | |
---|
| 739 | // private |
---|
| 740 | onClear: function(store, records){ |
---|
| 741 | Ext.each(records, function(rec, index){ |
---|
| 742 | this.destroyRecord(this, rec, index); |
---|
| 743 | }, this); |
---|
| 744 | }, |
---|
| 745 | |
---|
| 746 | /** |
---|
| 747 | * Inserts Records into the Store at the given index and fires the {@link #add} event. |
---|
| 748 | * See also <code>{@link #add}</code> and <code>{@link #addSorted}</code>. |
---|
| 749 | * @param {Number} index The start index at which to insert the passed Records. |
---|
| 750 | * @param {Ext.data.Record[]} records An Array of Ext.data.Record objects to add to the cache. |
---|
| 751 | */ |
---|
| 752 | insert : function(index, records) { |
---|
| 753 | var i, len, record; |
---|
| 754 | |
---|
| 755 | records = [].concat(records); |
---|
| 756 | for (i = 0, len = records.length; i < len; i++) { |
---|
| 757 | record = records[i]; |
---|
| 758 | |
---|
| 759 | this.data.insert(index + i, record); |
---|
| 760 | record.join(this); |
---|
| 761 | |
---|
| 762 | if (record.dirty || record.phantom) { |
---|
| 763 | this.modified.push(record); |
---|
| 764 | } |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | if (this.snapshot) { |
---|
| 768 | this.snapshot.addAll(records); |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | this.fireEvent('add', this, records, index); |
---|
| 772 | }, |
---|
| 773 | |
---|
| 774 | /** |
---|
| 775 | * Get the index within the cache of the passed Record. |
---|
| 776 | * @param {Ext.data.Record} record The Ext.data.Record object to find. |
---|
| 777 | * @return {Number} The index of the passed Record. Returns -1 if not found. |
---|
| 778 | */ |
---|
| 779 | indexOf : function(record){ |
---|
| 780 | return this.data.indexOf(record); |
---|
| 781 | }, |
---|
| 782 | |
---|
| 783 | /** |
---|
| 784 | * Get the index within the cache of the Record with the passed id. |
---|
| 785 | * @param {String} id The id of the Record to find. |
---|
| 786 | * @return {Number} The index of the Record. Returns -1 if not found. |
---|
| 787 | */ |
---|
| 788 | indexOfId : function(id){ |
---|
| 789 | return this.data.indexOfKey(id); |
---|
| 790 | }, |
---|
| 791 | |
---|
| 792 | /** |
---|
| 793 | * Get the Record with the specified id. |
---|
| 794 | * @param {String} id The id of the Record to find. |
---|
| 795 | * @return {Ext.data.Record} The Record with the passed id. Returns undefined if not found. |
---|
| 796 | */ |
---|
| 797 | getById : function(id){ |
---|
| 798 | return (this.snapshot || this.data).key(id); |
---|
| 799 | }, |
---|
| 800 | |
---|
| 801 | /** |
---|
| 802 | * Get the Record at the specified index. |
---|
| 803 | * @param {Number} index The index of the Record to find. |
---|
| 804 | * @return {Ext.data.Record} The Record at the passed index. Returns undefined if not found. |
---|
| 805 | */ |
---|
| 806 | getAt : function(index){ |
---|
| 807 | return this.data.itemAt(index); |
---|
| 808 | }, |
---|
| 809 | |
---|
| 810 | /** |
---|
| 811 | * Returns a range of Records between specified indices. |
---|
| 812 | * @param {Number} startIndex (optional) The starting index (defaults to 0) |
---|
| 813 | * @param {Number} endIndex (optional) The ending index (defaults to the last Record in the Store) |
---|
| 814 | * @return {Ext.data.Record[]} An array of Records |
---|
| 815 | */ |
---|
| 816 | getRange : function(start, end){ |
---|
| 817 | return this.data.getRange(start, end); |
---|
| 818 | }, |
---|
| 819 | |
---|
| 820 | // private |
---|
| 821 | storeOptions : function(o){ |
---|
| 822 | o = Ext.apply({}, o); |
---|
| 823 | delete o.callback; |
---|
| 824 | delete o.scope; |
---|
| 825 | this.lastOptions = o; |
---|
| 826 | }, |
---|
| 827 | |
---|
| 828 | // private |
---|
| 829 | clearData: function(){ |
---|
| 830 | this.data.each(function(rec) { |
---|
| 831 | rec.join(null); |
---|
| 832 | }); |
---|
| 833 | this.data.clear(); |
---|
| 834 | }, |
---|
| 835 | |
---|
| 836 | /** |
---|
| 837 | * <p>Loads the Record cache from the configured <tt>{@link #proxy}</tt> using the configured <tt>{@link #reader}</tt>.</p> |
---|
| 838 | * <br><p>Notes:</p><div class="mdetail-params"><ul> |
---|
| 839 | * <li><b><u>Important</u></b>: loading is asynchronous! This call will return before the new data has been |
---|
| 840 | * loaded. To perform any post-processing where information from the load call is required, specify |
---|
| 841 | * the <tt>callback</tt> function to be called, or use a {@link Ext.util.Observable#listeners a 'load' event handler}.</li> |
---|
| 842 | * <li>If using {@link Ext.PagingToolbar remote paging}, the first load call must specify the <tt>start</tt> and <tt>limit</tt> |
---|
| 843 | * properties in the <code>options.params</code> property to establish the initial position within the |
---|
| 844 | * dataset, and the number of Records to cache on each read from the Proxy.</li> |
---|
| 845 | * <li>If using {@link #remoteSort remote sorting}, the configured <code>{@link #sortInfo}</code> |
---|
| 846 | * will be automatically included with the posted parameters according to the specified |
---|
| 847 | * <code>{@link #paramNames}</code>.</li> |
---|
| 848 | * </ul></div> |
---|
| 849 | * @param {Object} options An object containing properties which control loading options:<ul> |
---|
| 850 | * <li><b><tt>params</tt></b> :Object<div class="sub-desc"><p>An object containing properties to pass as HTTP |
---|
| 851 | * parameters to a remote data source. <b>Note</b>: <code>params</code> will override any |
---|
| 852 | * <code>{@link #baseParams}</code> of the same name.</p> |
---|
| 853 | * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p></div></li> |
---|
| 854 | * <li><b>callback</b> : Function<div class="sub-desc"><p>A function to be called after the Records |
---|
| 855 | * have been loaded. The callback is called after the load event is fired, and is passed the following arguments:<ul> |
---|
| 856 | * <li>r : Ext.data.Record[] An Array of Records loaded.</li> |
---|
| 857 | * <li>options : Options object from the load call.</li> |
---|
| 858 | * <li>success : Boolean success indicator.</li></ul></p></div></li> |
---|
| 859 | * <li><b>scope</b> : Object<div class="sub-desc"><p>Scope with which to call the callback (defaults |
---|
| 860 | * to the Store object)</p></div></li> |
---|
| 861 | * <li><b>add</b> : Boolean<div class="sub-desc"><p>Indicator to append loaded records rather than |
---|
| 862 | * replace the current cache. <b>Note</b>: see note for <tt>{@link #loadData}</tt></p></div></li> |
---|
| 863 | * </ul> |
---|
| 864 | * @return {Boolean} If the <i>developer</i> provided <tt>{@link #beforeload}</tt> event handler returns |
---|
| 865 | * <tt>false</tt>, the load call will abort and will return <tt>false</tt>; otherwise will return <tt>true</tt>. |
---|
| 866 | */ |
---|
| 867 | load : function(options) { |
---|
| 868 | options = Ext.apply({}, options); |
---|
| 869 | this.storeOptions(options); |
---|
| 870 | if(this.sortInfo && this.remoteSort){ |
---|
| 871 | var pn = this.paramNames; |
---|
| 872 | options.params = Ext.apply({}, options.params); |
---|
| 873 | options.params[pn.sort] = this.sortInfo.field; |
---|
| 874 | options.params[pn.dir] = this.sortInfo.direction; |
---|
| 875 | } |
---|
| 876 | try { |
---|
| 877 | return this.execute('read', null, options); // <-- null represents rs. No rs for load actions. |
---|
| 878 | } catch(e) { |
---|
| 879 | this.handleException(e); |
---|
| 880 | return false; |
---|
| 881 | } |
---|
| 882 | }, |
---|
| 883 | |
---|
| 884 | /** |
---|
| 885 | * updateRecord Should not be used directly. This method will be called automatically if a Writer is set. |
---|
| 886 | * Listens to 'update' event. |
---|
| 887 | * @param {Object} store |
---|
| 888 | * @param {Object} record |
---|
| 889 | * @param {Object} action |
---|
| 890 | * @private |
---|
| 891 | */ |
---|
| 892 | updateRecord : function(store, record, action) { |
---|
| 893 | if (action == Ext.data.Record.EDIT && this.autoSave === true && (!record.phantom || (record.phantom && record.isValid()))) { |
---|
| 894 | this.save(); |
---|
| 895 | } |
---|
| 896 | }, |
---|
| 897 | |
---|
| 898 | /** |
---|
| 899 | * @private |
---|
| 900 | * Should not be used directly. Store#add will call this automatically if a Writer is set |
---|
| 901 | * @param {Object} store |
---|
| 902 | * @param {Object} records |
---|
| 903 | * @param {Object} index |
---|
| 904 | */ |
---|
| 905 | createRecords : function(store, records, index) { |
---|
| 906 | var modified = this.modified, |
---|
| 907 | length = records.length, |
---|
| 908 | record, i; |
---|
| 909 | |
---|
| 910 | for (i = 0; i < length; i++) { |
---|
| 911 | record = records[i]; |
---|
| 912 | |
---|
| 913 | if (record.phantom && record.isValid()) { |
---|
| 914 | record.markDirty(); // <-- Mark new records dirty (Ed: why?) |
---|
| 915 | |
---|
| 916 | if (modified.indexOf(record) == -1) { |
---|
| 917 | modified.push(record); |
---|
| 918 | } |
---|
| 919 | } |
---|
| 920 | } |
---|
| 921 | if (this.autoSave === true) { |
---|
| 922 | this.save(); |
---|
| 923 | } |
---|
| 924 | }, |
---|
| 925 | |
---|
| 926 | /** |
---|
| 927 | * Destroys a Record. Should not be used directly. It's called by Store#remove if a Writer is set. |
---|
| 928 | * @param {Store} store this |
---|
| 929 | * @param {Ext.data.Record} record |
---|
| 930 | * @param {Number} index |
---|
| 931 | * @private |
---|
| 932 | */ |
---|
| 933 | destroyRecord : function(store, record, index) { |
---|
| 934 | if (this.modified.indexOf(record) != -1) { // <-- handled already if @cfg pruneModifiedRecords == true |
---|
| 935 | this.modified.remove(record); |
---|
| 936 | } |
---|
| 937 | if (!record.phantom) { |
---|
| 938 | this.removed.push(record); |
---|
| 939 | |
---|
| 940 | // since the record has already been removed from the store but the server request has not yet been executed, |
---|
| 941 | // must keep track of the last known index this record existed. If a server error occurs, the record can be |
---|
| 942 | // put back into the store. @see Store#createCallback where the record is returned when response status === false |
---|
| 943 | record.lastIndex = index; |
---|
| 944 | |
---|
| 945 | if (this.autoSave === true) { |
---|
| 946 | this.save(); |
---|
| 947 | } |
---|
| 948 | } |
---|
| 949 | }, |
---|
| 950 | |
---|
| 951 | /** |
---|
| 952 | * This method should generally not be used directly. This method is called internally |
---|
| 953 | * by {@link #load}, or if a Writer is set will be called automatically when {@link #add}, |
---|
| 954 | * {@link #remove}, or {@link #update} events fire. |
---|
| 955 | * @param {String} action Action name ('read', 'create', 'update', or 'destroy') |
---|
| 956 | * @param {Record/Record[]} rs |
---|
| 957 | * @param {Object} options |
---|
| 958 | * @throws Error |
---|
| 959 | * @private |
---|
| 960 | */ |
---|
| 961 | execute : function(action, rs, options, /* private */ batch) { |
---|
| 962 | // blow up if action not Ext.data.CREATE, READ, UPDATE, DESTROY |
---|
| 963 | if (!Ext.data.Api.isAction(action)) { |
---|
| 964 | throw new Ext.data.Api.Error('execute', action); |
---|
| 965 | } |
---|
| 966 | // make sure options has a fresh, new params hash |
---|
| 967 | options = Ext.applyIf(options||{}, { |
---|
| 968 | params: {} |
---|
| 969 | }); |
---|
| 970 | if(batch !== undefined){ |
---|
| 971 | this.addToBatch(batch); |
---|
| 972 | } |
---|
| 973 | // have to separate before-events since load has a different signature than create,destroy and save events since load does not |
---|
| 974 | // include the rs (record resultset) parameter. Capture return values from the beforeaction into doRequest flag. |
---|
| 975 | var doRequest = true; |
---|
| 976 | |
---|
| 977 | if (action === 'read') { |
---|
| 978 | doRequest = this.fireEvent('beforeload', this, options); |
---|
| 979 | Ext.applyIf(options.params, this.baseParams); |
---|
| 980 | } |
---|
| 981 | else { |
---|
| 982 | // if Writer is configured as listful, force single-record rs to be [{}] instead of {} |
---|
| 983 | // TODO Move listful rendering into DataWriter where the @cfg is defined. Should be easy now. |
---|
| 984 | if (this.writer.listful === true && this.restful !== true) { |
---|
| 985 | rs = (Ext.isArray(rs)) ? rs : [rs]; |
---|
| 986 | } |
---|
| 987 | // if rs has just a single record, shift it off so that Writer writes data as '{}' rather than '[{}]' |
---|
| 988 | else if (Ext.isArray(rs) && rs.length == 1) { |
---|
| 989 | rs = rs.shift(); |
---|
| 990 | } |
---|
| 991 | // Write the action to options.params |
---|
| 992 | if ((doRequest = this.fireEvent('beforewrite', this, action, rs, options)) !== false) { |
---|
| 993 | this.writer.apply(options.params, this.baseParams, action, rs); |
---|
| 994 | } |
---|
| 995 | } |
---|
| 996 | if (doRequest !== false) { |
---|
| 997 | // Send request to proxy. |
---|
| 998 | if (this.writer && this.proxy.url && !this.proxy.restful && !Ext.data.Api.hasUniqueUrl(this.proxy, action)) { |
---|
| 999 | options.params.xaction = action; // <-- really old, probaby unecessary. |
---|
| 1000 | } |
---|
| 1001 | // Note: Up until this point we've been dealing with 'action' as a key from Ext.data.Api.actions. |
---|
| 1002 | // We'll flip it now and send the value into DataProxy#request, since it's the value which maps to |
---|
| 1003 | // the user's configured DataProxy#api |
---|
| 1004 | // TODO Refactor all Proxies to accept an instance of Ext.data.Request (not yet defined) instead of this looooooong list |
---|
| 1005 | // of params. This method is an artifact from Ext2. |
---|
| 1006 | this.proxy.request(Ext.data.Api.actions[action], rs, options.params, this.reader, this.createCallback(action, rs, batch), this, options); |
---|
| 1007 | } |
---|
| 1008 | return doRequest; |
---|
| 1009 | }, |
---|
| 1010 | |
---|
| 1011 | /** |
---|
| 1012 | * Saves all pending changes to the store. If the commensurate Ext.data.Api.actions action is not configured, then |
---|
| 1013 | * the configured <code>{@link #url}</code> will be used. |
---|
| 1014 | * <pre> |
---|
| 1015 | * change url |
---|
| 1016 | * --------------- -------------------- |
---|
| 1017 | * removed records Ext.data.Api.actions.destroy |
---|
| 1018 | * phantom records Ext.data.Api.actions.create |
---|
| 1019 | * {@link #getModifiedRecords modified records} Ext.data.Api.actions.update |
---|
| 1020 | * </pre> |
---|
| 1021 | * @TODO: Create extensions of Error class and send associated Record with thrown exceptions. |
---|
| 1022 | * e.g.: Ext.data.DataReader.Error or Ext.data.Error or Ext.data.DataProxy.Error, etc. |
---|
| 1023 | * @return {Number} batch Returns a number to uniquely identify the "batch" of saves occurring. -1 will be returned |
---|
| 1024 | * if there are no items to save or the save was cancelled. |
---|
| 1025 | */ |
---|
| 1026 | save : function() { |
---|
| 1027 | if (!this.writer) { |
---|
| 1028 | throw new Ext.data.Store.Error('writer-undefined'); |
---|
| 1029 | } |
---|
| 1030 | |
---|
| 1031 | var queue = [], |
---|
| 1032 | len, |
---|
| 1033 | trans, |
---|
| 1034 | batch, |
---|
| 1035 | data = {}, |
---|
| 1036 | i; |
---|
| 1037 | // DESTROY: First check for removed records. Records in this.removed are guaranteed non-phantoms. @see Store#remove |
---|
| 1038 | if(this.removed.length){ |
---|
| 1039 | queue.push(['destroy', this.removed]); |
---|
| 1040 | } |
---|
| 1041 | |
---|
| 1042 | // Check for modified records. Use a copy so Store#rejectChanges will work if server returns error. |
---|
| 1043 | var rs = [].concat(this.getModifiedRecords()); |
---|
| 1044 | if(rs.length){ |
---|
| 1045 | // CREATE: Next check for phantoms within rs. splice-off and execute create. |
---|
| 1046 | var phantoms = []; |
---|
| 1047 | for(i = rs.length-1; i >= 0; i--){ |
---|
| 1048 | if(rs[i].phantom === true){ |
---|
| 1049 | var rec = rs.splice(i, 1).shift(); |
---|
| 1050 | if(rec.isValid()){ |
---|
| 1051 | phantoms.push(rec); |
---|
| 1052 | } |
---|
| 1053 | }else if(!rs[i].isValid()){ // <-- while we're here, splice-off any !isValid real records |
---|
| 1054 | rs.splice(i,1); |
---|
| 1055 | } |
---|
| 1056 | } |
---|
| 1057 | // If we have valid phantoms, create them... |
---|
| 1058 | if(phantoms.length){ |
---|
| 1059 | queue.push(['create', phantoms]); |
---|
| 1060 | } |
---|
| 1061 | |
---|
| 1062 | // UPDATE: And finally, if we're still here after splicing-off phantoms and !isValid real records, update the rest... |
---|
| 1063 | if(rs.length){ |
---|
| 1064 | queue.push(['update', rs]); |
---|
| 1065 | } |
---|
| 1066 | } |
---|
| 1067 | len = queue.length; |
---|
| 1068 | if(len){ |
---|
| 1069 | batch = ++this.batchCounter; |
---|
| 1070 | for(i = 0; i < len; ++i){ |
---|
| 1071 | trans = queue[i]; |
---|
| 1072 | data[trans[0]] = trans[1]; |
---|
| 1073 | } |
---|
| 1074 | if(this.fireEvent('beforesave', this, data) !== false){ |
---|
| 1075 | for(i = 0; i < len; ++i){ |
---|
| 1076 | trans = queue[i]; |
---|
| 1077 | this.doTransaction(trans[0], trans[1], batch); |
---|
| 1078 | } |
---|
| 1079 | return batch; |
---|
| 1080 | } |
---|
| 1081 | } |
---|
| 1082 | return -1; |
---|
| 1083 | }, |
---|
| 1084 | |
---|
| 1085 | // private. Simply wraps call to Store#execute in try/catch. Defers to Store#handleException on error. Loops if batch: false |
---|
| 1086 | doTransaction : function(action, rs, batch) { |
---|
| 1087 | function transaction(records) { |
---|
| 1088 | try{ |
---|
| 1089 | this.execute(action, records, undefined, batch); |
---|
| 1090 | }catch (e){ |
---|
| 1091 | this.handleException(e); |
---|
| 1092 | } |
---|
| 1093 | } |
---|
| 1094 | if(this.batch === false){ |
---|
| 1095 | for(var i = 0, len = rs.length; i < len; i++){ |
---|
| 1096 | transaction.call(this, rs[i]); |
---|
| 1097 | } |
---|
| 1098 | }else{ |
---|
| 1099 | transaction.call(this, rs); |
---|
| 1100 | } |
---|
| 1101 | }, |
---|
| 1102 | |
---|
| 1103 | // private |
---|
| 1104 | addToBatch : function(batch){ |
---|
| 1105 | var b = this.batches, |
---|
| 1106 | key = this.batchKey + batch, |
---|
| 1107 | o = b[key]; |
---|
| 1108 | |
---|
| 1109 | if(!o){ |
---|
| 1110 | b[key] = o = { |
---|
| 1111 | id: batch, |
---|
| 1112 | count: 0, |
---|
| 1113 | data: {} |
---|
| 1114 | }; |
---|
| 1115 | } |
---|
| 1116 | ++o.count; |
---|
| 1117 | }, |
---|
| 1118 | |
---|
| 1119 | removeFromBatch : function(batch, action, data){ |
---|
| 1120 | var b = this.batches, |
---|
| 1121 | key = this.batchKey + batch, |
---|
| 1122 | o = b[key], |
---|
| 1123 | arr; |
---|
| 1124 | |
---|
| 1125 | |
---|
| 1126 | if(o){ |
---|
| 1127 | arr = o.data[action] || []; |
---|
| 1128 | o.data[action] = arr.concat(data); |
---|
| 1129 | if(o.count === 1){ |
---|
| 1130 | data = o.data; |
---|
| 1131 | delete b[key]; |
---|
| 1132 | this.fireEvent('save', this, batch, data); |
---|
| 1133 | }else{ |
---|
| 1134 | --o.count; |
---|
| 1135 | } |
---|
| 1136 | } |
---|
| 1137 | }, |
---|
| 1138 | |
---|
| 1139 | // @private callback-handler for remote CRUD actions |
---|
| 1140 | // Do not override -- override loadRecords, onCreateRecords, onDestroyRecords and onUpdateRecords instead. |
---|
| 1141 | createCallback : function(action, rs, batch) { |
---|
| 1142 | var actions = Ext.data.Api.actions; |
---|
| 1143 | return (action == 'read') ? this.loadRecords : function(data, response, success) { |
---|
| 1144 | // calls: onCreateRecords | onUpdateRecords | onDestroyRecords |
---|
| 1145 | this['on' + Ext.util.Format.capitalize(action) + 'Records'](success, rs, [].concat(data)); |
---|
| 1146 | // If success === false here, exception will have been called in DataProxy |
---|
| 1147 | if (success === true) { |
---|
| 1148 | this.fireEvent('write', this, action, data, response, rs); |
---|
| 1149 | } |
---|
| 1150 | this.removeFromBatch(batch, action, data); |
---|
| 1151 | }; |
---|
| 1152 | }, |
---|
| 1153 | |
---|
| 1154 | // Clears records from modified array after an exception event. |
---|
| 1155 | // NOTE: records are left marked dirty. Do we want to commit them even though they were not updated/realized? |
---|
| 1156 | // TODO remove this method? |
---|
| 1157 | clearModified : function(rs) { |
---|
| 1158 | if (Ext.isArray(rs)) { |
---|
| 1159 | for (var n=rs.length-1;n>=0;n--) { |
---|
| 1160 | this.modified.splice(this.modified.indexOf(rs[n]), 1); |
---|
| 1161 | } |
---|
| 1162 | } else { |
---|
| 1163 | this.modified.splice(this.modified.indexOf(rs), 1); |
---|
| 1164 | } |
---|
| 1165 | }, |
---|
| 1166 | |
---|
| 1167 | // remap record ids in MixedCollection after records have been realized. @see Store#onCreateRecords, @see DataReader#realize |
---|
| 1168 | reMap : function(record) { |
---|
| 1169 | if (Ext.isArray(record)) { |
---|
| 1170 | for (var i = 0, len = record.length; i < len; i++) { |
---|
| 1171 | this.reMap(record[i]); |
---|
| 1172 | } |
---|
| 1173 | } else { |
---|
| 1174 | delete this.data.map[record._phid]; |
---|
| 1175 | this.data.map[record.id] = record; |
---|
| 1176 | var index = this.data.keys.indexOf(record._phid); |
---|
| 1177 | this.data.keys.splice(index, 1, record.id); |
---|
| 1178 | delete record._phid; |
---|
| 1179 | } |
---|
| 1180 | }, |
---|
| 1181 | |
---|
| 1182 | // @protected onCreateRecord proxy callback for create action |
---|
| 1183 | onCreateRecords : function(success, rs, data) { |
---|
| 1184 | if (success === true) { |
---|
| 1185 | try { |
---|
| 1186 | this.reader.realize(rs, data); |
---|
| 1187 | } |
---|
| 1188 | catch (e) { |
---|
| 1189 | this.handleException(e); |
---|
| 1190 | if (Ext.isArray(rs)) { |
---|
| 1191 | // Recurse to run back into the try {}. DataReader#realize splices-off the rs until empty. |
---|
| 1192 | this.onCreateRecords(success, rs, data); |
---|
| 1193 | } |
---|
| 1194 | } |
---|
| 1195 | } |
---|
| 1196 | }, |
---|
| 1197 | |
---|
| 1198 | // @protected, onUpdateRecords proxy callback for update action |
---|
| 1199 | onUpdateRecords : function(success, rs, data) { |
---|
| 1200 | if (success === true) { |
---|
| 1201 | try { |
---|
| 1202 | this.reader.update(rs, data); |
---|
| 1203 | } catch (e) { |
---|
| 1204 | this.handleException(e); |
---|
| 1205 | if (Ext.isArray(rs)) { |
---|
| 1206 | // Recurse to run back into the try {}. DataReader#update splices-off the rs until empty. |
---|
| 1207 | this.onUpdateRecords(success, rs, data); |
---|
| 1208 | } |
---|
| 1209 | } |
---|
| 1210 | } |
---|
| 1211 | }, |
---|
| 1212 | |
---|
| 1213 | // @protected onDestroyRecords proxy callback for destroy action |
---|
| 1214 | onDestroyRecords : function(success, rs, data) { |
---|
| 1215 | // splice each rec out of this.removed |
---|
| 1216 | rs = (rs instanceof Ext.data.Record) ? [rs] : [].concat(rs); |
---|
| 1217 | for (var i=0,len=rs.length;i<len;i++) { |
---|
| 1218 | this.removed.splice(this.removed.indexOf(rs[i]), 1); |
---|
| 1219 | } |
---|
| 1220 | if (success === false) { |
---|
| 1221 | // put records back into store if remote destroy fails. |
---|
| 1222 | // @TODO: Might want to let developer decide. |
---|
| 1223 | for (i=rs.length-1;i>=0;i--) { |
---|
| 1224 | this.insert(rs[i].lastIndex, rs[i]); // <-- lastIndex set in Store#destroyRecord |
---|
| 1225 | } |
---|
| 1226 | } |
---|
| 1227 | }, |
---|
| 1228 | |
---|
| 1229 | // protected handleException. Possibly temporary until Ext framework has an exception-handler. |
---|
| 1230 | handleException : function(e) { |
---|
| 1231 | // @see core/Error.js |
---|
| 1232 | Ext.handleError(e); |
---|
| 1233 | }, |
---|
| 1234 | |
---|
| 1235 | /** |
---|
| 1236 | * <p>Reloads the Record cache from the configured Proxy using the configured |
---|
| 1237 | * {@link Ext.data.Reader Reader} and the options from the last load operation |
---|
| 1238 | * performed.</p> |
---|
| 1239 | * <p><b>Note</b>: see the Important note in {@link #load}.</p> |
---|
| 1240 | * @param {Object} options <p>(optional) An <tt>Object</tt> containing |
---|
| 1241 | * {@link #load loading options} which may override the {@link #lastOptions options} |
---|
| 1242 | * used in the last {@link #load} operation. See {@link #load} for details |
---|
| 1243 | * (defaults to <tt>null</tt>, in which case the {@link #lastOptions} are |
---|
| 1244 | * used).</p> |
---|
| 1245 | * <br><p>To add new params to the existing params:</p><pre><code> |
---|
| 1246 | lastOptions = myStore.lastOptions; |
---|
| 1247 | Ext.apply(lastOptions.params, { |
---|
| 1248 | myNewParam: true |
---|
| 1249 | }); |
---|
| 1250 | myStore.reload(lastOptions); |
---|
| 1251 | * </code></pre> |
---|
| 1252 | */ |
---|
| 1253 | reload : function(options){ |
---|
| 1254 | this.load(Ext.applyIf(options||{}, this.lastOptions)); |
---|
| 1255 | }, |
---|
| 1256 | |
---|
| 1257 | // private |
---|
| 1258 | // Called as a callback by the Reader during a load operation. |
---|
| 1259 | loadRecords : function(o, options, success){ |
---|
| 1260 | var i, len; |
---|
| 1261 | |
---|
| 1262 | if (this.isDestroyed === true) { |
---|
| 1263 | return; |
---|
| 1264 | } |
---|
| 1265 | if(!o || success === false){ |
---|
| 1266 | if(success !== false){ |
---|
| 1267 | this.fireEvent('load', this, [], options); |
---|
| 1268 | } |
---|
| 1269 | if(options.callback){ |
---|
| 1270 | options.callback.call(options.scope || this, [], options, false, o); |
---|
| 1271 | } |
---|
| 1272 | return; |
---|
| 1273 | } |
---|
| 1274 | var r = o.records, t = o.totalRecords || r.length; |
---|
| 1275 | if(!options || options.add !== true){ |
---|
| 1276 | if(this.pruneModifiedRecords){ |
---|
| 1277 | this.modified = []; |
---|
| 1278 | } |
---|
| 1279 | for(i = 0, len = r.length; i < len; i++){ |
---|
| 1280 | r[i].join(this); |
---|
| 1281 | } |
---|
| 1282 | if(this.snapshot){ |
---|
| 1283 | this.data = this.snapshot; |
---|
| 1284 | delete this.snapshot; |
---|
| 1285 | } |
---|
| 1286 | this.clearData(); |
---|
| 1287 | this.data.addAll(r); |
---|
| 1288 | this.totalLength = t; |
---|
| 1289 | this.applySort(); |
---|
| 1290 | this.fireEvent('datachanged', this); |
---|
| 1291 | }else{ |
---|
| 1292 | var toAdd = [], |
---|
| 1293 | rec, |
---|
| 1294 | cnt = 0; |
---|
| 1295 | for(i = 0, len = r.length; i < len; ++i){ |
---|
| 1296 | rec = r[i]; |
---|
| 1297 | if(this.indexOfId(rec.id) > -1){ |
---|
| 1298 | this.doUpdate(rec); |
---|
| 1299 | }else{ |
---|
| 1300 | toAdd.push(rec); |
---|
| 1301 | ++cnt; |
---|
| 1302 | } |
---|
| 1303 | } |
---|
| 1304 | this.totalLength = Math.max(t, this.data.length + cnt); |
---|
| 1305 | this.add(toAdd); |
---|
| 1306 | } |
---|
| 1307 | this.fireEvent('load', this, r, options); |
---|
| 1308 | if(options.callback){ |
---|
| 1309 | options.callback.call(options.scope || this, r, options, true); |
---|
| 1310 | } |
---|
| 1311 | }, |
---|
| 1312 | |
---|
| 1313 | /** |
---|
| 1314 | * Loads data from a passed data block and fires the {@link #load} event. A {@link Ext.data.Reader Reader} |
---|
| 1315 | * which understands the format of the data must have been configured in the constructor. |
---|
| 1316 | * @param {Object} data The data block from which to read the Records. The format of the data expected |
---|
| 1317 | * is dependent on the type of {@link Ext.data.Reader Reader} that is configured and should correspond to |
---|
| 1318 | * that {@link Ext.data.Reader Reader}'s <tt>{@link Ext.data.Reader#readRecords}</tt> parameter. |
---|
| 1319 | * @param {Boolean} append (Optional) <tt>true</tt> to append the new Records rather the default to replace |
---|
| 1320 | * the existing cache. |
---|
| 1321 | * <b>Note</b>: that Records in a Store are keyed by their {@link Ext.data.Record#id id}, so added Records |
---|
| 1322 | * with ids which are already present in the Store will <i>replace</i> existing Records. Only Records with |
---|
| 1323 | * new, unique ids will be added. |
---|
| 1324 | */ |
---|
| 1325 | loadData : function(o, append){ |
---|
| 1326 | var r = this.reader.readRecords(o); |
---|
| 1327 | this.loadRecords(r, {add: append}, true); |
---|
| 1328 | }, |
---|
| 1329 | |
---|
| 1330 | /** |
---|
| 1331 | * Gets the number of cached records. |
---|
| 1332 | * <p>If using paging, this may not be the total size of the dataset. If the data object |
---|
| 1333 | * used by the Reader contains the dataset size, then the {@link #getTotalCount} function returns |
---|
| 1334 | * the dataset size. <b>Note</b>: see the Important note in {@link #load}.</p> |
---|
| 1335 | * @return {Number} The number of Records in the Store's cache. |
---|
| 1336 | */ |
---|
| 1337 | getCount : function(){ |
---|
| 1338 | return this.data.length || 0; |
---|
| 1339 | }, |
---|
| 1340 | |
---|
| 1341 | /** |
---|
| 1342 | * Gets the total number of records in the dataset as returned by the server. |
---|
| 1343 | * <p>If using paging, for this to be accurate, the data object used by the {@link #reader Reader} |
---|
| 1344 | * must contain the dataset size. For remote data sources, the value for this property |
---|
| 1345 | * (<tt>totalProperty</tt> for {@link Ext.data.JsonReader JsonReader}, |
---|
| 1346 | * <tt>totalRecords</tt> for {@link Ext.data.XmlReader XmlReader}) shall be returned by a query on the server. |
---|
| 1347 | * <b>Note</b>: see the Important note in {@link #load}.</p> |
---|
| 1348 | * @return {Number} The number of Records as specified in the data object passed to the Reader |
---|
| 1349 | * by the Proxy. |
---|
| 1350 | * <p><b>Note</b>: this value is not updated when changing the contents of the Store locally.</p> |
---|
| 1351 | */ |
---|
| 1352 | getTotalCount : function(){ |
---|
| 1353 | return this.totalLength || 0; |
---|
| 1354 | }, |
---|
| 1355 | |
---|
| 1356 | /** |
---|
| 1357 | * Returns an object describing the current sort state of this Store. |
---|
| 1358 | * @return {Object} The sort state of the Store. An object with two properties:<ul> |
---|
| 1359 | * <li><b>field : String<p class="sub-desc">The name of the field by which the Records are sorted.</p></li> |
---|
| 1360 | * <li><b>direction : String<p class="sub-desc">The sort order, 'ASC' or 'DESC' (case-sensitive).</p></li> |
---|
| 1361 | * </ul> |
---|
| 1362 | * See <tt>{@link #sortInfo}</tt> for additional details. |
---|
| 1363 | */ |
---|
| 1364 | getSortState : function(){ |
---|
| 1365 | return this.sortInfo; |
---|
| 1366 | }, |
---|
| 1367 | |
---|
| 1368 | /** |
---|
| 1369 | * @private |
---|
| 1370 | * Invokes sortData if we have sortInfo to sort on and are not sorting remotely |
---|
| 1371 | */ |
---|
| 1372 | applySort : function(){ |
---|
| 1373 | if ((this.sortInfo || this.multiSortInfo) && !this.remoteSort) { |
---|
| 1374 | this.sortData(); |
---|
| 1375 | } |
---|
| 1376 | }, |
---|
| 1377 | |
---|
| 1378 | /** |
---|
| 1379 | * @private |
---|
| 1380 | * Performs the actual sorting of data. This checks to see if we currently have a multi sort or not. It applies |
---|
| 1381 | * each sorter field/direction pair in turn by building an OR'ed master sorting function and running it against |
---|
| 1382 | * the full dataset |
---|
| 1383 | */ |
---|
| 1384 | sortData : function() { |
---|
| 1385 | var sortInfo = this.hasMultiSort ? this.multiSortInfo : this.sortInfo, |
---|
| 1386 | direction = sortInfo.direction || "ASC", |
---|
| 1387 | sorters = sortInfo.sorters, |
---|
| 1388 | sortFns = []; |
---|
| 1389 | |
---|
| 1390 | //if we just have a single sorter, pretend it's the first in an array |
---|
| 1391 | if (!this.hasMultiSort) { |
---|
| 1392 | sorters = [{direction: direction, field: sortInfo.field}]; |
---|
| 1393 | } |
---|
| 1394 | |
---|
| 1395 | //create a sorter function for each sorter field/direction combo |
---|
| 1396 | for (var i=0, j = sorters.length; i < j; i++) { |
---|
| 1397 | sortFns.push(this.createSortFunction(sorters[i].field, sorters[i].direction)); |
---|
| 1398 | } |
---|
| 1399 | |
---|
| 1400 | if (sortFns.length == 0) { |
---|
| 1401 | return; |
---|
| 1402 | } |
---|
| 1403 | |
---|
| 1404 | //the direction modifier is multiplied with the result of the sorting functions to provide overall sort direction |
---|
| 1405 | //(as opposed to direction per field) |
---|
| 1406 | var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1; |
---|
| 1407 | |
---|
| 1408 | //create a function which ORs each sorter together to enable multi-sort |
---|
| 1409 | var fn = function(r1, r2) { |
---|
| 1410 | var result = sortFns[0].call(this, r1, r2); |
---|
| 1411 | |
---|
| 1412 | //if we have more than one sorter, OR any additional sorter functions together |
---|
| 1413 | if (sortFns.length > 1) { |
---|
| 1414 | for (var i=1, j = sortFns.length; i < j; i++) { |
---|
| 1415 | result = result || sortFns[i].call(this, r1, r2); |
---|
| 1416 | } |
---|
| 1417 | } |
---|
| 1418 | |
---|
| 1419 | return directionModifier * result; |
---|
| 1420 | }; |
---|
| 1421 | |
---|
| 1422 | //sort the data |
---|
| 1423 | this.data.sort(direction, fn); |
---|
| 1424 | if (this.snapshot && this.snapshot != this.data) { |
---|
| 1425 | this.snapshot.sort(direction, fn); |
---|
| 1426 | } |
---|
| 1427 | }, |
---|
| 1428 | |
---|
| 1429 | /** |
---|
| 1430 | * @private |
---|
| 1431 | * Creates and returns a function which sorts an array by the given field and direction |
---|
| 1432 | * @param {String} field The field to create the sorter for |
---|
| 1433 | * @param {String} direction The direction to sort by (defaults to "ASC") |
---|
| 1434 | * @return {Function} A function which sorts by the field/direction combination provided |
---|
| 1435 | */ |
---|
| 1436 | createSortFunction: function(field, direction) { |
---|
| 1437 | direction = direction || "ASC"; |
---|
| 1438 | var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1; |
---|
| 1439 | |
---|
| 1440 | var sortType = this.fields.get(field).sortType; |
---|
| 1441 | |
---|
| 1442 | //create a comparison function. Takes 2 records, returns 1 if record 1 is greater, |
---|
| 1443 | //-1 if record 2 is greater or 0 if they are equal |
---|
| 1444 | return function(r1, r2) { |
---|
| 1445 | var v1 = sortType(r1.data[field]), |
---|
| 1446 | v2 = sortType(r2.data[field]); |
---|
| 1447 | |
---|
| 1448 | return directionModifier * (v1 > v2 ? 1 : (v1 < v2 ? -1 : 0)); |
---|
| 1449 | }; |
---|
| 1450 | }, |
---|
| 1451 | |
---|
| 1452 | /** |
---|
| 1453 | * Sets the default sort column and order to be used by the next {@link #load} operation. |
---|
| 1454 | * @param {String} fieldName The name of the field to sort by. |
---|
| 1455 | * @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>) |
---|
| 1456 | */ |
---|
| 1457 | setDefaultSort : function(field, dir) { |
---|
| 1458 | dir = dir ? dir.toUpperCase() : 'ASC'; |
---|
| 1459 | this.sortInfo = {field: field, direction: dir}; |
---|
| 1460 | this.sortToggle[field] = dir; |
---|
| 1461 | }, |
---|
| 1462 | |
---|
| 1463 | /** |
---|
| 1464 | * Sort the Records. |
---|
| 1465 | * If remote sorting is used, the sort is performed on the server, and the cache is reloaded. If local |
---|
| 1466 | * sorting is used, the cache is sorted internally. See also {@link #remoteSort} and {@link #paramNames}. |
---|
| 1467 | * This function accepts two call signatures - pass in a field name as the first argument to sort on a single |
---|
| 1468 | * field, or pass in an array of sort configuration objects to sort by multiple fields. |
---|
| 1469 | * Single sort example: |
---|
| 1470 | * store.sort('name', 'ASC'); |
---|
| 1471 | * Multi sort example: |
---|
| 1472 | * store.sort([ |
---|
| 1473 | * { |
---|
| 1474 | * field : 'name', |
---|
| 1475 | * direction: 'ASC' |
---|
| 1476 | * }, |
---|
| 1477 | * { |
---|
| 1478 | * field : 'salary', |
---|
| 1479 | * direction: 'DESC' |
---|
| 1480 | * } |
---|
| 1481 | * ], 'ASC'); |
---|
| 1482 | * In this second form, the sort configs are applied in order, with later sorters sorting within earlier sorters' results. |
---|
| 1483 | * For example, if two records with the same name are present they will also be sorted by salary if given the sort configs |
---|
| 1484 | * above. Any number of sort configs can be added. |
---|
| 1485 | * @param {String/Array} fieldName The name of the field to sort by, or an array of ordered sort configs |
---|
| 1486 | * @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>) |
---|
| 1487 | */ |
---|
| 1488 | sort : function(fieldName, dir) { |
---|
| 1489 | if (Ext.isArray(arguments[0])) { |
---|
| 1490 | return this.multiSort.call(this, fieldName, dir); |
---|
| 1491 | } else { |
---|
| 1492 | return this.singleSort(fieldName, dir); |
---|
| 1493 | } |
---|
| 1494 | }, |
---|
| 1495 | |
---|
| 1496 | /** |
---|
| 1497 | * Sorts the store contents by a single field and direction. This is called internally by {@link sort} and would |
---|
| 1498 | * not usually be called manually |
---|
| 1499 | * @param {String} fieldName The name of the field to sort by. |
---|
| 1500 | * @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>) |
---|
| 1501 | */ |
---|
| 1502 | singleSort: function(fieldName, dir) { |
---|
| 1503 | var field = this.fields.get(fieldName); |
---|
| 1504 | if (!field) { |
---|
| 1505 | return false; |
---|
| 1506 | } |
---|
| 1507 | |
---|
| 1508 | var name = field.name, |
---|
| 1509 | sortInfo = this.sortInfo || null, |
---|
| 1510 | sortToggle = this.sortToggle ? this.sortToggle[name] : null; |
---|
| 1511 | |
---|
| 1512 | if (!dir) { |
---|
| 1513 | if (sortInfo && sortInfo.field == name) { // toggle sort dir |
---|
| 1514 | dir = (this.sortToggle[name] || 'ASC').toggle('ASC', 'DESC'); |
---|
| 1515 | } else { |
---|
| 1516 | dir = field.sortDir; |
---|
| 1517 | } |
---|
| 1518 | } |
---|
| 1519 | |
---|
| 1520 | this.sortToggle[name] = dir; |
---|
| 1521 | this.sortInfo = {field: name, direction: dir}; |
---|
| 1522 | this.hasMultiSort = false; |
---|
| 1523 | |
---|
| 1524 | if (this.remoteSort) { |
---|
| 1525 | if (!this.load(this.lastOptions)) { |
---|
| 1526 | if (sortToggle) { |
---|
| 1527 | this.sortToggle[name] = sortToggle; |
---|
| 1528 | } |
---|
| 1529 | if (sortInfo) { |
---|
| 1530 | this.sortInfo = sortInfo; |
---|
| 1531 | } |
---|
| 1532 | } |
---|
| 1533 | } else { |
---|
| 1534 | this.applySort(); |
---|
| 1535 | this.fireEvent('datachanged', this); |
---|
| 1536 | } |
---|
| 1537 | return true; |
---|
| 1538 | }, |
---|
| 1539 | |
---|
| 1540 | /** |
---|
| 1541 | * Sorts the contents of this store by multiple field/direction sorters. This is called internally by {@link sort} |
---|
| 1542 | * and would not usually be called manually. |
---|
| 1543 | * Multi sorting only currently applies to local datasets - multiple sort data is not currently sent to a proxy |
---|
| 1544 | * if remoteSort is used. |
---|
| 1545 | * @param {Array} sorters Array of sorter objects (field and direction) |
---|
| 1546 | * @param {String} direction Overall direction to sort the ordered results by (defaults to "ASC") |
---|
| 1547 | */ |
---|
| 1548 | multiSort: function(sorters, direction) { |
---|
| 1549 | this.hasMultiSort = true; |
---|
| 1550 | direction = direction || "ASC"; |
---|
| 1551 | |
---|
| 1552 | //toggle sort direction |
---|
| 1553 | if (this.multiSortInfo && direction == this.multiSortInfo.direction) { |
---|
| 1554 | direction = direction.toggle("ASC", "DESC"); |
---|
| 1555 | } |
---|
| 1556 | |
---|
| 1557 | /** |
---|
| 1558 | * Object containing overall sort direction and an ordered array of sorter configs used when sorting on multiple fields |
---|
| 1559 | * @property multiSortInfo |
---|
| 1560 | * @type Object |
---|
| 1561 | */ |
---|
| 1562 | this.multiSortInfo = { |
---|
| 1563 | sorters : sorters, |
---|
| 1564 | direction: direction |
---|
| 1565 | }; |
---|
| 1566 | |
---|
| 1567 | if (this.remoteSort) { |
---|
| 1568 | this.singleSort(sorters[0].field, sorters[0].direction); |
---|
| 1569 | |
---|
| 1570 | } else { |
---|
| 1571 | this.applySort(); |
---|
| 1572 | this.fireEvent('datachanged', this); |
---|
| 1573 | } |
---|
| 1574 | }, |
---|
| 1575 | |
---|
| 1576 | /** |
---|
| 1577 | * Calls the specified function for each of the {@link Ext.data.Record Records} in the cache. |
---|
| 1578 | * @param {Function} fn The function to call. The {@link Ext.data.Record Record} is passed as the first parameter. |
---|
| 1579 | * Returning <tt>false</tt> aborts and exits the iteration. |
---|
| 1580 | * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. |
---|
| 1581 | * Defaults to the current {@link Ext.data.Record Record} in the iteration. |
---|
| 1582 | */ |
---|
| 1583 | each : function(fn, scope){ |
---|
| 1584 | this.data.each(fn, scope); |
---|
| 1585 | }, |
---|
| 1586 | |
---|
| 1587 | /** |
---|
| 1588 | * Gets all {@link Ext.data.Record records} modified since the last commit. Modified records are |
---|
| 1589 | * persisted across load operations (e.g., during paging). <b>Note</b>: deleted records are not |
---|
| 1590 | * included. See also <tt>{@link #pruneModifiedRecords}</tt> and |
---|
| 1591 | * {@link Ext.data.Record}<tt>{@link Ext.data.Record#markDirty markDirty}.</tt>. |
---|
| 1592 | * @return {Ext.data.Record[]} An array of {@link Ext.data.Record Records} containing outstanding |
---|
| 1593 | * modifications. To obtain modified fields within a modified record see |
---|
| 1594 | *{@link Ext.data.Record}<tt>{@link Ext.data.Record#modified modified}.</tt>. |
---|
| 1595 | */ |
---|
| 1596 | getModifiedRecords : function(){ |
---|
| 1597 | return this.modified; |
---|
| 1598 | }, |
---|
| 1599 | |
---|
| 1600 | /** |
---|
| 1601 | * Sums the value of <tt>property</tt> for each {@link Ext.data.Record record} between <tt>start</tt> |
---|
| 1602 | * and <tt>end</tt> and returns the result. |
---|
| 1603 | * @param {String} property A field in each record |
---|
| 1604 | * @param {Number} start (optional) The record index to start at (defaults to <tt>0</tt>) |
---|
| 1605 | * @param {Number} end (optional) The last record index to include (defaults to length - 1) |
---|
| 1606 | * @return {Number} The sum |
---|
| 1607 | */ |
---|
| 1608 | sum : function(property, start, end){ |
---|
| 1609 | var rs = this.data.items, v = 0; |
---|
| 1610 | start = start || 0; |
---|
| 1611 | end = (end || end === 0) ? end : rs.length-1; |
---|
| 1612 | |
---|
| 1613 | for(var i = start; i <= end; i++){ |
---|
| 1614 | v += (rs[i].data[property] || 0); |
---|
| 1615 | } |
---|
| 1616 | return v; |
---|
| 1617 | }, |
---|
| 1618 | |
---|
| 1619 | /** |
---|
| 1620 | * @private |
---|
| 1621 | * Returns a filter function used to test a the given property's value. Defers most of the work to |
---|
| 1622 | * Ext.util.MixedCollection's createValueMatcher function |
---|
| 1623 | * @param {String} property The property to create the filter function for |
---|
| 1624 | * @param {String/RegExp} value The string/regex to compare the property value to |
---|
| 1625 | * @param {Boolean} anyMatch True if we don't care if the filter value is not the full value (defaults to false) |
---|
| 1626 | * @param {Boolean} caseSensitive True to create a case-sensitive regex (defaults to false) |
---|
| 1627 | * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true. |
---|
| 1628 | */ |
---|
| 1629 | createFilterFn : function(property, value, anyMatch, caseSensitive, exactMatch){ |
---|
| 1630 | if(Ext.isEmpty(value, false)){ |
---|
| 1631 | return false; |
---|
| 1632 | } |
---|
| 1633 | value = this.data.createValueMatcher(value, anyMatch, caseSensitive, exactMatch); |
---|
| 1634 | return function(r) { |
---|
| 1635 | return value.test(r.data[property]); |
---|
| 1636 | }; |
---|
| 1637 | }, |
---|
| 1638 | |
---|
| 1639 | /** |
---|
| 1640 | * @private |
---|
| 1641 | * Given an array of filter functions (each with optional scope), constructs and returns a single function that returns |
---|
| 1642 | * the result of all of the filters ANDed together |
---|
| 1643 | * @param {Array} filters The array of filter objects (each object should contain an 'fn' and optional scope) |
---|
| 1644 | * @return {Function} The multiple filter function |
---|
| 1645 | */ |
---|
| 1646 | createMultipleFilterFn: function(filters) { |
---|
| 1647 | return function(record) { |
---|
| 1648 | var isMatch = true; |
---|
| 1649 | |
---|
| 1650 | for (var i=0, j = filters.length; i < j; i++) { |
---|
| 1651 | var filter = filters[i], |
---|
| 1652 | fn = filter.fn, |
---|
| 1653 | scope = filter.scope; |
---|
| 1654 | |
---|
| 1655 | isMatch = isMatch && fn.call(scope, record); |
---|
| 1656 | } |
---|
| 1657 | |
---|
| 1658 | return isMatch; |
---|
| 1659 | }; |
---|
| 1660 | }, |
---|
| 1661 | |
---|
| 1662 | /** |
---|
| 1663 | * Filter the {@link Ext.data.Record records} by a specified property. Alternatively, pass an array of filter |
---|
| 1664 | * options to filter by more than one property. |
---|
| 1665 | * Single filter example: |
---|
| 1666 | * store.filter('name', 'Ed', true, true); //finds all records containing the substring 'Ed' |
---|
| 1667 | * Multiple filter example: |
---|
| 1668 | * <pre><code> |
---|
| 1669 | * store.filter([ |
---|
| 1670 | * { |
---|
| 1671 | * property : 'name', |
---|
| 1672 | * value : 'Ed', |
---|
| 1673 | * anyMatch : true, //optional, defaults to true |
---|
| 1674 | * caseSensitive: true //optional, defaults to true |
---|
| 1675 | * }, |
---|
| 1676 | * |
---|
| 1677 | * //filter functions can also be passed |
---|
| 1678 | * { |
---|
| 1679 | * fn : function(record) { |
---|
| 1680 | * return record.get('age') == 24 |
---|
| 1681 | * }, |
---|
| 1682 | * scope: this |
---|
| 1683 | * } |
---|
| 1684 | * ]); |
---|
| 1685 | * </code></pre> |
---|
| 1686 | * @param {String|Array} field A field on your records, or an array containing multiple filter options |
---|
| 1687 | * @param {String/RegExp} value Either a string that the field should begin with, or a RegExp to test |
---|
| 1688 | * against the field. |
---|
| 1689 | * @param {Boolean} anyMatch (optional) <tt>true</tt> to match any part not just the beginning |
---|
| 1690 | * @param {Boolean} caseSensitive (optional) <tt>true</tt> for case sensitive comparison |
---|
| 1691 | * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true. |
---|
| 1692 | */ |
---|
| 1693 | filter : function(property, value, anyMatch, caseSensitive, exactMatch){ |
---|
| 1694 | var fn; |
---|
| 1695 | //we can accept an array of filter objects, or a single filter object - normalize them here |
---|
| 1696 | if (Ext.isObject(property)) { |
---|
| 1697 | property = [property]; |
---|
| 1698 | } |
---|
| 1699 | |
---|
| 1700 | if (Ext.isArray(property)) { |
---|
| 1701 | var filters = []; |
---|
| 1702 | |
---|
| 1703 | //normalize the filters passed into an array of filter functions |
---|
| 1704 | for (var i=0, j = property.length; i < j; i++) { |
---|
| 1705 | var filter = property[i], |
---|
| 1706 | func = filter.fn, |
---|
| 1707 | scope = filter.scope || this; |
---|
| 1708 | |
---|
| 1709 | //if we weren't given a filter function, construct one now |
---|
| 1710 | if (!Ext.isFunction(func)) { |
---|
| 1711 | func = this.createFilterFn(filter.property, filter.value, filter.anyMatch, filter.caseSensitive, filter.exactMatch); |
---|
| 1712 | } |
---|
| 1713 | |
---|
| 1714 | filters.push({fn: func, scope: scope}); |
---|
| 1715 | } |
---|
| 1716 | |
---|
| 1717 | fn = this.createMultipleFilterFn(filters); |
---|
| 1718 | } else { |
---|
| 1719 | //classic single property filter |
---|
| 1720 | fn = this.createFilterFn(property, value, anyMatch, caseSensitive, exactMatch); |
---|
| 1721 | } |
---|
| 1722 | |
---|
| 1723 | return fn ? this.filterBy(fn) : this.clearFilter(); |
---|
| 1724 | }, |
---|
| 1725 | |
---|
| 1726 | /** |
---|
| 1727 | * Filter by a function. The specified function will be called for each |
---|
| 1728 | * Record in this Store. If the function returns <tt>true</tt> the Record is included, |
---|
| 1729 | * otherwise it is filtered out. |
---|
| 1730 | * @param {Function} fn The function to be called. It will be passed the following parameters:<ul> |
---|
| 1731 | * <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record} |
---|
| 1732 | * to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li> |
---|
| 1733 | * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li> |
---|
| 1734 | * </ul> |
---|
| 1735 | * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store. |
---|
| 1736 | */ |
---|
| 1737 | filterBy : function(fn, scope){ |
---|
| 1738 | this.snapshot = this.snapshot || this.data; |
---|
| 1739 | this.data = this.queryBy(fn, scope || this); |
---|
| 1740 | this.fireEvent('datachanged', this); |
---|
| 1741 | }, |
---|
| 1742 | |
---|
| 1743 | /** |
---|
| 1744 | * Revert to a view of the Record cache with no filtering applied. |
---|
| 1745 | * @param {Boolean} suppressEvent If <tt>true</tt> the filter is cleared silently without firing the |
---|
| 1746 | * {@link #datachanged} event. |
---|
| 1747 | */ |
---|
| 1748 | clearFilter : function(suppressEvent){ |
---|
| 1749 | if(this.isFiltered()){ |
---|
| 1750 | this.data = this.snapshot; |
---|
| 1751 | delete this.snapshot; |
---|
| 1752 | if(suppressEvent !== true){ |
---|
| 1753 | this.fireEvent('datachanged', this); |
---|
| 1754 | } |
---|
| 1755 | } |
---|
| 1756 | }, |
---|
| 1757 | |
---|
| 1758 | /** |
---|
| 1759 | * Returns true if this store is currently filtered |
---|
| 1760 | * @return {Boolean} |
---|
| 1761 | */ |
---|
| 1762 | isFiltered : function(){ |
---|
| 1763 | return !!this.snapshot && this.snapshot != this.data; |
---|
| 1764 | }, |
---|
| 1765 | |
---|
| 1766 | /** |
---|
| 1767 | * Query the records by a specified property. |
---|
| 1768 | * @param {String} field A field on your records |
---|
| 1769 | * @param {String/RegExp} value Either a string that the field |
---|
| 1770 | * should begin with, or a RegExp to test against the field. |
---|
| 1771 | * @param {Boolean} anyMatch (optional) True to match any part not just the beginning |
---|
| 1772 | * @param {Boolean} caseSensitive (optional) True for case sensitive comparison |
---|
| 1773 | * @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records |
---|
| 1774 | */ |
---|
| 1775 | query : function(property, value, anyMatch, caseSensitive){ |
---|
| 1776 | var fn = this.createFilterFn(property, value, anyMatch, caseSensitive); |
---|
| 1777 | return fn ? this.queryBy(fn) : this.data.clone(); |
---|
| 1778 | }, |
---|
| 1779 | |
---|
| 1780 | /** |
---|
| 1781 | * Query the cached records in this Store using a filtering function. The specified function |
---|
| 1782 | * will be called with each record in this Store. If the function returns <tt>true</tt> the record is |
---|
| 1783 | * included in the results. |
---|
| 1784 | * @param {Function} fn The function to be called. It will be passed the following parameters:<ul> |
---|
| 1785 | * <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record} |
---|
| 1786 | * to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li> |
---|
| 1787 | * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li> |
---|
| 1788 | * </ul> |
---|
| 1789 | * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store. |
---|
| 1790 | * @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records |
---|
| 1791 | **/ |
---|
| 1792 | queryBy : function(fn, scope){ |
---|
| 1793 | var data = this.snapshot || this.data; |
---|
| 1794 | return data.filterBy(fn, scope||this); |
---|
| 1795 | }, |
---|
| 1796 | |
---|
| 1797 | /** |
---|
| 1798 | * Finds the index of the first matching Record in this store by a specific field value. |
---|
| 1799 | * @param {String} fieldName The name of the Record field to test. |
---|
| 1800 | * @param {String/RegExp} value Either a string that the field value |
---|
| 1801 | * should begin with, or a RegExp to test against the field. |
---|
| 1802 | * @param {Number} startIndex (optional) The index to start searching at |
---|
| 1803 | * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning |
---|
| 1804 | * @param {Boolean} caseSensitive (optional) True for case sensitive comparison |
---|
| 1805 | * @return {Number} The matched index or -1 |
---|
| 1806 | */ |
---|
| 1807 | find : function(property, value, start, anyMatch, caseSensitive){ |
---|
| 1808 | var fn = this.createFilterFn(property, value, anyMatch, caseSensitive); |
---|
| 1809 | return fn ? this.data.findIndexBy(fn, null, start) : -1; |
---|
| 1810 | }, |
---|
| 1811 | |
---|
| 1812 | /** |
---|
| 1813 | * Finds the index of the first matching Record in this store by a specific field value. |
---|
| 1814 | * @param {String} fieldName The name of the Record field to test. |
---|
| 1815 | * @param {Mixed} value The value to match the field against. |
---|
| 1816 | * @param {Number} startIndex (optional) The index to start searching at |
---|
| 1817 | * @return {Number} The matched index or -1 |
---|
| 1818 | */ |
---|
| 1819 | findExact: function(property, value, start){ |
---|
| 1820 | return this.data.findIndexBy(function(rec){ |
---|
| 1821 | return rec.get(property) === value; |
---|
| 1822 | }, this, start); |
---|
| 1823 | }, |
---|
| 1824 | |
---|
| 1825 | /** |
---|
| 1826 | * Find the index of the first matching Record in this Store by a function. |
---|
| 1827 | * If the function returns <tt>true</tt> it is considered a match. |
---|
| 1828 | * @param {Function} fn The function to be called. It will be passed the following parameters:<ul> |
---|
| 1829 | * <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record} |
---|
| 1830 | * to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li> |
---|
| 1831 | * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li> |
---|
| 1832 | * </ul> |
---|
| 1833 | * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store. |
---|
| 1834 | * @param {Number} startIndex (optional) The index to start searching at |
---|
| 1835 | * @return {Number} The matched index or -1 |
---|
| 1836 | */ |
---|
| 1837 | findBy : function(fn, scope, start){ |
---|
| 1838 | return this.data.findIndexBy(fn, scope, start); |
---|
| 1839 | }, |
---|
| 1840 | |
---|
| 1841 | /** |
---|
| 1842 | * Collects unique values for a particular dataIndex from this store. |
---|
| 1843 | * @param {String} dataIndex The property to collect |
---|
| 1844 | * @param {Boolean} allowNull (optional) Pass true to allow null, undefined or empty string values |
---|
| 1845 | * @param {Boolean} bypassFilter (optional) Pass true to collect from all records, even ones which are filtered |
---|
| 1846 | * @return {Array} An array of the unique values |
---|
| 1847 | **/ |
---|
| 1848 | collect : function(dataIndex, allowNull, bypassFilter){ |
---|
| 1849 | var d = (bypassFilter === true && this.snapshot) ? |
---|
| 1850 | this.snapshot.items : this.data.items; |
---|
| 1851 | var v, sv, r = [], l = {}; |
---|
| 1852 | for(var i = 0, len = d.length; i < len; i++){ |
---|
| 1853 | v = d[i].data[dataIndex]; |
---|
| 1854 | sv = String(v); |
---|
| 1855 | if((allowNull || !Ext.isEmpty(v)) && !l[sv]){ |
---|
| 1856 | l[sv] = true; |
---|
| 1857 | r[r.length] = v; |
---|
| 1858 | } |
---|
| 1859 | } |
---|
| 1860 | return r; |
---|
| 1861 | }, |
---|
| 1862 | |
---|
| 1863 | // private |
---|
| 1864 | afterEdit : function(record){ |
---|
| 1865 | if(this.modified.indexOf(record) == -1){ |
---|
| 1866 | this.modified.push(record); |
---|
| 1867 | } |
---|
| 1868 | this.fireEvent('update', this, record, Ext.data.Record.EDIT); |
---|
| 1869 | }, |
---|
| 1870 | |
---|
| 1871 | // private |
---|
| 1872 | afterReject : function(record){ |
---|
| 1873 | this.modified.remove(record); |
---|
| 1874 | this.fireEvent('update', this, record, Ext.data.Record.REJECT); |
---|
| 1875 | }, |
---|
| 1876 | |
---|
| 1877 | // private |
---|
| 1878 | afterCommit : function(record){ |
---|
| 1879 | this.modified.remove(record); |
---|
| 1880 | this.fireEvent('update', this, record, Ext.data.Record.COMMIT); |
---|
| 1881 | }, |
---|
| 1882 | |
---|
| 1883 | /** |
---|
| 1884 | * Commit all Records with {@link #getModifiedRecords outstanding changes}. To handle updates for changes, |
---|
| 1885 | * subscribe to the Store's {@link #update update event}, and perform updating when the third parameter is |
---|
| 1886 | * Ext.data.Record.COMMIT. |
---|
| 1887 | */ |
---|
| 1888 | commitChanges : function(){ |
---|
| 1889 | var modified = this.modified.slice(0), |
---|
| 1890 | length = modified.length, |
---|
| 1891 | i; |
---|
| 1892 | |
---|
| 1893 | for (i = 0; i < length; i++){ |
---|
| 1894 | modified[i].commit(); |
---|
| 1895 | } |
---|
| 1896 | |
---|
| 1897 | this.modified = []; |
---|
| 1898 | this.removed = []; |
---|
| 1899 | }, |
---|
| 1900 | |
---|
| 1901 | /** |
---|
| 1902 | * {@link Ext.data.Record#reject Reject} outstanding changes on all {@link #getModifiedRecords modified records}. |
---|
| 1903 | */ |
---|
| 1904 | rejectChanges : function() { |
---|
| 1905 | var modified = this.modified.slice(0), |
---|
| 1906 | removed = this.removed.slice(0).reverse(), |
---|
| 1907 | mLength = modified.length, |
---|
| 1908 | rLength = removed.length, |
---|
| 1909 | i; |
---|
| 1910 | |
---|
| 1911 | for (i = 0; i < mLength; i++) { |
---|
| 1912 | modified[i].reject(); |
---|
| 1913 | } |
---|
| 1914 | |
---|
| 1915 | for (i = 0; i < rLength; i++) { |
---|
| 1916 | this.insert(removed[i].lastIndex || 0, removed[i]); |
---|
| 1917 | removed[i].reject(); |
---|
| 1918 | } |
---|
| 1919 | |
---|
| 1920 | this.modified = []; |
---|
| 1921 | this.removed = []; |
---|
| 1922 | }, |
---|
| 1923 | |
---|
| 1924 | // private |
---|
| 1925 | onMetaChange : function(meta){ |
---|
| 1926 | this.recordType = this.reader.recordType; |
---|
| 1927 | this.fields = this.recordType.prototype.fields; |
---|
| 1928 | delete this.snapshot; |
---|
| 1929 | if(this.reader.meta.sortInfo){ |
---|
| 1930 | this.sortInfo = this.reader.meta.sortInfo; |
---|
| 1931 | }else if(this.sortInfo && !this.fields.get(this.sortInfo.field)){ |
---|
| 1932 | delete this.sortInfo; |
---|
| 1933 | } |
---|
| 1934 | if(this.writer){ |
---|
| 1935 | this.writer.meta = this.reader.meta; |
---|
| 1936 | } |
---|
| 1937 | this.modified = []; |
---|
| 1938 | this.fireEvent('metachange', this, this.reader.meta); |
---|
| 1939 | }, |
---|
| 1940 | |
---|
| 1941 | // private |
---|
| 1942 | findInsertIndex : function(record){ |
---|
| 1943 | this.suspendEvents(); |
---|
| 1944 | var data = this.data.clone(); |
---|
| 1945 | this.data.add(record); |
---|
| 1946 | this.applySort(); |
---|
| 1947 | var index = this.data.indexOf(record); |
---|
| 1948 | this.data = data; |
---|
| 1949 | this.resumeEvents(); |
---|
| 1950 | return index; |
---|
| 1951 | }, |
---|
| 1952 | |
---|
| 1953 | /** |
---|
| 1954 | * Set the value for a property name in this store's {@link #baseParams}. Usage:</p><pre><code> |
---|
| 1955 | myStore.setBaseParam('foo', {bar:3}); |
---|
| 1956 | </code></pre> |
---|
| 1957 | * @param {String} name Name of the property to assign |
---|
| 1958 | * @param {Mixed} value Value to assign the <tt>name</tt>d property |
---|
| 1959 | **/ |
---|
| 1960 | setBaseParam : function (name, value){ |
---|
| 1961 | this.baseParams = this.baseParams || {}; |
---|
| 1962 | this.baseParams[name] = value; |
---|
| 1963 | } |
---|
| 1964 | }); |
---|
| 1965 | |
---|
| 1966 | Ext.reg('store', Ext.data.Store); |
---|
| 1967 | |
---|
| 1968 | /** |
---|
| 1969 | * @class Ext.data.Store.Error |
---|
| 1970 | * @extends Ext.Error |
---|
| 1971 | * Store Error extension. |
---|
| 1972 | * @param {String} name |
---|
| 1973 | */ |
---|
| 1974 | Ext.data.Store.Error = Ext.extend(Ext.Error, { |
---|
| 1975 | name: 'Ext.data.Store' |
---|
| 1976 | }); |
---|
| 1977 | Ext.apply(Ext.data.Store.Error.prototype, { |
---|
| 1978 | lang: { |
---|
| 1979 | 'writer-undefined' : 'Attempted to execute a write-action without a DataWriter installed.' |
---|
| 1980 | } |
---|
| 1981 | }); |
---|