This project is supported by Generative Objects
This is a plugin for the Vuex-ORM library.
It adds two flags $isDirty
and $isNew
(as boolean attributes) on any instance of the entities created through this library and updates their values automatically.
Simply run npm install @vuex-orm/plugin-change-flags --save
in your favorite terminal.
Then, you need to install the plugin as any VuexORM plugin. In your store initialization code, simply add:
import VuexORMisDirtyPlugin from '@vuex-orm/plugin-change-flags';
and then
VuexORM.use(VuexORMisDirtyPlugin);
Once the plugin installed, every time you create a new instance of an entity, the $isDirty
and $isNew
will be automatically added.
The default value for those flags is false
. They are automatically set to true
in the following cases:
This flag is automatically set to true
when:
- creating a new instance using the
createNew
method; - updating the entity instance in the store, using the
update
action (insert
andcreate
mutation will not set this flag totrue
).
Preventing flag setting
If you want to prevent the flag to be set to true
when updating, you can pass preventDirtyFlag: true
to the update
and insertOrUpdate
calls:
User.update({
data: myUserToUpdate,
preventDirtyFlag: true
});
This flag is automatically set to true
when calling the createNew
method.
When calling the Model constructor new Model()
, the default values for both flags is false
.
Other values can be set by passing the initial record value to the constructor:
let user = new User({ id: 1, $isNew: true });
console.log(user.$isNew); // true (set through parameter)
console.log(user.$isDirty); // false (default value)
This plugin also adds the Model.createNew
method and the allDirty
& allNew
getters.
The plugin provides a new Model.createNew(insertInStore = true)
method which returns a new instance of the entity, with both flags set to true
by default and all other fields set to their default value.
This method copies VuexORM new method behavior. As such, it will automatically insert the newly created entity in the store.
If you don't want the created instance to be inserted in store directly, you can pass false
as parameter.
createNew
method without parameter, this will try to insert the given record directly in the store. It might fail if default value for the Primary Key is null
. Try using an increment type attribute or a mutator to make sure your PK as a value.
This new getter returns all entities marked as dirty currently in the store.
It can be used globally:
// Returns an array of mixed types with all entities
// currently marked as dirty in the store
let results = store.getters['entities/allDirty']();
or specifically to a type:
// Returns an array User entities currently marked as dirty in the store
let results = store.getters['entities/users/allDirty']();
This new getter returns all entities marked as new currently in the store.
It can be used globally:
// Returns an array of mixed types with all entities
// currently marked as new in the store
let results = store.getters['entities/allNew']();
or specifically to a type:
// Returns an array User entities currently marked as new in the store
let results = store.getters['entities/users/allNew']();
This action will run through all the entities marked as dirty in your store and set the corresponding flag to false
:
store.dispatch['entities/resetAllDirtyFlags']({}, { root: true });
By default, the flags are named $isDirty
and $isNew
.
You can override those default by setting the corresponding options at plugin initialization.
Option name | Description | Default value |
---|---|---|
isNewFlagName | Sets the name of the isNew flag | $isNew |
isDirtyFlagName | Sets the name of the isDirty flag | $isDirty |
exposeFlagsExternally | Adds the isNew and isDirty flags to the JSON stringified output | true |
In order to use those options, you can pass them as the second parameter of the install
call:
VuexORM.use(VuexORMisDirtyPlugin, {
isNewFlagName: 'IsNew',
isDirtyFlagName: 'IsDirty',
exposeFlagsExternally: true
});
This project is licensed under the MIT License - see the LICENSE.md file for details