Store specification
This document describes in detail how you can interact with the Store API to load/save the data displayed in the grid,
which events it triggers when and what server responses it expects when using AjaxStore.
All changes are kept local until Store#commit() is called. Store may be
configured with Store#autoCommit to always commit changes
automatically.
Exceptions from an AjaxStore
If errors are signalled during server requests, an exception event will be triggered, and the Promise returned from
the commit or load
call will be rejected. The parameter passed to the reject function and the exception event object is an object which
describes the error. See AjaxStore documentation for details.
Create
Records in local store
Adding to the store using either Store#add()
or Store#insert() triggers the following behaviour:
- If raw data is added, convert it into a record using configured
Model (
Store#modelClass). Also assigns the new record a generated id if none was specified. - Add to store
- Add raw data to
Store#data - Add record to
Store#records - Add record to
Store#added(only contains records added since last commit)
- Add raw data to
- Assign the store to the record (
Model#stores) - Init record relations, if any
- Update
Store#idMap, which is used for fast lookups by id or index - Trigger events
- Trigger
addevent - Trigger
changeevent (triggered on all manipulations of store data)
- Trigger
Committing added records in a local store simply empties the Store#added array, to signal that records are now
considered as adequate members of the store. Committing triggers the following behaviour:
- Trigger
beforeCommitevent, which is cancelable - Clear stored changes (accept them)
- Trigger
commitevent
Records in AjaxStore
Adding a record to an AjaxStore follows the same behaviour as outlined in 1 - 6 above, but replaces the behaviour upon commit with the following:
- Trigger events:
- Trigger
beforeCommitevent, which is cancelable - Trigger
beforeRequestevent
- Trigger
- Post the added records to the configured url as Ajax with JSON encoding (
AjaxStore#createUrl). Post has the following format:data=[{ field1: xx, field2: xx, ... }, ...] - Server is expected to return a JSON encoded response containing:
- A success property
{ "success" : true/false } - Upon success, also an array of JSON encoded objects corresponding to the added records. These objects should at
least contain id:s assigned by the server to replace any locally generated
ids:
{ "success": true, "data": [ { "id": 1 }, ... ] }
- A success property
- If server call is unsuccessful trigger
exceptionevent and abort commit - If server call was successful, continue
- Update
Store#idMap, which is used for fast lookups by id or index - Trigger events:
- Trigger
commitAddedevent - Trigger
refreshevent - Trigger
afterRequestevent - Trigger
commitevent
- Trigger
Read
Setting already available data
Assigning an array of json data to Store#data loads it into the store and triggers the following behaviour:
- Clear any existing data by calling
Store#clear() - If raw data is loaded, convert it into records using configured model (
Store#modelClass) - Apply sorting, grouping & filtering
- Update
Store#idMap, which is used for fast lookups by id or index - Trigger events:
- Trigger
refreshevent, with{ action : 'dataset' } - Trigger
changeevent, with{ action : 'dataset' }(triggered on all manipulations of store data)
- Trigger
Loading data using AjaxStore
Loading data using Ajax by calling AjaxStore#load() (or configuring Store#autoLoad) triggers the following
behaviour:
- Trigger events:
- Trigger
beforeLoadevent, which is cancelable - Trigger
loadStartevent - Trigger
beforeRequestevent
- Trigger
- Set
Store#isLoadingto true, to prevent actions that should not occur while loading - Make Ajax request to configured url (
AjaxStore#readUrl) - If server request fails, trigger
exceptionevent and abort - Server is expected to return an array of JSON encoded data upon success
- Assign returned data to
Store#data, triggering steps 1 - 5 in the previous section - Clear any uncommited changes
- Trigger events:
- Trigger
loadevent - Trigger
afterRequestevent
- Trigger
Update
Records in local store
Modifying records in a local store (by either using a models properties or Model#set()) triggers the following
behaviour:
- Modify value
- Store old value in
record.meta.modified - Flag model as changed (
record.meta.changed) - If changing a foreign id (when using model relations), also update the relation
- Add to
Store#modified[]for all containing stores, if not already present inStore#added - If changing id:
- Update
Store#idMap - Trigger
idChangeevent on store
- Update
- Trigger events on store:
- Trigger
updateevent - Trigger
changeevent (triggered on all manipulations of store data)
- Trigger
Committing modified records in a local store simply empties the Store#modified[] array and
clear record.meta.modified and record.meta.changed for all modified records. Committing triggers the following
behaviour:
- Trigger
beforeCommitevent, which is cancelable - Clear stored changes (accept them)
- Trigger
commitevent
Records in AjaxStore
Modifying records in an AjaxStore triggers the same behaviour as outlined in steps 1 - 7 above. Committing triggers the following behaviour:
- Trigger events:
- Trigger
beforeCommitevent, which is cancelable - Trigger
beforeRequestevent
- Trigger
- Post modifications from the modified records to the configured url as Ajax with JSON encoding (
AjaxStore#updateUrl) . Post has the following format:data=[{ id: 1, field1: xx, field2: xx, ... }, ...] - If server call is unsuccessful, trigger
exceptionevent and abort - If server call was successful, continue
- Clear stored changes (accept them)
- Update
Store#idMap, which is used for fast lookups by id or index - Trigger events:
- trigger
commitModifiedevent - trigger
refreshevent - Trigger
afterRequestevent - Trigger
commitevent
- trigger
Destroy
Records in local store
When removing records from a local store by either
calling Store#remove()
or Model#remove() the following behaviour is triggered:
- Remove record from store
- If record is added but not committed, also remove from
Store#added[] - Otherwise add record to
Store#removed[] - Update relations, if any
- Flag record as removed, by setting
record.meta.removed - Update
Store#idMap, which is used for fast lookups by id or index - Trigger events:
- trigger
removeevent - trigger
changeevent (triggered on all manipulations of store data)
- trigger
Committing removed records in a local store simply empties Store#removed[]. It triggers the following:
- Trigger
beforeCommitevent, which is cancelable - Clear stored changes (accept them)
- Trigger
commitevent
Records in AjaxStore
Removing records from an AjaxStore triggers the same behaviour as outlined in steps 1 - 5 above. Committing triggers the following:
- Trigger events:
- Trigger
beforeCommitevent, which is cancelable - Trigger
beforeRequestevent
- Trigger
- Post ids of removed records using Ajax to
AjaxStore#destroyUrl. Post has formatid=x,y,... - If server call is unsuccessful, trigger
exceptionevent and abort - If server call was successful, continue
- Clear stored changes (
Store#removed[]) - Trigger events:
- Trigger
commitRemovedevent - Trigger
refreshevent - Trigger
afterRequestevent - Trigger
commitevent
- Trigger
Sending HTTP Headers
You can configure the AjaxStore to append HTTP headers to its server requests by using the headers config.
// Configuring headers for each request
const store = new AjaxStore({
readUrl : 'backend/read.php',
headers : {
'Content-Type' : 'text/xml',
'Accept-Charset' : 'utf-8'
}
});