-
-
Save igordata/c9b05582bb88b920afb55912ef4a1b3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param options | |
var defaults = { | |
url: '', | |
data: {}, | |
onSuccess: false, | |
onError: false, | |
onFail: false, | |
popupOnSuccess: false, | |
popupOnError: false, | |
messageOnSuccess: false, | |
messageOnError: false, | |
redirect: false, | |
}; | |
*/ | |
function apirequest(options) { | |
var response = {}; | |
var defaults = { | |
url: '', | |
data: {}, | |
onSuccess: false, | |
onError: false, | |
onFail: function (reply) { | |
if (typeof console === "undefined") { | |
this.console = {log: function() {}}; | |
} | |
console.log('Request to ' + settings.url + ' can not be parsed. Reply: '); | |
console.log(reply); | |
if (settings.popupOnError) { | |
noticePopup({text: 'Сервер ответил ошибкой', type: 'error'}); | |
} | |
}, | |
onDone: false, | |
after: false, | |
popupOnSuccess: false, | |
popupOnError: false, | |
messageOnSuccess: false, | |
messageOnError: false, | |
redirect: false, | |
}; | |
var settings = $.extend({}, defaults, options); | |
var request = { | |
onFail: function (reply) { | |
if (typeof(settings.onFail) === "function") { | |
settings.onFail(reply); | |
} | |
}, | |
onDone: function () { | |
if (typeof(settings.onDone) === "function") { | |
settings.onDone(); | |
} | |
}, | |
onError: function () { | |
if (typeof(settings.onError) === "function") { | |
settings.onError(response); | |
} | |
if (settings.popupOnError) { | |
this.popup(); | |
} | |
if (settings.messageOnError) { | |
this.messages(); | |
} | |
}, | |
onSuccess: function () { | |
if (typeof(settings.onSuccess) === "function") { | |
settings.onSuccess(response); | |
} | |
if (settings.redirect && response.checkRedirect()) { | |
response.doRedirect(); | |
} | |
if (settings.reload && response.checkReload()) { | |
response.doReload(); | |
} | |
if (settings.popupOnSuccess) { | |
this.popup(); | |
} | |
if (settings.messageOnSuccess) { | |
this.messages(); | |
} | |
}, | |
after: function () { | |
if (typeof(settings.after) === "function") { | |
settings.after(response); | |
} | |
}, | |
popup: function () { | |
response.popupFirstMessage(); | |
}, | |
messages: function () { | |
response.drawAllMessages(); | |
} | |
}; | |
this.getToken = function () { | |
if (typeof JohnSnow === 'undefined') { | |
return false; | |
} | |
if (typeof JohnSnow.tokens === 'undefined') { | |
return false; | |
} | |
if (typeof JohnSnow.tokens[settings.url] === 'undefined') { | |
return false; | |
} | |
return JohnSnow.tokens[settings.url]; | |
}; | |
var ajaxSettings = {}; | |
ajaxSettings.url = settings.url; | |
ajaxSettings.type = 'POST'; | |
ajaxSettings.data = settings.data; | |
var token = this.getToken(); | |
var pd = Object.prototype.toString.call(settings.data) === "[object FormData]"; | |
if (pd) { | |
/* form data */ | |
ajaxSettings.dataType = 'text'; | |
ajaxSettings.processData = false; | |
ajaxSettings.contentType = false; | |
ajaxSettings.data.append('token', token); | |
} else { | |
/* general */ | |
ajaxSettings.data.token = token; | |
} | |
ajaxSettings.success = function (reply) { | |
if (ajaxResponseParse === undefined) { | |
return false; | |
} | |
response = ajaxResponseParse(reply); | |
if (!response) { | |
request.onFail(reply); | |
return false; | |
} | |
if (response.isStatusError()) { | |
request.onError(); | |
} | |
if (response.isStatusOK()) { | |
request.onSuccess(); | |
} | |
request.after(); | |
} | |
$.ajax(ajaxSettings).done(function () { | |
request.onDone(); | |
}).error(function (reply) { | |
request.onFail(reply); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment