From: Alexander Ebert Date: Sat, 30 May 2015 19:18:05 +0000 (+0200) Subject: Resolved a few issues and simplified API of `Ajax` X-Git-Tag: 3.0.0_Beta_1~2301 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=3b88c9ada57148194beafbdbc4d5105a5e25dd83;p=GitHub%2FWoltLab%2FWCF.git Resolved a few issues and simplified API of `Ajax` --- diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Ajax.js b/wcfsetup/install/files/js/WoltLab/WCF/Ajax.js index 5d6e930df8..eaf6bb8b2c 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/Ajax.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/Ajax.js @@ -16,17 +16,6 @@ define(['AjaxRequest', 'Core', 'ObjectMap'], function(AjaxRequest, Core, ObjectM */ function Ajax() {}; Ajax.prototype = { - /** - * Shorthand function to perform a request against the WCF-API. - * - * @param {object} callbackObject callback object - * @param {object=} data request data - * @return {AjaxRequest} - */ - api: function(callbackObject, data) { - return this.apiProxy(callbackObject, data); - }, - /** * Shorthand function to perform a request against the WCF-API with overrides * for success and failure callbacks. @@ -37,43 +26,45 @@ define(['AjaxRequest', 'Core', 'ObjectMap'], function(AjaxRequest, Core, ObjectM * @param {function=} failure failure callback * @return {AjaxRequest} */ - apiProxy: function(callbackObject, data, success, failure) { + api: function(callbackObject, data, success, failure) { + if (typeof data !== 'object') data = {}; + var request = _requests.get(callbackObject); - if (request !== undefined) { - data = data || {}; + if (request === undefined) { + if (typeof callbackObject._ajaxSetup !== 'function') { + throw new TypeError("Callback object must implement at least _ajaxSetup()."); + } - if (typeof success === 'function') request.setOption('success', success); - if (typeof failure === 'function') request.setOption('failure', failure); + var options = callbackObject._ajaxSetup(); - request.setData(data || {}); - request.sendRequest(); + options.pinData = true; + options.callbackObject = callbackObject; - return request; - } - - if (typeof callbackObject._ajaxSetup !== 'function') { - throw new TypeError("Callback object must implement at least _ajaxSetup()."); + if (!options.url) options.url = 'index.php/AJAXProxy/?t=' + SECURITY_TOKEN; + + request = new AjaxRequest(options); + + _requests.set(callbackObject, request); } - var options = callbackObject._ajaxSetup(); + var oldSuccess = null; + var oldFailure = null; - options.pinData = true; - options.callbackObject = callbackObject; - - if (!options.url) options.url = 'index.php/AJAXProxy/?t=' + SECURITY_TOKEN; - - request = new AjaxRequest(options); - - if (typeof success === 'function') request.setOption('success', success); - if (typeof failure === 'function') request.setOption('failure', failure); - - if (typeof data === 'object') { - request.setData(data); + if (typeof success === 'function') { + oldSuccess = request.getOption('success'); + request.setOption('success', success); + } + if (typeof failure === 'function') { + oldFailure = request.getOption('failure'); + request.setOption('failure', failure); } + request.setData(data); request.sendRequest(); - _requests.set(callbackObject, request); + // restore callbacks + if (oldSuccess !== null) request.setOption('success', oldSuccess); + if (oldFailure !== null) request.setOption('failure', oldFailure); return request; },