2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-02 21:38:48 +00:00

Back-fill newly created model data into the parent modal form

This commit is contained in:
Oliver Walters 2021-08-07 23:27:31 +10:00
parent 759a3724b5
commit 25af8559ba

View File

@ -974,10 +974,10 @@ function initializeRelatedFields(fields, options) {
switch (field.type) { switch (field.type) {
case 'related field': case 'related field':
initializeRelatedField(name, field, options); initializeRelatedField(field, fields, options);
break; break;
case 'choice': case 'choice':
initializeChoiceField(name, field, options); initializeChoiceField(field, fields, options);
break; break;
} }
} }
@ -992,7 +992,9 @@ function initializeRelatedFields(fields, options) {
* - field: The field data object * - field: The field data object
* - options: The options object provided by the client * - options: The options object provided by the client
*/ */
function addSecondaryModal(name, field, options) { function addSecondaryModal(field, fields, options) {
var name = field.name;
var secondary = field.secondary; var secondary = field.secondary;
@ -1005,22 +1007,41 @@ function addSecondaryModal(name, field, options) {
$(options.modal).find(`label[for="id_${name}"]`).append(html); $(options.modal).find(`label[for="id_${name}"]`).append(html);
// TODO: Launch a callback // Callback function when the secondary button is pressed
$(options.modal).find(`#btn-new-${name}`).click(function() { $(options.modal).find(`#btn-new-${name}`).click(function() {
if (secondary.callback) { // Determine the API query URL
// A "custom" callback can be specified for the button var url = secondary.api_url || field.api_url;
secondary.callback(field, options);
} else if (secondary.api_url) {
// By default, a new modal form is created, with the parameters specified
// The parameters match the "normal" form creation parameters
secondary.onSuccess = function(data, opts) { // Extract form values at time of button press
setRelatedFieldData(name, data, options); var data = extractFormData(fields, options)
};
constructForm(secondary.api_url, secondary); // Allow the secondary form to be "prefilled" with a custom function
if (secondary.prefill) {
secondary.fields = secondary.prefill(data);
} }
// If no onSuccess function is defined, provide a default one
if (!secondary.onSuccess) {
secondary.onSuccess = function(data, opts) {
// Force refresh from the API, to get full detail
inventreeGet(`${url}${data.pk}/`, {}, {
success: function(responseData) {
setRelatedFieldData(name, responseData, options);
}
});
};
}
// Method should be "POST" for creation
secondary.method = secondary.method || 'POST';
constructForm(
url,
secondary
);
}); });
} }
@ -1034,7 +1055,9 @@ function addSecondaryModal(name, field, options) {
* - field: Field definition from the OPTIONS request * - field: Field definition from the OPTIONS request
* - options: Original options object provided by the client * - options: Original options object provided by the client
*/ */
function initializeRelatedField(name, field, options) { function initializeRelatedField(field, fields, options) {
var name = field.name;
if (!field.api_url) { if (!field.api_url) {
// TODO: Provide manual api_url option? // TODO: Provide manual api_url option?
@ -1047,7 +1070,7 @@ function initializeRelatedField(name, field, options) {
// Add a button to launch a 'secondary' modal // Add a button to launch a 'secondary' modal
if (field.secondary != null) { if (field.secondary != null) {
addSecondaryModal(name, field, options); addSecondaryModal(field, fields, options);
} }
// TODO: Add 'placeholder' support for entry select2 fields // TODO: Add 'placeholder' support for entry select2 fields