A model is a collection of domain-specific data with a namespace.
// Model example of `User`constuserModel=app.model('User',{ username:'foo', password:'123'})
The above codes create an User model which has an initial state of { username: 'foo', password: '123' }
3. Create Mutations
The mutations define a set of operations of how the model will be updated. Use model.mutations() function to create mutations
model.mutations() accepts an object as the first parameter which key is the name of the operation, value is a function that describes the mutation input and output. For example, to mutate user.username and user.password we can have the following mutations
The mutation function (username: string) => user => ({...user, username}) is a curried function that accepts an input username and together with the current user to produce a new user state.
The mutation functions should follow the pattern which
The "outer" function args are the inputs
The "inner" function arg is the current model state
It should return a new model state
4. Use in a Component
Assume we are implementing a user sign-up form:
In the above example, the component usesuseModel hook to connect the component with the user model.