mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
Improve onEdit behavior: if any onEdit returns null, unset the field
This commit is contained in:
parent
ea4fb84d06
commit
e07a0b1477
1 changed files with 11 additions and 5 deletions
|
@ -65,7 +65,7 @@ export const newMutation = ({ collection, document, currentUser, validate, conte
|
||||||
|
|
||||||
// run onInsert step
|
// run onInsert step
|
||||||
_.keys(schema).forEach(fieldName => {
|
_.keys(schema).forEach(fieldName => {
|
||||||
if (!newDocument[fieldName] && schema[fieldName].onInsert) {
|
if (schema[fieldName].onInsert) {
|
||||||
const autoValue = schema[fieldName].onInsert(newDocument, currentUser);
|
const autoValue = schema[fieldName].onInsert(newDocument, currentUser);
|
||||||
if (autoValue) {
|
if (autoValue) {
|
||||||
newDocument[fieldName] = autoValue;
|
newDocument[fieldName] = autoValue;
|
||||||
|
@ -136,12 +136,18 @@ export const editMutation = ({ collection, documentId, set, unset, currentUser,
|
||||||
|
|
||||||
// run onEdit step
|
// run onEdit step
|
||||||
_.keys(schema).forEach(fieldName => {
|
_.keys(schema).forEach(fieldName => {
|
||||||
if (!document[fieldName] && schema[fieldName].onEdit) {
|
|
||||||
|
if (schema[fieldName].onEdit) {
|
||||||
const autoValue = schema[fieldName].onEdit(modifier, document, currentUser);
|
const autoValue = schema[fieldName].onEdit(modifier, document, currentUser);
|
||||||
if (autoValue) {
|
if (typeof autoValue !== 'undefined') {
|
||||||
|
if (autoValue === null) {
|
||||||
|
// if any autoValue returns null, then unset the field
|
||||||
|
modifier.$unset[fieldName] = true;
|
||||||
|
} else {
|
||||||
modifier.$set[fieldName] = autoValue;
|
modifier.$set[fieldName] = autoValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// run sync callbacks (on mongo modifier)
|
// run sync callbacks (on mongo modifier)
|
||||||
|
@ -194,7 +200,7 @@ export const removeMutation = ({ collection, documentId, currentUser, validate,
|
||||||
|
|
||||||
// run onRemove step
|
// run onRemove step
|
||||||
_.keys(schema).forEach(fieldName => {
|
_.keys(schema).forEach(fieldName => {
|
||||||
if (!document[fieldName] && schema[fieldName].onRemove) {
|
if (schema[fieldName].onRemove) {
|
||||||
schema[fieldName].onRemove(document, currentUser);
|
schema[fieldName].onRemove(document, currentUser);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue