Improve XHR and ol.net.jsonp result/failure handling

This commit is contained in:
Andreas Hocevar
2016-02-02 17:53:49 +01:00
parent e48ab95735
commit 78f44dcc8a
4 changed files with 27 additions and 8 deletions

View File

@@ -75,7 +75,7 @@ ol.featureloader.loadFeaturesXhr = function(url, format, success, failure) {
* @private * @private
*/ */
xhr.onload = function(event) { xhr.onload = function(event) {
if (xhr.status < 400) { if (xhr.status >= 200 && xhr.status < 300) {
var type = format.getType(); var type = format.getType();
/** @type {Document|Node|Object|string|undefined} */ /** @type {Document|Node|Object|string|undefined} */
var source; var source;

View File

@@ -14,19 +14,23 @@ goog.provide('ol.net');
*/ */
ol.net.jsonp = function(url, callback, opt_errback, opt_callbackParam) { ol.net.jsonp = function(url, callback, opt_errback, opt_callbackParam) {
var script = goog.global.document.createElement('script'); var script = goog.global.document.createElement('script');
script.async = true;
var key = 'olc_' + goog.getUid(callback); var key = 'olc_' + goog.getUid(callback);
function cleanup() {
delete goog.global[key];
script.parentNode.removeChild(script);
}
script.async = true;
script.src = url + (url.indexOf('?') == -1 ? '?' : '&') + script.src = url + (url.indexOf('?') == -1 ? '?' : '&') +
(opt_callbackParam || 'callback') + '=' + key; (opt_callbackParam || 'callback') + '=' + key;
var timer = goog.global.setTimeout(function() { var timer = goog.global.setTimeout(function() {
delete goog.global[key]; cleanup();
if (opt_errback) { if (opt_errback) {
opt_errback(); opt_errback();
} }
}, 10000); }, 10000);
goog.global[key] = function(data) { goog.global[key] = function(data) {
goog.global.clearTimeout(timer); goog.global.clearTimeout(timer);
delete goog.global[key]; cleanup();
callback(data); callback(data);
}; };
goog.global.document.getElementsByTagName('head')[0].appendChild(script); goog.global.document.getElementsByTagName('head')[0].appendChild(script);

View File

@@ -46,7 +46,7 @@ ol.source.TileJSON = function(options) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('GET', options.url, true); xhr.open('GET', options.url, true);
xhr.onload = function(e) { xhr.onload = function(e) {
if (xhr.status < 400) { if (xhr.status >= 200 && xhr.status < 300) {
var response = /** @type {TileJSON} */(JSON.parse(xhr.responseText)); var response = /** @type {TileJSON} */(JSON.parse(xhr.responseText));
this.handleTileJSONResponse(response); this.handleTileJSONResponse(response);
} else { } else {

View File

@@ -6,12 +6,15 @@ describe('ol.net', function() {
describe('jsonp()', function() { describe('jsonp()', function() {
var head = goog.global.document.getElementsByTagName('head')[0]; var head = goog.global.document.getElementsByTagName('head')[0];
var origAppendChild = head.appendChild; var origAppendChild = head.appendChild;
var origCreateElement = document.createElement;
var origSetTimeout = goog.global.setTimeout; var origSetTimeout = goog.global.setTimeout;
var key; var key, removeChild;
function createCallback(url, done) { function createCallback(url, done) {
removeChild = sinon.spy();
var callback = function(data) { var callback = function(data) {
expect(data).to.be(url + key); expect(data).to.be(url + key);
expect(removeChild.called).to.be(true);
done(); done();
}; };
key = 'olc_' + goog.getUid(callback); key = 'olc_' + goog.getUid(callback);
@@ -19,9 +22,15 @@ describe('ol.net', function() {
} }
beforeEach(function() { beforeEach(function() {
document.createElement = function() {
return {}
};
head.appendChild = function(element) { head.appendChild = function(element) {
element.parentNode = {
removeChild: removeChild
};
origSetTimeout(function() { origSetTimeout(function() {
goog.global[key](element.getAttribute('src')); goog.global[key](element.src);
}, 0); }, 0);
}; };
goog.global.setTimeout = function(fn, time) { goog.global.setTimeout = function(fn, time) {
@@ -30,6 +39,7 @@ describe('ol.net', function() {
}); });
afterEach(function() { afterEach(function() {
document.createElement = origCreateElement;
head.appendChild = origAppendChild; head.appendChild = origAppendChild;
goog.global.setTimeout = origSetTimeout; goog.global.setTimeout = origSetTimeout;
}); });
@@ -42,12 +52,17 @@ describe('ol.net', function() {
ol.net.jsonp('http://foo/bar?baz', callback); ol.net.jsonp('http://foo/bar?baz', callback);
}); });
it('calls errback when jsonp is not executed, cleans up', function(done) { it('calls errback when jsonp is not executed, cleans up', function(done) {
head.appendChild = function() {}; head.appendChild = function(element) {
element.parentNode = {
removeChild: removeChild
};
};
function callback() { function callback() {
expect.fail(); expect.fail();
} }
function errback() { function errback() {
expect(goog.global[key]).to.be(undefined); expect(goog.global[key]).to.be(undefined);
expect(removeChild.called).to.be(true);
done(); done();
} }
ol.net.jsonp('foo', callback, errback); ol.net.jsonp('foo', callback, errback);