Resolved a few issues and simplified API of `Ajax`
authorAlexander Ebert <ebert@woltlab.com>
Sat, 30 May 2015 19:18:05 +0000 (21:18 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 30 May 2015 19:18:05 +0000 (21:18 +0200)
wcfsetup/install/files/js/WoltLab/WCF/Ajax.js

index 5d6e930df8093767b727c1e1842b2754117e999b..eaf6bb8b2c27924df664c0b6c291ae23abd20aef 100644 (file)
@@ -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<string, *>=}    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;
                },