Grid Xhr Model is a class that interacts with the server API holding the data.
Grid Xhr Model implements Grid Model Interface
Parameters:
Type | Name | Description |
---|---|---|
String | settings.api | Required. API address to interact with. |
Validator | settings.validator | Optional. Validator. By default there is created a new Validator instance. |
Function | settings.xhr | Optional. XHR interface. By default is used built-in xhr function, but you can override it here. |
Create a new grid record.
It sends POST
request with header {'Content-type': 'application/json'}
and body = JSON.stringify(record)
to the URI = settings.api parameter taken in the constructor).
Validation is supposed to be performed at the server
(but you can also do explicit local validation calling gridXhrModel.isValidRecord(record) before).
If the server response is successful(Status 200) and it has field error
(which is expected to be validation errors),
the ValidationErrors instance will be thrown.
In case of unexpected error(e.g. server response status !== 200) an Error instance will be thrown.
If the server response is successful(Status 200) and it doesn’t have the field error
,
so serverResponse.data
will be resolved.
Parameters:
Type | Name | Description |
---|---|---|
Object | record | Required. The record data to be created. Expected structure: `{field1Name: ‘value1’, …, fieldNName: ‘valueN’} |
Example:
Retrieves grid records from the server.
It sends GET
request to the URI = settings.api
parameter taken in the constructor).
In successful case(response status == 200) returns server response, otherwise throws an error.
Parameters:
Type | Name | Description |
---|---|---|
String [ ] | settings.fields | Optional. Array o field names(string values) to retrieve |
Number | settings.limit | Optional. Maximum amount of result items |
Number | settings.offset | Optional. Offset from the beginning of the records array |
Object | settings.filters | Optional. Object with filtering criteria. The structure of the object is up to your servers’ API. |
String [ ] [ ] | settings.sort | Optional. Sorting order. Array of sorting parameters where each parameter is an array of 2 string values: sorted column name and sorting mode(asc/desc). |
String [ ] | settings.extra | Optional. Record IDs, that are needed to be fetched additionally(despite filtering criteria)(explicitly returned in a result). Usually they are edited records that are displayed in spite of filters and the current page of the grid. |
Returns: Promise which resolves with the server response which is expected to be an Object with the next structure:
Type | Name | Description |
---|---|---|
[ ] [String, Object] | result.records | Requested grid records with applied limit, offset, filters and sorting. Array of arrays. Expected to have the next structure: |
[ | ||
[“recordID1”, {“field1Name”: “value11”, …, “fieldNName”:”value1N”}], | ||
[“recordID2”, {“field1Name”: “value21”, …, “fieldNName”:”value2N”}], | ||
… | ||
] | ||
Object [ ] | result.extraRecords | Extra records. Array of grid records which correspond to ids requested in settings.extra |
Number | result.count | Number of records in result.records |
Object | result.totals | Data for totals(bottom row with table data summary like ‘Total costs’ of costs table). Expected to be an Object with the next structure: |
`{field1Name: ‘value1’, …, fieldNName: ‘valueN’} |
Example:
Fetch record data by id
.
It sends GET
request to the URI = ‘/:api/:id’ (where :api
is settings.api parameter taken in the constructor, :id
= passed argument)
with header {'Content-type': 'application/json'}
and body = JSON.stringify(fields)
In successful case(response status == 200) returns server response, otherwise throws an error.
Parameters:
Type | Name | Description |
---|---|---|
Any | id | Required. Record id to get data of. |
String[ ] | fields | Optional. Grid fields to get values of. By default there is returned values of all the grid fields. |
Returns: Promise which resolves with Object with record data.
It has the following structure: {field1dName: 'value1', ..., fieldNName: 'valueN'}
.
Example:
Applies records changes.
It sends PUT
request with header {'Content-type': 'application/json'}
and body = JSON.stringify(changes)
to the URI = settings.api parameter taken in the constructor.
Validation is supposed to be performed at the server
(but you can also do explicit local validation calling gridXhrModel.isValidRecord(record) before).
If the server response is successful(Status 200) and it has field errors
(which is expected to be validation errors),
the ValidationErrors instance will be thrown.
In case of unexpected error(e.g. server response status !== 200) an Error instance will be thrown.
If the server response is successful(Status 200) and it doesn’t have the field error
,
so serverResponse.data
will be resolved.
Parameters:
Type | Name | Description |
---|---|---|
[ ] [Any, Object] | changes | Required. Array of changed records data, where each record data is an array of 2 fields: recordId(any serializable value) and record data(Object). |
Example:
Return fields(Array of string values) that need to be sent additionally to validate fields specified in passed parameters. This method is required for creating group validators(read details here).
Parameters:
Type | Name | Description |
---|---|---|
String[] | fields | Required. Array of fields to get their validation-dependant fields |
Validate specified record by means of the validator passed in the constructor, or the default one. In case of an unexpected error it will be thrown(e.g. if there are async validation rules and the Internet connection doesn’t work, or server responds with status !== 200) will be thrown.
Parameters:
Type | Name | Description |
---|---|---|
Object | record | Required. Record to validate. Expected structure: {field1Name: 'value1', ..., fieldNName: 'valueN'} |
Returns: Promise which resolves with ValidationErrors