Vuex ORM plugin adding IsDirty / IsNew flags to model entities
npm install @vuex-orm/plugin-change-flags

 
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:
``javascript`
import VuexORMisDirtyPlugin from '@vuex-orm/plugin-change-flags';
and then
`javascript`
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;update
- updating the entity instance in the store, using the action (insert and create mutation will not set this flag to true).
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:
`js`
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:
`javascript
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.
:warning: When calling the 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:
`javascript`
// 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:
`javascript`
// 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:
`javascript`
// 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:
`javascript`
// 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:
`js`
store.dispatch'entities/resetAllDirtyFlags';
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 |$isDirty
| isDirtyFlagName | Sets the name of the _isDirty_ flag | |true
| exposeFlagsExternally | Adds the _isNew_ and _isDirty_ flags to the JSON stringified output | |
In order to use those options, you can pass them as the second parameter of the install call:
`javascript``
VuexORM.use(VuexORMisDirtyPlugin, {
isNewFlagName: 'IsNew',
isDirtyFlagName: 'IsDirty',
exposeFlagsExternally: true
});

This project is licensed under the MIT License - see the LICENSE.md file for details