-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
232 lines (203 loc) · 7.53 KB
/
index.js
File metadata and controls
232 lines (203 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const defaultOptions = {
isDirtyFlagName: '$isDirty',
isNewFlagName: '$isNew',
exposeFlagsExternally: true
};
export default {
install(components, installOptions) {
const pluginOptions = {
...defaultOptions,
...installOptions
};
const {
Model,
Query,
RootGetters,
Getters,
RootMutations,
RootActions,
Actions
} = components;
/**
* Flags are exposed by default when stringiying into JSON.
* This can be deactivated by setting the flag to false
*/
if (pluginOptions.exposeFlagsExternally) {
const localFieldModel = {
[pluginOptions.isDirtyFlagName]: Model.attr(false),
[pluginOptions.isNewFlagName]: Model.attr(false)
};
const _saveGetFiedsMethod = Model.prototype.$fields;
Model.prototype.$fields = function () {
const existing = _saveGetFiedsMethod.call(this);
return Object.assign({}, existing, localFieldModel);
}
}
/**
* Overwriting the $fill method used when calling
* new Model() to inject automatically the 2 flags
* with default value to false and set value to the
* provided record if it exists
*/
// Save
const _saveFillMethod = Model.prototype.$fill;
// Overwrite
Model.prototype.$fill = function (record) {
_saveFillMethod.call(this, record); // Calling initial
// $isDirty
this[pluginOptions.isDirtyFlagName] =
(record && record[pluginOptions.isDirtyFlagName]) || false;
// $isNew
this[pluginOptions.isNewFlagName] =
(record && record[pluginOptions.isNewFlagName]) || false;
};
/**
* Setting $isDirty = true when updating an entity.
* This is the only automatic way which sets this flag
* to true once it's in the store.
*/
let _ignoreIsDirtyFlag = false;
Query.on('beforeUpdate', function (model) {
if (!_ignoreIsDirtyFlag)
model[pluginOptions.isDirtyFlagName] = true;
});
/**
* Providing the resetFlags actions
*/
RootActions.resetAllDirtyFlags = function ({
rootGetters
}) {
const allDirty = rootGetters['entities/allDirty']();
_ignoreIsDirtyFlag = true;
allDirty.forEach(e =>
e.$update({
[pluginOptions.isDirtyFlagName]: false
})
);
_ignoreIsDirtyFlag = false;
};
// Overwriting to add preventDirtyFlag option
const _insertOrUpdate = RootMutations.insertOrUpdate;
RootMutations.insertOrUpdate = function (state, payload) {
if (payload.preventDirtyFlag === true) {
_ignoreIsDirtyFlag = true;
_insertOrUpdate.call(this, state, payload);
_ignoreIsDirtyFlag = false;
} else
_insertOrUpdate.call(this, state, payload);
};
// Overwriting to add preventDirtyFlag option
const _update = RootMutations.update;
RootMutations.update = function (state, payload) {
if (payload.preventDirtyFlag === true) {
_ignoreIsDirtyFlag = true;
_update.call(this, state, payload);
_ignoreIsDirtyFlag = false;
} else
_update.call(this, state, payload);
};
/**
* Providing the allDirty getter
*/
RootGetters.allDirty = function (state) {
return function (entity) {
if (entity) {
return new Query(Model.database().store, entity)
.where(elt => elt[pluginOptions.isDirtyFlagName])
.get();
} else {
let result = [];
const allEntities = Model.database().entities;
allEntities.forEach(e => {
let elts = new Query(Model.database().store, e.name)
.where(elt => elt[pluginOptions.isDirtyFlagName])
.get();
result = result.concat(elts);
});
return [...new Set(result)];
}
};
};
Getters.allDirty = function (state, _getters, _rootState, rootGetters) {
return function () {
return rootGetters[`${state.$connection}/allDirty`](
state.$name
);
};
};
/**
* Providing the allNew getter
*/
RootGetters.allNew = function (state) {
return function (entity) {
if (entity) {
return new Query(Model.database().store, entity)
.where(elt => elt[pluginOptions.isNewFlagName])
.get();
} else {
let result = [];
const allEntities = Model.database().entities;
allEntities.forEach(e => {
let elts = new Query(Model.database().store, e.name)
.where(elt => elt[pluginOptions.isNewFlagName])
.get();
result = result.concat(elts);
});
return [...new Set(result)];
}
};
};
Getters.allNew = function (state, _getters, _rootState, rootGetters) {
return function () {
return rootGetters[`${state.$connection}/allNew`](state.$name);
};
};
/**
* Providing the createNew factory
* When called on the Model instead of new, it will
* set the 2 flags to true
*/
Model.createNew = function (insertInStore = true) {
if (insertInStore) return this.dispatch('createNew');
else {
let record = new this();
record[pluginOptions.isNewFlagName] = true;
record[pluginOptions.isDirtyFlagName] = true;
return Promise.resolve(record);
}
};
RootMutations.createNew = function (state, payload) {
const entity = payload.entity;
const result = payload.result;
result.data = (new Query(Model.database().store(), entity)).createNew();
};
Actions.createNew = function (context) {
const state = context.state;
const entity = state.$name;
return context.dispatch(
`${state.$connection}/createNew`, {
entity
}, {
root: true
}
);
};
RootActions.createNew = function (context, payload) {
const result = {
data: {}
};
context.commit('createNew', {
...payload,
result
});
return result.data;
};
Query.prototype.createNew = function () {
let record = new this.model().$toJson();
record[pluginOptions.isNewFlagName] = true;
record[pluginOptions.isDirtyFlagName] = true;
const result = this.insert(record, {});
return result[this.entity][0];
};
}
};