Skip to content

Commit a8db834

Browse files
committed
refactor: improve beforeunload event handling in CreateView and EditView
1 parent 5b4ec30 commit a8db834

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

adminforth/spa/src/views/CreateView.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,17 @@ function checkIfWeCanLeavePage() {
124124
return wasSaveSuccessful.value || cancelButtonClicked.value || JSON.stringify(record.value) === JSON.stringify(initialValues.value);
125125
}
126126
127-
window.addEventListener('beforeunload', (event) => {
127+
function onBeforeUnload(event: BeforeUnloadEvent) {
128128
if (!checkIfWeCanLeavePage()) {
129129
event.preventDefault();
130130
event.returnValue = '';
131131
}
132-
});
132+
}
133+
134+
window.addEventListener('beforeunload', onBeforeUnload);
133135
134136
onBeforeUnmount(() => {
135-
window.removeEventListener('beforeunload', () => {});
137+
window.removeEventListener('beforeunload', onBeforeUnload);
136138
});
137139
138140
onBeforeRouteLeave(async (to, from, next) => {

adminforth/spa/src/views/EditView.vue

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,21 @@ const initialRecord = computed(() => coreStore.record);
105105
const wasSaveSuccessful = ref(false);
106106
const cancelButtonClicked = ref(false);
107107
108-
function checkIfWeCanLeavePage() {
109-
return wasSaveSuccessful.value || cancelButtonClicked.value || JSON.stringify(record.value) === JSON.stringify(initialRecord.value);
110-
}
111-
112-
window.addEventListener('beforeunload', (event) => {
108+
function onBeforeUnload(event: BeforeUnloadEvent) {
113109
if (!checkIfWeCanLeavePage()) {
114110
event.preventDefault();
115111
event.returnValue = '';
116112
}
117-
});
113+
}
114+
115+
function checkIfWeCanLeavePage() {
116+
return wasSaveSuccessful.value || cancelButtonClicked.value || JSON.stringify(record.value) === JSON.stringify(initialRecord.value);
117+
}
118+
119+
window.addEventListener('beforeunload', onBeforeUnload);
118120
119121
onBeforeUnmount(() => {
120-
window.removeEventListener('beforeunload', () => {});
122+
window.removeEventListener('beforeunload', onBeforeUnload);
121123
});
122124
123125
onBeforeRouteLeave(async (to, from, next) => {

0 commit comments

Comments
 (0)