OpenLayers OpenLayers

Changeset 7091

Show
Ignore:
Timestamp:
05/07/08 17:59:52 (2 months ago)
Author:
tschaub
Message:

add optional success and failure callbacks

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/tschaub/request/lib/OpenLayers/Request.js

    r6366 r7091  
    2626        data: null, 
    2727        callback: function() {}, 
     28        success: null, 
     29        failure: null, 
    2830        scope: null 
    2931    }, 
     
    3739     * config - {Object} Object containing properties for configuring the 
    3840     *     request.  Allowed configuration properties are described below. 
    39      * config.method - {String} One of GET, POST, PUT, DELETE, HEAD, or 
     41     * 
     42     * Allowed config properties: 
     43     * method - {String} One of GET, POST, PUT, DELETE, HEAD, or 
    4044     *     OPTIONS.  Default is GET. 
    41      * config.url - {String} URL for the request. 
    42      * config.async - {Boolean} Open an asynchronous request.  Default is true. 
    43      * config.user - {String} User for relevant authentication scheme.  Set 
     45     * url - {String} URL for the request. 
     46     * async - {Boolean} Open an asynchronous request.  Default is true. 
     47     * user - {String} User for relevant authentication scheme.  Set 
    4448     *     to null to clear current user. 
    45      * config.password - {String} Password for relevant authentication scheme. 
     49     * password - {String} Password for relevant authentication scheme. 
    4650     *     Set to null to clear current password. 
    47      * config.proxy - {String} Optional proxy.  Defaults to 
     51     * proxy - {String} Optional proxy.  Defaults to 
    4852     *     <OpenLayers.ProxyHost>. 
    49      * config.params - {Object} Any key:value pairs to be appended to the 
     53     * params - {Object} Any key:value pairs to be appended to the 
    5054     *     url as a query string.  Assumes url doesn't already include a query 
    5155     *     string or hash.  Parameter values that are arrays will be 
    5256     *     concatenated with a comma (note that this goes against form-encoding) 
    5357     *     as is done with <OpenLayers.Util.getParameterString>. 
    54      * config.headers - {Object} Object with header:value pairs to be set on 
     58     * headers - {Object} Object with header:value pairs to be set on 
    5559     *     the request. 
    56      * config.data - {Object} Any data to send with the request. 
    57      * config.callback - {Function} Function to call when request is done. 
     60     * data - {Object} Any data to send with the request. 
     61     * callback - {Function} Function to call when request is done. 
    5862     *     To determine if the request failed, check request.status (200 
    5963     *     indicates success). 
    60      * config.scope - {Object} If callback is a public method on some object, 
     64     * success - {Function} Optional function to call if request status is in 
     65     *     the 200s.  This will be called in addition to callback above and 
     66     *     would typically only be used as an alternative. 
     67     * failure - {Function} Optional function to call if request status is not 
     68     *     in the 200s.  This will be called in addition to callback above and 
     69     *     would typically only be used as an alternative. 
     70     * scope - {Object} If callback is a public method on some object, 
    6171     *     set the scope to that object. 
    6272     * 
     
    8999        } 
    90100 
    91         // bind callback to readyState 4 (done) 
     101        // bind callbacks to readyState 4 (done) 
    92102        var complete = (config.scope) ? 
    93103            OpenLayers.Function.bind(config.callback, config.scope) : 
    94104            config.callback; 
     105         
     106        // optional success callback 
     107        var success; 
     108        if(config.success) { 
     109            success = (config.scope) ? 
     110                OpenLayers.Function.bind(config.success, config.scope) : 
     111                config.success; 
     112        } 
     113 
     114        // optional failure callback 
     115        var failure; 
     116        if(config.failure) { 
     117            failure = (config.scope) ? 
     118                OpenLayers.Function.bind(config.failure, config.scope) : 
     119                config.failure; 
     120        } 
     121          
    95122        request.onreadystatechange = function() { 
    96123            if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) { 
    97124                complete(request); 
     125                if(success && request.status >= 200 && request.status < 300) { 
     126                    success(request); 
     127                } 
     128                if(failure && (request.status < 200 || request.status >= 300)) { 
     129                    failure(request); 
     130                } 
    98131            } 
    99132        } 
  • sandbox/tschaub/request/tests/Request.html

    r6360 r7091  
    2222        setup(); 
    2323 
    24         t.plan(14); 
     24        t.plan(18); 
    2525        var request, config; 
    2626        var proto = OpenLayers.Request.XMLHttpRequest.prototype; 
    27         var issue = OpenLayers.Request.issue; 
     27        var issue = OpenLayers.Function.bind(OpenLayers.Request.issue, 
     28                                             OpenLayers.Request); 
    2829 
    2930        // test that issue returns a new XMLHttpRequest - 1 test 
     
    104105        request = issue(config); 
    105106        proto.send = _send; 
     107         
     108        // test that optional success callback is only called with 200s and 
     109        // failure is only called with non-200s 
     110        var _send = proto.send; 
     111        proto.send = function() {}; 
     112         
     113        config = { 
     114            success: function(req) { 
     115                t.ok(req.status >= 200 && req.status < 300, 
     116                     "success callback called with " + req.status + " status"); 
     117            }, 
     118            failure: function(req) { 
     119                t.ok(req.status < 200 || req.status >= 300, 
     120                     "failure callback called with " + req.status + " status"); 
     121            } 
     122        }; 
     123        request = issue(config); 
     124        request.readyState = 4; 
     125         
     126        // mock up status 200 (1 test) 
     127        request.status = 200; 
     128        request.onreadystatechange(); 
     129         
     130        // mock up status 299 (1 test) 
     131        request.status = 299; 
     132        request.onreadystatechange(); 
     133         
     134        // mock up status 100 (1 test) 
     135        request.status = 100; 
     136        request.onreadystatechange(); 
     137 
     138        // mock up status 300 (1 test) 
     139        request.status = 300; 
     140        request.onreadystatechange(); 
     141 
     142        proto.send = _send; 
    106143 
    107144        teardown();