diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 9835c78b69..7f87255f4b 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -79,9 +79,9 @@ "Rico/Corner.js", "Rico/Color.js", "OpenLayers/Ajax.js", + "OpenLayers/Events.js", "OpenLayers/Request.js", "OpenLayers/Request/XMLHttpRequest.js", - "OpenLayers/Events.js", "OpenLayers/Projection.js", "OpenLayers/Map.js", "OpenLayers/Layer.js", diff --git a/lib/OpenLayers/Request.js b/lib/OpenLayers/Request.js index 1c2b2c9fff..c5e985e8d0 100644 --- a/lib/OpenLayers/Request.js +++ b/lib/OpenLayers/Request.js @@ -2,6 +2,10 @@ * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the * full text of the license. */ +/** + * @requires OpenLayers/Events.js + */ + /** * Namespace: OpenLayers.Request * The OpenLayers.Request namespace contains convenience methods for working @@ -30,6 +34,25 @@ OpenLayers.Request = { scope: null }, + /** + * APIProperty: events + * {} An events object that handles all + * events on the {} object. + * + * All event listeners will receive an event object with three properties: + * request - {} The request object. + * config - {Object} The config object sent to the specific request method. + * requestUrl - {String} The request url. + * + * Supported event types: + * complete - Triggered when we have a response from the request, if a + * listener returns false, no further response processing will take + * place. + * success - Triggered when the HTTP response has a success code (200-299). + * failure - Triggered when the HTTP response does not have a success code. + */ + events: new OpenLayers.Events(this, null, ["complete", "success", "failure"]), + /** * APIMethod: issue * Create a new XMLHttpRequest object, open it, set any headers, bind @@ -134,17 +157,35 @@ OpenLayers.Request = { OpenLayers.Function.bind(config.failure, config.scope) : config.failure; } + + var events = this.events; request.onreadystatechange = function() { if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) { - complete(request); - if(success && (!request.status || - (request.status >= 200 && request.status < 300))) { - success(request); - } - if(failure && (request.status && - (request.status < 200 || request.status >= 300))) { - failure(request); + var proceed = events.triggerEvent( + "complete", + {request: request, config: config, requestUrl: url} + ); + if(proceed !== false) { + complete(request); + if (!request.status || (request.status >= 200 && request.status < 300)) { + events.triggerEvent( + "success", + {request: request, config: config, requestUrl: url} + ); + if(success) { + success(request); + } + } + if(request.status && (request.status < 200 || request.status >= 300)) { + events.triggerEvent( + "failure", + {request: request, config: config, requestUrl: url} + ); + if(failure) { + failure(request); + } + } } } }; diff --git a/tests/Request.html b/tests/Request.html index 44349d333f..2fffbb2a39 100644 --- a/tests/Request.html +++ b/tests/Request.html @@ -247,6 +247,77 @@ OpenLayers.Request.OPTIONS(); OpenLayers.Request.issue = _issue; } + + function test_events_success(t) { + + t.plan(5); + + var events = []; + function listener(event) { + events.push(event); + } + + // set up event listeners + OpenLayers.Request.events.on({ + complete: listener, + success: listener, + failure: listener + }); + + // issue a request that succeeds + OpenLayers.Request.GET({ + url: ".", params: {bar: "baz"}, async: false + }); + t.eq(events.length, 2, "two events logged"); + t.eq(events[0].type, "complete", "first event is complete"); + t.eq(events[1].type, "success", "second event is success"); + t.ok(events[1].config, "success listener sent config"); + t.eq(events[1].requestUrl, ".?bar=baz", "success listener sent config.url"); + + // remove event listeners + OpenLayers.Request.events.un({ + complete: listener, + success: listener, + failure: listener + }); + + } + + function test_events_failure(t) { + + t.plan(5); + + var events = []; + function listener(event) { + events.push(event); + } + + // set up event listeners + OpenLayers.Request.events.on({ + complete: listener, + success: listener, + failure: listener + }); + + // issue a request that succeeds + OpenLayers.Request.GET({ + url: "foo", params: {bar: "baz"}, async: false + }); + t.eq(events.length, 2, "two events logged"); + t.eq(events[0].type, "complete", "first event is complete"); + t.eq(events[1].type, "failure", "second event is failure"); + t.ok(events[1].config, "failure listener sent config"); + t.eq(events[1].requestUrl, "foo?bar=baz", "failure listener sent requestUrl"); + + // remove event listeners + OpenLayers.Request.events.un({ + complete: listener, + success: listener, + failure: listener + }); + + } +