diff --git a/src/components/FormModel.vue b/src/components/FormModel.vue index 79407dcb4..f5cfdd734 100644 --- a/src/components/FormModel.vue +++ b/src/components/FormModel.vue @@ -109,7 +109,7 @@ const defaultButtons = computed(() => ({ })); onMounted(async () => { - originalData.value = $props.formInitialData; + originalData.value = JSON.parse(JSON.stringify($props.formInitialData ?? {})); nextTick(() => (componentIsRendered.value = true)); @@ -124,7 +124,9 @@ onMounted(async () => { () => formData.value, (newVal, oldVal) => { if (!oldVal) return; - hasChanges.value = !isResetting.value && newVal; + hasChanges.value = + !isResetting.value && + JSON.stringify(newVal) !== JSON.stringify(originalData.value); isResetting.value = false; }, { deep: true } @@ -145,6 +147,7 @@ watch(formUrl, async () => { }); onBeforeRouteLeave((to, from, next) => { + console.log('entro aqui antes'); if (hasChanges.value && $props.observeFormChanges) quasar.dialog({ component: VnConfirm, @@ -177,7 +180,7 @@ async function fetch() { } } -async function save() { +async function save(emit = true) { if ($props.observeFormChanges && !hasChanges.value) return notify('globals.noChanges', 'negative'); @@ -194,18 +197,18 @@ async function save() { if ($props.urlCreate) notify('globals.dataCreated', 'positive'); - updateAndEmit(response?.data, 'onDataSaved'); hasChanges.value = false; + isLoading.value = false; + + if (emit) updateAndEmit(response?.data, 'onDataSaved'); } catch (err) { console.error(err); notify('errors.writeRequest', 'negative'); - } finally { - isLoading.value = false; } } async function saveAndGo() { - await save(); + await save(null); push({ path: $props.goTo }); } @@ -240,7 +243,7 @@ function updateAndEmit(val, evt) { emit(evt, state.get($props.model)); } -defineExpose({ save, isLoading, hasChanges }); +defineExpose({ save, saveAndGo, isLoading, hasChanges });