Creating a model
Our model will have the following methods: read
, getRecord
, update
, create
, delete
, isValidRecord
and getValidationDependency
.
Here, we’re going to use MySQL and Squel.
First, let’s define read
.
userGrid/model.js
:
The read
method returns an object with two properties: records
and count
(the number of returned records).
Pay attention to this part:
The value of records
is an array consisting of arrays that store a record id as their first element and
a record as the second one.
For example:
Let’s define methods for validation:
Next, we’ll define getRecord
:
The getRecord
method returns a single record.
Here’s the code for create
:
If data is valid, create
returns the id of the inserted record. Otherwise, it returns validation errors.
Let’s define the update
method:
This method returns validation errors and updated records. The return value format is the same as for the read
method.
Validation is used to highlight the form fields which were filled wrongly.
Updated records are used for updating of the grid.
Finally, let’s define delete
:
The return value of delete
can be different. It depends on the definition of this method in the client model.