Bug 38201: Move getResource to BaseResource
Also adds refreshTemplateState, for use to update components on state changes when access to the component context is not available Signed-off-by: Michaela Sieber <michaela.sieber@kit.edu> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
parent
29233b0b6e
commit
3876b74ad8
|
|
@ -48,7 +48,7 @@
|
|||
v-if="['add', 'edit'].includes(routeAction)"
|
||||
v-bind="{
|
||||
...passCommonProps(),
|
||||
resource: newResource,
|
||||
newResource,
|
||||
onSubmit,
|
||||
...optionalResourceProps,
|
||||
}"
|
||||
|
|
@ -108,13 +108,11 @@ export default {
|
|||
appendToShow: this.appendToShow || null,
|
||||
nameAttr: this.nameAttr || null,
|
||||
idAttr: this.idAttr || null,
|
||||
afterResourceFetch:
|
||||
this.afterResourceFetch ||
|
||||
((component, resource, caller) => null),
|
||||
};
|
||||
return {
|
||||
resourceToBeGenerated: {},
|
||||
optionalResourceProps,
|
||||
refreshTemplate: false,
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
|
|
@ -141,6 +139,8 @@ export default {
|
|||
resourceAttrs: this.resourceAttrs,
|
||||
listComponent: this.listComponent,
|
||||
resourceNamePlural: this.resourceNamePlural,
|
||||
getResource: this.getResource,
|
||||
key: this.refreshTemplate,
|
||||
};
|
||||
|
||||
return commonProps;
|
||||
|
|
@ -497,6 +497,25 @@ export default {
|
|||
return [...acc, groupInfo];
|
||||
}, []);
|
||||
},
|
||||
async getResource(resourceId, componentData, caller) {
|
||||
this.apiClient.get(resourceId).then(
|
||||
resource => {
|
||||
componentData.resource = resource;
|
||||
if (this.afterResourceFetch) {
|
||||
this.afterResourceFetch(
|
||||
componentData,
|
||||
resource,
|
||||
caller
|
||||
);
|
||||
}
|
||||
componentData.initialized = true;
|
||||
},
|
||||
error => {}
|
||||
);
|
||||
},
|
||||
refreshTemplateState() {
|
||||
this.refreshTemplate = !this.refreshTemplate;
|
||||
},
|
||||
},
|
||||
computed: {
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -180,7 +180,7 @@ export default {
|
|||
//if (train_id) {
|
||||
return client.trains
|
||||
.update(train, train_id)
|
||||
.then(() => this.getTrain(train_id));
|
||||
.then(() => this.refreshTemplateState());
|
||||
//} else {
|
||||
// return client.trains
|
||||
// .create(train)
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ export default {
|
|||
data() {
|
||||
return {
|
||||
initialized: false,
|
||||
resourceToEdit: null,
|
||||
resource: null,
|
||||
};
|
||||
},
|
||||
props: {
|
||||
|
|
@ -156,46 +156,23 @@ export default {
|
|||
i18n: Object,
|
||||
resourceAttrs: Array,
|
||||
listComponent: String,
|
||||
resource: Object,
|
||||
newResource: Object,
|
||||
onSubmit: Function,
|
||||
resourceNamePlural: String,
|
||||
getFieldGroupings: Function,
|
||||
formGroupsDisplayMode: String,
|
||||
afterResourceFetch: Function,
|
||||
getResource: Function,
|
||||
},
|
||||
created() {
|
||||
if (this.$route.params[this.idAttr]) {
|
||||
this.getResource(this.$route.params[this.idAttr]);
|
||||
this.getResource(this.$route.params[this.idAttr], this, "form");
|
||||
} else {
|
||||
this.initialized = true;
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
async getResource(resourceId) {
|
||||
const resourceAttrs = this.resourceAttrs;
|
||||
this.apiClient.get(resourceId).then(
|
||||
resource => {
|
||||
resourceAttrs.forEach(attr => {
|
||||
if (
|
||||
attr.formatForForm &&
|
||||
typeof attr.formatForForm === "function"
|
||||
) {
|
||||
resource[attr.name] = attr.formatForForm(
|
||||
resource[attr.name]
|
||||
);
|
||||
}
|
||||
});
|
||||
this.resourceToEdit = resource;
|
||||
this.afterResourceFetch(this, resource, "form");
|
||||
this.initialized = true;
|
||||
},
|
||||
error => {}
|
||||
);
|
||||
},
|
||||
},
|
||||
computed: {
|
||||
resourceToAddOrEdit() {
|
||||
return this.resourceToEdit || this.resource;
|
||||
return this.resource || this.newResource;
|
||||
},
|
||||
},
|
||||
components: {
|
||||
|
|
|
|||
|
|
@ -63,24 +63,10 @@ export default {
|
|||
appendToShow: Function,
|
||||
getToolbarButtons: Function,
|
||||
afterResourceFetch: Function,
|
||||
getResource: Function,
|
||||
},
|
||||
created() {
|
||||
this.getResource(this.$route.params[this.idAttr]);
|
||||
},
|
||||
// beforeRouteUpdate(to, from) {
|
||||
// this.resource = this.getResource(to.params[this.idAttr])
|
||||
// },
|
||||
methods: {
|
||||
async getResource(resourceId) {
|
||||
this.apiClient.get(resourceId).then(
|
||||
resource => {
|
||||
this.resource = resource;
|
||||
this.afterResourceFetch(this, resource, "show");
|
||||
this.initialized = true;
|
||||
},
|
||||
error => {}
|
||||
);
|
||||
},
|
||||
this.getResource(this.$route.params[this.idAttr], this, "show");
|
||||
},
|
||||
components: { Toolbar, ShowElement },
|
||||
name: "ResourceShow",
|
||||
|
|
|
|||
Loading…
Reference in New Issue