JSFiddle - React, Tailwind, and code Playground
by st3fan
JavaScript
// TODO: Find a better place for this helper function
var handleResponse = function(responseArgs, successFn, errorFn) {
//console.log('handleResponse', this, arguments); // DEBUG
var defaultErrorFn = function(data) {
//console.log('defaultErrorFn', this, arguments); // DEBUG
var message = !data.CustomerError ? 'Tekniskt fel' :
(data.CustomerError.Title + '\n' + data.CustomerError.Description);
alert(message);
};
var data = responseArgs[0] || {},
success = !!data.d && data.d.Rc === 200,
callback = success ? successFn : (errorFn || defaultErrorFn);
data = success ? data.d.Data : data.d;
//console.log(success, callback, data); // DEBUG
if (typeof callback === 'function')
callback.call(this, data);
};
// Usage:
//$.ajax(...).success(function() {
handleResponse.call(this,
arguments,
function(data) { }, // successFn
function(data) { }, // errorFn
);
//});