DirtyCheckPlugin is useful for cases when you want an indication whether the state is dirty (data in the store has been modified). For example, you may want to display a save button only if the user changes something.
To activate the plugin you need to create a new instance of
DirtyCheckPlugin, providing it with the
From the moment you call
DirtyCheckPlugin takes the current store snapshot and saves it as the head (the value that we compare against). With every change to the store the plugin will compare it to the head value and notify you whether the state is dirty.
reset() you are telling the plugin to update the store with the
head value. The plugin also provides a special method called isPathDirty() that checks whether a given
dirty. For example:
If you have set the head, you can get the current head value with
comparator compares the object by using the native
JSON.stringify() method, but you can pass a custom
comparator, for example:
The dirty check plugin can watch specific properties in your store's state and not just the entire store, this can be achieved by passing the properties keys to
DirtyCheckPlugin, for example:
After the first time you call
setHead(), each subsequent call to this method will re-set the current store value as the
head and update the dirtiness to
In addition to the general dirty check functionality, Akita also provides a powerful API to help keep track of one or many entities, instead of the entire store.
A good example is when you have a table or a list of entities that the users can modify, and you want to give them a way to revert it per entity. Here is how you can do it:
With this setup you can track the dirtiness per entity and revert it.
EntityDirtyCheckPlugin doesn't track the entities count. It only tracks changes on the entities themselves. If you want to track the addition or removal of entities, you can do so by using the
DirtyCheckPlugin and watch the
Sometimes it's useful to partially reset the entity value when clicking on revert. The
revert() method can accept a custom
update function which receives as parameters the current
head and the current entity
value, and returns the modified entity. For example:
In the above example we are reverting the whole entity except for the
title (note that this will still mark the entity as dirty).
Sometimes it's also useful to check whether at least one of the entities is dirty. For this you can use the
A single id or an array of entity ids (default: all).