This commit is contained in:
@@ -273,37 +273,33 @@ export default {
|
||||
vm.rights = window.$gz.role.getRights(window.$gz.type.Widget);
|
||||
vm.formState.readOnly = !vm.rights.change;
|
||||
window.$gz.eventBus.$on("menu-click", clickHandler);
|
||||
|
||||
//id 0 means create a new record don't load one
|
||||
if (vm.$route.params.recordid != 0) {
|
||||
vm.getDataFromApi(vm.$route.params.recordid);
|
||||
//is there already an obj from a prior operation?
|
||||
if (this.$route.params.obj) {
|
||||
//yes, no need to fetch it
|
||||
this.obj = this.$route.params.obj;
|
||||
vm.formState.loading = false; //here we handle it immediately
|
||||
} else {
|
||||
vm.getDataFromApi(vm.$route.params.recordid); //let getdata handle loading
|
||||
}
|
||||
} else {
|
||||
//setup for new record
|
||||
//Update the form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
dirty: false,
|
||||
valid: true,
|
||||
loading: false
|
||||
});
|
||||
|
||||
generateMenu(vm);
|
||||
vm.formState.loading = false; //here we handle it immediately
|
||||
}
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
dirty: false,
|
||||
valid: true
|
||||
});
|
||||
|
||||
generateMenu(vm);
|
||||
} catch (error) {
|
||||
window.$gz.errorHandler.handleFormError(error, vm);
|
||||
} finally {
|
||||
vm.formState.ready = true;
|
||||
}
|
||||
},
|
||||
beforeRouteUpdate(to, from, next) {
|
||||
//This triggers a fetch of the data when the ID value changes on the route
|
||||
//which happens on duplicate, submit new record and change of active record id (backward nav/forward nav etc)
|
||||
if (to.params.recordid != 0) {
|
||||
this.getDataFromApi(to.params.recordid);
|
||||
}
|
||||
next();
|
||||
// react to route changes...
|
||||
// don't forget to call next()
|
||||
},
|
||||
async beforeRouteLeave(to, from, next) {
|
||||
if (!this.formState.dirty || JUST_DELETED) {
|
||||
next();
|
||||
@@ -442,6 +438,8 @@ export default {
|
||||
window.$gz.form.setErrorBoxErrors(vm);
|
||||
} else {
|
||||
vm.obj = res.data;
|
||||
//modify the menu as necessary
|
||||
generateMenu(vm);
|
||||
//Update the form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
@@ -449,55 +447,61 @@ export default {
|
||||
valid: true,
|
||||
loading: false
|
||||
});
|
||||
|
||||
//modify the menu as necessary
|
||||
generateMenu(vm);
|
||||
}
|
||||
} catch (error) {
|
||||
//Update the form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
loading: false
|
||||
});
|
||||
window.$gz.errorHandler.handleFormError(error, vm);
|
||||
} finally {
|
||||
vm.formState.loading = false;
|
||||
}
|
||||
},
|
||||
async submit() {
|
||||
let vm = this;
|
||||
if (vm.canSave) {
|
||||
if (vm.canSave == false) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
vm.formState.loading = true;
|
||||
let url = API_BASE_URL; // + vm.$route.params.recordid;
|
||||
//clear any errors vm might be around from previous submit
|
||||
window.$gz.form.deleteAllErrorBoxErrors(vm);
|
||||
|
||||
try {
|
||||
//clear any errors vm might be around from previous submit
|
||||
window.$gz.form.deleteAllErrorBoxErrors(vm);
|
||||
let res = await window.$gz.api.upsert(url, vm.obj);
|
||||
vm.formState.loading = false;
|
||||
if (res.error) {
|
||||
vm.formState.serverError = res.error;
|
||||
window.$gz.form.setErrorBoxErrors(vm);
|
||||
let res = await window.$gz.api.upsert(url, vm.obj);
|
||||
|
||||
if (res.error) {
|
||||
vm.formState.serverError = res.error;
|
||||
window.$gz.form.setErrorBoxErrors(vm);
|
||||
} else {
|
||||
//Logic for detecting if a post or put: if id then it was a post, if no id then it was a put
|
||||
if (res.data.id) {
|
||||
//POST - whole new object returned
|
||||
vm.obj = res.data;
|
||||
//Change URL to new record
|
||||
//NOTE: will not cause a page re-render, almost nothing does unless forced with a KEY property or using router.GO()
|
||||
|
||||
this.$router.push({
|
||||
name: "widget-edit",
|
||||
params: {
|
||||
recordid: res.data.id,
|
||||
obj: res.data // Pass data object to new form
|
||||
}
|
||||
});
|
||||
} else {
|
||||
//Logic for detecting if a post or put: if id then it was a post, if no id then it was a put
|
||||
if (res.data.id) {
|
||||
//Handle "post" of new record (CREATE)
|
||||
|
||||
//change url to new record in history
|
||||
//NOTE: will not cause a page re-render, almost nothing does unless forced with a KEY property or using router.GO()
|
||||
//but will trigger navigation guard beforeRouteUpdate which we use here in this form to fetch data freshly
|
||||
vm.$router.replace(vm.$route.fullPath.slice(0, -1) + res.data.id);
|
||||
} else {
|
||||
//Handle "put" of an existing record (UPDATE)
|
||||
vm.obj.concurrency = res.data.concurrency;
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
dirty: false
|
||||
});
|
||||
}
|
||||
//PUT - only concurrency token is returned (**warning, if server changes object other fields then this needs to act more like POST above but is more efficient this way**)
|
||||
//Handle "put" of an existing record (UPDATE)
|
||||
vm.obj.concurrency = res.data.concurrency;
|
||||
}
|
||||
} catch (error) {
|
||||
vm.formState.loading = false;
|
||||
window.$gz.errorHandler.handleFormError(error, vm);
|
||||
//Update the form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
dirty: false,
|
||||
valid: true
|
||||
});
|
||||
}
|
||||
} catch (ex) {
|
||||
window.$gz.errorHandler.handleFormError(ex, vm);
|
||||
} finally {
|
||||
vm.formState.loading = false;
|
||||
}
|
||||
},
|
||||
async remove() {
|
||||
@@ -552,22 +556,23 @@ export default {
|
||||
try {
|
||||
window.$gz.form.deleteAllErrorBoxErrors(vm);
|
||||
let res = await window.$gz.api.upsert(url);
|
||||
vm.formState.loading = false;
|
||||
if (res.error) {
|
||||
vm.formState.serverError = res.error;
|
||||
window.$gz.form.setErrorBoxErrors(vm);
|
||||
} else {
|
||||
//Navigate to new record
|
||||
vm.$router.push(
|
||||
window.$gz.api.replaceAfterLastSlash(
|
||||
vm.$route.fullPath,
|
||||
res.data.id
|
||||
)
|
||||
);
|
||||
this.$router.push({
|
||||
name: "widget-edit",
|
||||
params: {
|
||||
recordid: res.data.id,
|
||||
obj: res.data // Pass data object to new form
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
} catch (ex) {
|
||||
window.$gz.errorHandler.handleFormError(ex, vm);
|
||||
} finally {
|
||||
vm.formState.loading = false;
|
||||
window.$gz.errorHandler.handleFormError(error, vm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user