From 507e3de05b3f7a0740f60413cdb7441940680336 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 27 Apr 2018 00:16:54 +1000 Subject: [PATCH] Generecised launchModalForm - Add 'options' e.g. redirect / data --- .../part/templates/part/category_detail.html | 8 +++---- InvenTree/static/script/modal_form.js | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/InvenTree/part/templates/part/category_detail.html b/InvenTree/part/templates/part/category_detail.html index 0e2a3db91d..1c6212f6f4 100644 --- a/InvenTree/part/templates/part/category_detail.html +++ b/InvenTree/part/templates/part/category_detail.html @@ -50,15 +50,15 @@ $("#create-cat").click(function() { launchModalForm("#modal-form", "{% url 'category-create' %}", - {category: {{ category.id }} - }); + {data: {category: {{ category.id }} + }}); }); $("#create-part").click( function() { launchModalForm("#modal-form", "{% url 'part-create' %}", - {category: {{ category.id }} - }); + {data: {category: {{ category.id }} + }}); }); diff --git a/InvenTree/static/script/modal_form.js b/InvenTree/static/script/modal_form.js index 2107c5038a..447ca589ea 100644 --- a/InvenTree/static/script/modal_form.js +++ b/InvenTree/static/script/modal_form.js @@ -62,18 +62,17 @@ function launchDeleteForm(modal, url, options) { }); } -function launchModalForm(modal, url, data, options) { +function launchModalForm(modal, url, options) { $(modal).on('shown.bs.modal', function () { $(modal + ' .modal-form-content').scrollTop(0); }); - $.ajax({ - url: url, // Where to request the data from - type: 'get', // GET request - data: data, // Any additional context data (e.g. initial values) + ajax_data = { + url: url, + type: 'get', dataType: 'json', - beforeSend: function() { + beforeSend: function () { $(modal).modal('show'); }, success: function(response) { @@ -98,7 +97,15 @@ function launchModalForm(modal, url, data, options) { alert('Error requesting form data:\n' + thrownError); $(modal).modal('hide'); } - }); + }; + + // Add in extra request data if provided + if (options.data) { + ajax_data.data = options.data; + } + + // Send the AJAX request + $.ajax(ajax_data); $(modal).on('click', '#modal-form-submit', function() {