# Rollback mixin

The rollback mixin allows to reset the model state to the last saved (origin) state.

# Rollback mask

The mixin uses an object called "rollback mask" that determines fields must be rolled back, and which field don't.

TIP

If a rollback mask is not specified the whole model data will be reset.

Simple rollback mask example:

The rollback mask from the previous example allows the model fields age and name to be reset prevents the id field from being reset.

TIP

If a field is true or undefined in a user-defined mask, the field will be ignored.

Rollback masks support special keys to define arrays and items with specified array positions (indexes). To define a mask of array, use $array: true marker:

The phones field of the mask is marked as array with $array: true, overwise the field will be processed like an object. The number field of each item of the phones array will be rolled back, but the name field won't.

If it is necessary to roll back only a few elements of an array, the $index keyword should be used:

# Model definition

The mixin provides a factory function that returns a mixin function. The following example presents a typical use of the rollback mixin:

# Flags

The mixin does not provide any flags, but toggles the dirty flag of the BaseModel class.

# Usage

The rollback() method is provided by the mixin. Optionally, it accepts a runtime rollback mask which is used to roll back only specified data fields.

# Combination with the Save mixin

The rollback mixin automatically updates an origin data object after the model saving, it uses afterCreate anf afterSave hooks.