From c2bc65f9033a70254234430e36a103ab5f31071b Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 6 May 2018 20:20:39 +1000 Subject: [PATCH] API improvements - Add success and error callbacks - Further improvements for modal forms --- InvenTree/static/script/inventree/api.js | 42 ++++++++++++------------ InvenTree/static/script/modal_form.js | 10 ++---- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/InvenTree/static/script/inventree/api.js b/InvenTree/static/script/inventree/api.js index be8222a6b5..7cd3c3d66c 100644 --- a/InvenTree/static/script/inventree/api.js +++ b/InvenTree/static/script/inventree/api.js @@ -17,7 +17,7 @@ function getCookie(name) { return cookieValue; } -function inventreeGet(url, filters={}, callback=null) { +function inventreeGet(url, filters={}, options={}) { $.ajax({ url: url, type: 'GET', @@ -25,15 +25,15 @@ function inventreeGet(url, filters={}, callback=null) { dataType: 'json', success: function(response) { console.log('Success GET data at ' + url); - if (callback) { - callback(response); + if (options.success) { + options.success(response); } }, error: function(xhr, ajaxOptions, thrownError) { console.error('Error on GET at ' + url); console.error(thrownError); - if (callback) { - callback({ + if (options.error) { + options.error({ error: thrownError }); } @@ -41,8 +41,8 @@ function inventreeGet(url, filters={}, callback=null) { }) } -function inventreeUpdate(url, data, callback=null, final=false) { - if (final) { +function inventreeUpdate(url, data={}, options={}) { + if ('final' in options && options.final) { data["_is_final"] = true; } @@ -61,15 +61,15 @@ function inventreeUpdate(url, data, callback=null, final=false) { success: function(response, status) { response['_status_code'] = status; console.log('UPDATE object to ' + url + ' - result = ' + status); - if (callback) { - callback(response); + if (options.success) { + options.success(response); } }, error: function(xhr, ajaxOptions, thrownError) { console.error('Error on UPDATE to ' + url); console.error(thrownError); - if (callback) { - callback({ + if (options.error) { + options.error({ error: thrownError }); } @@ -78,25 +78,25 @@ function inventreeUpdate(url, data, callback=null, final=false) { } // Return list of parts with optional filters -function getParts(filters={}) { - return inventreeGet('/api/part/', filters); +function getParts(filters={}, options={}) { + return inventreeGet('/api/part/', filters, options); } // Return list of part categories with optional filters -function getPartCategories(filters={}) { - return inventreeGet('/api/part/category/', filters); +function getPartCategories(filters={}, options={}) { + return inventreeGet('/api/part/category/', filters, options); } -function getStock(filters={}) { - return inventreeGet('/api/stock/', filters); +function getStock(filters={}, options={}) { + return inventreeGet('/api/stock/', filters, options); } -function getStockLocations(filters={}) { - return inventreeGet('/api/stock/location/', filters) +function getStockLocations(filters={}, options={}) { + return inventreeGet('/api/stock/location/', filters, options) } -function getCompanies(filters={}) { - return inventreeGet('/api/company/', filters); +function getCompanies(filters={}, options={}) { + return inventreeGet('/api/company/', filters, options); } function updateStockItem(pk, data, final=false) { diff --git a/InvenTree/static/script/modal_form.js b/InvenTree/static/script/modal_form.js index 4119c330e3..28a1a46df1 100644 --- a/InvenTree/static/script/modal_form.js +++ b/InvenTree/static/script/modal_form.js @@ -50,7 +50,7 @@ function modalSetTitle(modal, title='') { } function modalSetContent(modal, content='') { - $(modal + ' .modal-form-content').html(content); + $(modal).find('.modal-form-content').html(content); } @@ -134,13 +134,7 @@ function launchDeleteForm(modal, url, options = {}) { function injectModalForm(modal, form_html) { // Inject the form data into the modal window $(modal).find('.modal-form-content').html(form_html); - - // Attach to any 'select' inputs on the modal - // Provide search filtering of dropdown items - $(modal + ' .select').select2({ - dropdownParent: $(modal), - dropdownAutoWidth: true, - }); + attachSelect(modal); } function handleModalForm(modal, url, options) {