Skip to content

Commit 1062398

Browse files
author
tvillaren
committed
Added an option to expose the flags through the Model.prototype.$fields() method, used by $toJSON internally, thus exposing the flags to any JSON.stringify output.
See vuex-orm/vuex-orm#319 for rationale.
1 parent ee633a7 commit 1062398

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,19 @@ store.dispatch['entities/resetAllDirtyFlags']({}, { root: true });
131131
By default, the flags are named `$isDirty` and `$isNew`.
132132
You can override those default by setting the corresponding options at plugin initialization.
133133

134-
| Option name | Description | Default value |
135-
| --------------- | ----------------------------------- | :-----------: |
136-
| isNewFlagName | Sets the name of the _isNew_ flag | `$isNew` |
137-
| isDirtyFlagName | Sets the name of the _isDirty_ flag | `$isDirty` |
134+
| Option name | Description | Default value |
135+
| --------------------- | ------------------------------------------------------------------- | :-----------: |
136+
| isNewFlagName | Sets the name of the _isNew_ flag | `$isNew` |
137+
| isDirtyFlagName | Sets the name of the _isDirty_ flag | `$isDirty` |
138+
| exposeFlagsExternally | Adds the _isNew_ and _isDirty_ flags to the JSON stringified output | `true` |
138139

139140
In order to use those options, you can pass them as the second parameter of the `install` call:
140141

141142
```javascript
142143
VuexORM.use(VuexORMisDirtyPlugin, {
143144
isNewFlagName: 'IsNew',
144-
isDirtyFlagName: 'IsDirty'
145+
isDirtyFlagName: 'IsDirty',
146+
exposeFlagsExternally: true
145147
});
146148
```
147149

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vuex-orm/plugin-change-flags",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "Vuex ORM plugin adding IsDirty / IsNew flags to model entities",
55
"author": "Thomas Villaren",
66
"main": "dist/index.js",

src/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const defaultOptions = {
22
isDirtyFlagName: '$isDirty',
3-
isNewFlagName: '$isNew'
3+
isNewFlagName: '$isNew',
4+
exposeFlagsExternally: true
45
};
56

67
export default {
@@ -20,6 +21,23 @@ export default {
2021
Actions
2122
} = components;
2223

24+
/**
25+
* Flags are exposed by default when stringiying into JSON.
26+
* This can be deactivated by setting the flag to false
27+
*/
28+
if (pluginOptions.exposeFlagsExternally) {
29+
const localFieldModel = {
30+
[pluginOptions.isDirtyFlagName]: Model.attr(false),
31+
[pluginOptions.isNewFlagName]: Model.attr(false)
32+
};
33+
34+
const _saveGetFiedsMethod = Model.prototype.$fields;
35+
Model.prototype.$fields = function () {
36+
const existing = _saveGetFiedsMethod.call(this);
37+
return Object.assign({}, existing, localFieldModel);
38+
}
39+
}
40+
2341
/**
2442
* Overwriting the $fill method used when calling
2543
* new Model() to inject automatically the 2 flags
@@ -61,7 +79,6 @@ export default {
6179
RootActions.resetAllDirtyFlags = function ({
6280
rootGetters
6381
}) {
64-
debugger
6582
const allDirty = rootGetters['entities/allDirty']();
6683
_ignoreIsDirtyFlag = true;
6784
allDirty.forEach(e =>

test/unit/test-all.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,24 @@ describe('Vuex ORM $isDirty/$isNew plugin default installation', function () {
322322
expect(users.length).to.equal(2);
323323

324324
});
325+
326+
it('should expose flags by default', async function () {
327+
const store = createStore([{
328+
model: User
329+
}]);
330+
331+
let user = new User({
332+
id: 1,
333+
roleId: 1,
334+
name: "Test",
335+
email: "Test"
336+
});
337+
338+
User.insert({
339+
data: [user]
340+
});
341+
342+
let u = store.getters['entities/users/find'](1);
343+
expect(JSON.stringify(u)).to.equal('{"id":1,"name":"Test","email":"Test","phone":"","roleId":1,"role":null,"$isDirty":false,"$isNew":false}');
344+
});
325345
});

0 commit comments

Comments
 (0)