Skip to content

Commit a54f716

Browse files
author
tvillaren
committed
Adding resetAllDirtyFlags action + a way to prevent Dirty Flag to be set on update
1 parent 71d0c9f commit a54f716

File tree

3 files changed

+179
-2
lines changed

3 files changed

+179
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ This flag is _automatically_ set to `true` when:
4040
- creating a new instance using the `createNew` method;
4141
- updating the entity instance in the store, using the `update` action (`insert` and `create` mutation will not set this flag to `true`).
4242

43+
**Preventing flag setting**
44+
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:
45+
46+
```js
47+
User.update({
48+
data: myUserToUpdate,
49+
preventDirtyFlag: true
50+
});
51+
```
52+
4353
### \$isNew
4454

4555
This flag is _automatically_ set to `true` when calling the `createNew` method.
@@ -108,6 +118,14 @@ or specifically to a type:
108118
let results = store.getters['entities/users/allNew']();
109119
```
110120

121+
### `resetAllDirtyFlags` action
122+
123+
This action will run through all the entities marked as dirty in your store and set the corresponding flag to `false`:
124+
125+
```js
126+
store.dispatch['entities/resetAllDirtyFlags']({}, { root: true });
127+
```
128+
111129
## Plugin Options
112130

113131
By default, the flags are named `$isDirty` and `$isNew`.

src/index.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const defaultOptions = {
55

66
export default {
77
install(components, installOptions) {
8-
const pluginOptions = { ...defaultOptions,
8+
const pluginOptions = {
9+
...defaultOptions,
910
...installOptions
1011
};
1112

@@ -47,10 +48,52 @@ export default {
4748
* This is the only automatic way which sets this flag
4849
* to true once it's in the store.
4950
*/
51+
let _ignoreIsDirtyFlag = false;
52+
5053
Query.on('beforeUpdate', function (model) {
51-
model[pluginOptions.isDirtyFlagName] = true;
54+
if (!_ignoreIsDirtyFlag)
55+
model[pluginOptions.isDirtyFlagName] = true;
5256
});
5357

58+
/**
59+
* Providing the resetFlags actions
60+
*/
61+
RootActions.resetAllDirtyFlags = function ({
62+
rootGetters
63+
}) {
64+
debugger
65+
const allDirty = rootGetters['entities/allDirty']();
66+
_ignoreIsDirtyFlag = true;
67+
allDirty.forEach(e =>
68+
e.$update({
69+
[pluginOptions.isDirtyFlagName]: false
70+
})
71+
);
72+
_ignoreIsDirtyFlag = false;
73+
};
74+
75+
// Overwriting to add preventDirtyFlag option
76+
const _insertOrUpdate = RootMutations.insertOrUpdate;
77+
RootMutations.insertOrUpdate = function (state, payload) {
78+
if (payload.preventDirtyFlag === true) {
79+
_ignoreIsDirtyFlag = true;
80+
_insertOrUpdate.call(this, state, payload);
81+
_ignoreIsDirtyFlag = false;
82+
} else
83+
_insertOrUpdate.call(this, state, payload);
84+
};
85+
86+
// Overwriting to add preventDirtyFlag option
87+
const _update = RootMutations.update;
88+
RootMutations.update = function (state, payload) {
89+
if (payload.preventDirtyFlag === true) {
90+
_ignoreIsDirtyFlag = true;
91+
_update.call(this, state, payload);
92+
_ignoreIsDirtyFlag = false;
93+
} else
94+
_update.call(this, state, payload);
95+
};
96+
5497
/**
5598
* Providing the allDirty getter
5699
*/

test/unit/test-all.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import Role from '../dev/common/models/Role';
66
import {
77
expect
88
} from 'chai';
9+
import {
10+
Query
11+
} from '@vuex-orm/core';
12+
import {
13+
debug
14+
} from 'util';
915

1016
describe('Vuex ORM $isDirty/$isNew plugin default installation', function () {
1117
it('should have both flag set to false when creating new', function () {
@@ -211,4 +217,114 @@ describe('Vuex ORM $isDirty/$isNew plugin default installation', function () {
211217

212218
expect(result[0] instanceof User).to.equal(true);
213219
});
220+
221+
it('should provide a way to reset all dirty flags to false', async function () {
222+
const store = createStore([{
223+
model: User
224+
}, {
225+
model: Role
226+
}]);
227+
228+
let user = new User({
229+
id: 1,
230+
roleId: 1
231+
});
232+
let user2 = new User({
233+
id: 2,
234+
roleId: 1
235+
});
236+
let role = new Role({
237+
id: 3
238+
});
239+
240+
User.insert({
241+
data: [user, user2]
242+
});
243+
Role.insert({
244+
data: role
245+
});
246+
247+
user.name = 'AA';
248+
role.name = 'AA';
249+
250+
User.update({
251+
data: user
252+
});
253+
Role.update({
254+
data: role
255+
});
256+
257+
258+
let result = store.getters['entities/allDirty']();
259+
expect(result.length).to.equal(2);
260+
await store.dispatch('entities/resetAllDirtyFlags', {}, {
261+
root: true
262+
});
263+
264+
let result2 = store.getters['entities/allDirty']();
265+
expect(result2.length).to.equal(0);
266+
});
267+
268+
269+
it('should provide a way to ignore dirty flag when updating data', async function () {
270+
const store = createStore([{
271+
model: User
272+
}, {
273+
model: Role
274+
}]);
275+
276+
let user = new User({
277+
id: 1,
278+
roleId: 1
279+
});
280+
281+
User.insert({
282+
data: [user]
283+
});
284+
285+
user.name = 'AA';
286+
287+
User.update({
288+
data: user,
289+
preventDirtyFlag: true
290+
});
291+
292+
let result = store.getters['entities/allDirty']();
293+
expect(result.length).to.equal(0);
294+
});
295+
296+
it('should provide a way to ignore dirty flag when insertingOrUpdating data', async function () {
297+
const store = createStore([{
298+
model: User
299+
}, {
300+
model: Role
301+
}]);
302+
303+
let user = new User({
304+
id: 1,
305+
roleId: 1
306+
});
307+
let user2 = new User({
308+
id: 2,
309+
roleId: 1
310+
});
311+
312+
User.insert({
313+
data: [user, user2]
314+
});
315+
316+
user.name = 'AA';
317+
318+
User.insertOrUpdate({
319+
data: [user, user2],
320+
preventDirtyFlag: true
321+
});
322+
323+
let result = store.getters['entities/allDirty']();
324+
expect(result.length).to.equal(0);
325+
326+
let users = store.getters['entities/users/all']();
327+
expect(users.length).to.equal(2);
328+
329+
});
214330
});

0 commit comments

Comments
 (0)