Callback for API

This commit is contained in:
Oliver 2018-05-06 19:26:11 +10:00
parent 19ce05931c
commit 890c9002aa

View File

@ -17,7 +17,7 @@ function getCookie(name) {
return cookieValue; return cookieValue;
} }
function inventreeGet(url, filters={}) { function inventreeGet(url, filters={}, callback=null) {
$.ajax({ $.ajax({
url: url, url: url,
type: 'GET', type: 'GET',
@ -25,17 +25,23 @@ function inventreeGet(url, filters={}) {
dataType: 'json', dataType: 'json',
success: function(response) { success: function(response) {
console.log('Success GET data at ' + url); console.log('Success GET data at ' + url);
return response; if (callback) {
callback(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);
return {error: thrownError}; if (callback) {
return {
error: thrownError;
}
}
} }
}) })
} }
function inventreeUpdate(url, data, final=false) { function inventreeUpdate(url, data, callback=null, final=false) {
if (final) { if (final) {
data["_is_final"] = true; data["_is_final"] = true;
} }
@ -55,12 +61,18 @@ function inventreeUpdate(url, data, 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);
return response; if (callback) {
callback(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);
return {error: thrownError}; if (callback) {
callback({
error: thrownError;
})
}
} }
}) })
} }