Woohoo for .then

This commit is contained in:
Oliver 2018-05-09 21:32:48 +10:00
parent 517569f510
commit 690ec83265
2 changed files with 21 additions and 37 deletions

View File

@ -18,7 +18,7 @@ function getCookie(name) {
} }
function inventreeGet(url, filters={}, options={}) { function inventreeGet(url, filters={}, options={}) {
$.ajax({ return $.ajax({
url: url, url: url,
type: 'GET', type: 'GET',
data: filters, data: filters,
@ -39,7 +39,7 @@ function inventreeGet(url, filters={}, options={}) {
}); });
} }
} }
}) });
} }
function inventreeUpdate(url, data={}, options={}) { function inventreeUpdate(url, data={}, options={}) {
@ -57,7 +57,7 @@ function inventreeUpdate(url, data={}, options={}) {
//var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val(); //var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
var csrftoken = getCookie('csrftoken'); var csrftoken = getCookie('csrftoken');
$.ajax({ return $.ajax({
beforeSend: function(xhr, settings) { beforeSend: function(xhr, settings) {
xhr.setRequestHeader('X-CSRFToken', csrftoken); xhr.setRequestHeader('X-CSRFToken', csrftoken);
}, },
@ -69,20 +69,12 @@ function inventreeUpdate(url, data={}, options={}) {
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 (options.success) {
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 (options.error) {
options.error({
error: thrownError
});
} }
} });
})
} }
// Return list of parts with optional filters // Return list of parts with optional filters

View File

@ -50,11 +50,12 @@ function updateStock(items, options={}) {
var item = items[idx]; var item = items[idx];
var vMin = 0; var vMin = 0;
var vMax = item.quantity; var vMax = 0;
var vCur = item.quantity; var vCur = item.quantity;
if (options.action == 'remove') { if (options.action == 'remove') {
vCur = 0; vCur = 0;
vMax = item.quantity;
} }
else if (options.action == 'add') { else if (options.action == 'add') {
vCur = 0; vCur = 0;
@ -140,18 +141,15 @@ function updateStock(items, options={}) {
'notes': $(modal).find('#stocktake-notes').val() 'notes': $(modal).find('#stocktake-notes').val()
}, },
{ {
success: function(response) { method: 'post',
closeModal(modal); }).then(function(response) {
if (options.success) { closeModal(modal);
options.success(); if (options.success) {
} options.success();
}, }
error: function(error) { }).fail(function(xhr, status, error) {
alert(error); alert(error);
}, });
method: 'post'
}
);
}); });
} }
@ -162,20 +160,14 @@ function adjustStock(options) {
else { else {
// Lookup of individual item // Lookup of individual item
if (options.query.pk) { if (options.query.pk) {
getStockDetail(options.query.pk, getStockDetail(options.query.pk).then(function(response) {
{ updateStock([response], options);
success: function(response) { });
updateStock([response], options);
}
});
} }
else { else {
getStockList(options.query, getStockList(options.query).then(function(response) {
{ updateStock(response, options);
success: function(response) { });
updateStock(response, options);
}
});
} }
} }
} }