Skip to content

Commit 71d0c9f

Browse files
author
Thomas V
authored
Merge pull request #1 from vuex-orm/fix-createNew-signature
Fixing the method signature for Model.createNew
2 parents 05688e3 + 5860039 commit 71d0c9f

File tree

5 files changed

+155
-84
lines changed

5 files changed

+155
-84
lines changed

README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,12 @@ It adds two flags `$isDirty` and `$isNew` (as _boolean_ attributes) on any insta
1313

1414
## Installation
1515

16-
Simply reference the github project in your `package.json`
17-
18-
```javascript
19-
dependencies: {
20-
...
21-
"vuexorm-isdirty-plugin": "git://github.com/vuex-orm/vuexorm-isdirty-plugin"
22-
...
23-
}
24-
```
25-
26-
and run `npm install`.
16+
Simply run `npm install @vuex-orm/plugin-change-flags --save` in your favorite terminal.
2717

2818
Then, you need to install the plugin as any VuexORM plugin. In your store initialization code, simply add:
2919

3020
```javascript
31-
import VuexORMisDirtyPlugin from 'vuexorm-isdirty-plugin';
21+
import VuexORMisDirtyPlugin from '@vuex-orm/plugin-change-flags';
3222
```
3323

3424
and then

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vuex-orm/plugin-change-flags",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Vuex ORM plugin adding IsDirty / IsNew flags to model entities",
55
"author": "Thomas Villaren",
66
"main": "dist/index.js",
@@ -75,4 +75,4 @@
7575
"url": "https://github.com/vuex-orm/plugin-change-flags/issues"
7676
},
7777
"homepage": "https://github.com/vuex-orm/plugin-change-flags#readme"
78-
}
78+
}

src/index.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const defaultOptions = {
55

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

1012
const {
1113
Model,
@@ -28,7 +30,7 @@ export default {
2830
const _saveFillMethod = Model.prototype.$fill;
2931

3032
// Overwrite
31-
Model.prototype.$fill = function(record) {
33+
Model.prototype.$fill = function (record) {
3234
_saveFillMethod.call(this, record); // Calling initial
3335

3436
// $isDirty
@@ -45,15 +47,15 @@ export default {
4547
* This is the only automatic way which sets this flag
4648
* to true once it's in the store.
4749
*/
48-
Query.on('beforeUpdate', function(model) {
50+
Query.on('beforeUpdate', function (model) {
4951
model[pluginOptions.isDirtyFlagName] = true;
5052
});
5153

5254
/**
5355
* Providing the allDirty getter
5456
*/
55-
RootGetters.allDirty = function(state) {
56-
return function(entity) {
57+
RootGetters.allDirty = function (state) {
58+
return function (entity) {
5759
if (entity) {
5860
return new Query(state, entity)
5961
.where(elt => elt[pluginOptions.isDirtyFlagName])
@@ -72,8 +74,8 @@ export default {
7274
};
7375
};
7476

75-
Getters.allDirty = function(state, _getters, _rootState, rootGetters) {
76-
return function() {
77+
Getters.allDirty = function (state, _getters, _rootState, rootGetters) {
78+
return function () {
7779
return rootGetters[`${state.$connection}/allDirty`](
7880
state.$name
7981
);
@@ -83,8 +85,8 @@ export default {
8385
/**
8486
* Providing the allNew getter
8587
*/
86-
RootGetters.allNew = function(state) {
87-
return function(entity) {
88+
RootGetters.allNew = function (state) {
89+
return function (entity) {
8890
if (entity) {
8991
return new Query(state, entity)
9092
.where(elt => elt[pluginOptions.isNewFlagName])
@@ -103,8 +105,8 @@ export default {
103105
};
104106
};
105107

106-
Getters.allNew = function(state, _getters, _rootState, rootGetters) {
107-
return function() {
108+
Getters.allNew = function (state, _getters, _rootState, rootGetters) {
109+
return function () {
108110
return rootGetters[`${state.$connection}/allNew`](state.$name);
109111
};
110112
};
@@ -114,17 +116,17 @@ export default {
114116
* When called on the Model instead of new, it will
115117
* set the 2 flags to true
116118
*/
117-
Model.createNew = function(insertInStore = true) {
119+
Model.createNew = function (insertInStore = true) {
118120
if (insertInStore) return this.dispatch('createNew');
119121
else {
120122
let record = new this();
121123
record[pluginOptions.isNewFlagName] = true;
122124
record[pluginOptions.isDirtyFlagName] = true;
123-
return record;
125+
return Promise.resolve(record);
124126
}
125127
};
126128

127-
RootMutations.createNew = function(state, payload) {
129+
RootMutations.createNew = function (state, payload) {
128130
const entity = payload.entity;
129131
const result = payload.result;
130132

@@ -133,19 +135,23 @@ export default {
133135
query.setResult(result).createNew();
134136
};
135137

136-
Actions.createNew = function(context) {
138+
Actions.createNew = function (context) {
137139
const state = context.state;
138140
const entity = state.$name;
139141

140142
return context.dispatch(
141-
`${state.$connection}/createNew`,
142-
{ entity },
143-
{ root: true }
143+
`${state.$connection}/createNew`, {
144+
entity
145+
}, {
146+
root: true
147+
}
144148
);
145149
};
146150

147-
RootActions.createNew = function(context, payload) {
148-
const result = { data: {} };
151+
RootActions.createNew = function (context, payload) {
152+
const result = {
153+
data: {}
154+
};
149155

150156
context.commit('createNew', {
151157
...payload,
@@ -155,7 +161,7 @@ export default {
155161
return result.data;
156162
};
157163

158-
Query.prototype.createNew = function() {
164+
Query.prototype.createNew = function () {
159165
let record = new this.model().$toJson();
160166

161167
record[pluginOptions.isNewFlagName] = true;
@@ -168,4 +174,4 @@ export default {
168174
return this.result.data;
169175
};
170176
}
171-
};
177+
};

0 commit comments

Comments
 (0)