2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 20:16:44 +00:00

API improvements

- Add success and error callbacks
- Further improvements for modal forms
This commit is contained in:
Oliver 2018-05-06 20:20:39 +10:00
parent fa04a9ae61
commit c2bc65f903
2 changed files with 23 additions and 29 deletions

View File

@ -17,7 +17,7 @@ function getCookie(name) {
return cookieValue; return cookieValue;
} }
function inventreeGet(url, filters={}, callback=null) { function inventreeGet(url, filters={}, options={}) {
$.ajax({ $.ajax({
url: url, url: url,
type: 'GET', type: 'GET',
@ -25,15 +25,15 @@ function inventreeGet(url, filters={}, callback=null) {
dataType: 'json', dataType: 'json',
success: function(response) { success: function(response) {
console.log('Success GET data at ' + url); console.log('Success GET data at ' + url);
if (callback) { if (options.success) {
callback(response); options.success(response);
} }
}, },
error: function(xhr, ajaxOptions, thrownError) { error: function(xhr, ajaxOptions, thrownError) {
console.error('Error on GET at ' + url); console.error('Error on GET at ' + url);
console.error(thrownError); console.error(thrownError);
if (callback) { if (options.error) {
callback({ options.error({
error: thrownError error: thrownError
}); });
} }
@ -41,8 +41,8 @@ function inventreeGet(url, filters={}, callback=null) {
}) })
} }
function inventreeUpdate(url, data, callback=null, final=false) { function inventreeUpdate(url, data={}, options={}) {
if (final) { if ('final' in options && options.final) {
data["_is_final"] = true; data["_is_final"] = true;
} }
@ -61,15 +61,15 @@ function inventreeUpdate(url, data, callback=null, final=false) {
success: function(response, status) { success: function(response, status) {
response['_status_code'] = status; response['_status_code'] = status;
console.log('UPDATE object to ' + url + ' - result = ' + status); console.log('UPDATE object to ' + url + ' - result = ' + status);
if (callback) { if (options.success) {
callback(response); options.success(response);
} }
}, },
error: function(xhr, ajaxOptions, thrownError) { error: function(xhr, ajaxOptions, thrownError) {
console.error('Error on UPDATE to ' + url); console.error('Error on UPDATE to ' + url);
console.error(thrownError); console.error(thrownError);
if (callback) { if (options.error) {
callback({ options.error({
error: thrownError error: thrownError
}); });
} }
@ -78,25 +78,25 @@ function inventreeUpdate(url, data, callback=null, final=false) {
} }
// Return list of parts with optional filters // Return list of parts with optional filters
function getParts(filters={}) { function getParts(filters={}, options={}) {
return inventreeGet('/api/part/', filters); return inventreeGet('/api/part/', filters, options);
} }
// Return list of part categories with optional filters // Return list of part categories with optional filters
function getPartCategories(filters={}) { function getPartCategories(filters={}, options={}) {
return inventreeGet('/api/part/category/', filters); return inventreeGet('/api/part/category/', filters, options);
} }
function getStock(filters={}) { function getStock(filters={}, options={}) {
return inventreeGet('/api/stock/', filters); return inventreeGet('/api/stock/', filters, options);
} }
function getStockLocations(filters={}) { function getStockLocations(filters={}, options={}) {
return inventreeGet('/api/stock/location/', filters) return inventreeGet('/api/stock/location/', filters, options)
} }
function getCompanies(filters={}) { function getCompanies(filters={}, options={}) {
return inventreeGet('/api/company/', filters); return inventreeGet('/api/company/', filters, options);
} }
function updateStockItem(pk, data, final=false) { function updateStockItem(pk, data, final=false) {

View File

@ -50,7 +50,7 @@ function modalSetTitle(modal, title='') {
} }
function modalSetContent(modal, content='') { 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) { function injectModalForm(modal, form_html) {
// Inject the form data into the modal window // Inject the form data into the modal window
$(modal).find('.modal-form-content').html(form_html); $(modal).find('.modal-form-content').html(form_html);
attachSelect(modal);
// Attach to any 'select' inputs on the modal
// Provide search filtering of dropdown items
$(modal + ' .select').select2({
dropdownParent: $(modal),
dropdownAutoWidth: true,
});
} }
function handleModalForm(modal, url, options) { function handleModalForm(modal, url, options) {