Changeset 7091
- Timestamp:
- 05/07/08 17:59:52 (2 months ago)
- Files:
-
- sandbox/tschaub/request/lib/OpenLayers/Request.js (modified) (3 diffs)
- sandbox/tschaub/request/tests/Request.html (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/tschaub/request/lib/OpenLayers/Request.js
r6366 r7091 26 26 data: null, 27 27 callback: function() {}, 28 success: null, 29 failure: null, 28 30 scope: null 29 31 }, … … 37 39 * config - {Object} Object containing properties for configuring the 38 40 * 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 40 44 * 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. Set45 * 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 44 48 * to null to clear current user. 45 * config.password - {String} Password for relevant authentication scheme.49 * password - {String} Password for relevant authentication scheme. 46 50 * Set to null to clear current password. 47 * config.proxy - {String} Optional proxy. Defaults to51 * proxy - {String} Optional proxy. Defaults to 48 52 * <OpenLayers.ProxyHost>. 49 * config.params - {Object} Any key:value pairs to be appended to the53 * params - {Object} Any key:value pairs to be appended to the 50 54 * url as a query string. Assumes url doesn't already include a query 51 55 * string or hash. Parameter values that are arrays will be 52 56 * concatenated with a comma (note that this goes against form-encoding) 53 57 * as is done with <OpenLayers.Util.getParameterString>. 54 * config.headers - {Object} Object with header:value pairs to be set on58 * headers - {Object} Object with header:value pairs to be set on 55 59 * the request. 56 * config.data - {Object} Any data to send with the request.57 * c onfig.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. 58 62 * To determine if the request failed, check request.status (200 59 63 * 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, 61 71 * set the scope to that object. 62 72 * … … 89 99 } 90 100 91 // bind callback to readyState 4 (done)101 // bind callbacks to readyState 4 (done) 92 102 var complete = (config.scope) ? 93 103 OpenLayers.Function.bind(config.callback, config.scope) : 94 104 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 95 122 request.onreadystatechange = function() { 96 123 if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) { 97 124 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 } 98 131 } 99 132 } sandbox/tschaub/request/tests/Request.html
r6360 r7091 22 22 setup(); 23 23 24 t.plan(1 4);24 t.plan(18); 25 25 var request, config; 26 26 var proto = OpenLayers.Request.XMLHttpRequest.prototype; 27 var issue = OpenLayers.Request.issue; 27 var issue = OpenLayers.Function.bind(OpenLayers.Request.issue, 28 OpenLayers.Request); 28 29 29 30 // test that issue returns a new XMLHttpRequest - 1 test … … 104 105 request = issue(config); 105 106 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; 106 143 107 144 teardown();
