Make loadFeaturesFromUrl accept an error callback

This commit is contained in:
Éric Lemoine
2014-11-24 14:42:21 +01:00
parent 32d5300682
commit 8ba830f91f
3 changed files with 31 additions and 18 deletions

View File

@@ -48,12 +48,14 @@ goog.inherits(ol.source.FormatVector, ol.source.Vector);
/** /**
* @param {goog.Uri|string} url URL. * @param {goog.Uri|string} url URL.
* @param {function(this: T, Array.<ol.Feature>)} callback Callback. * @param {function(this: T, Array.<ol.Feature>)} success Success Callback.
* @param {T} thisArg Value to use as `this` when executing `callback`. * @param {function(this: T)} error Error callback.
* @param {T} thisArg Value to use as `this` when executing `success` or
* `error`.
* @template T * @template T
*/ */
ol.source.FormatVector.prototype.loadFeaturesFromURL = ol.source.FormatVector.prototype.loadFeaturesFromURL =
function(url, callback, thisArg) { function(url, success, error, thisArg) {
var xhrIo = new goog.net.XhrIo(); var xhrIo = new goog.net.XhrIo();
var type = this.format.getType(); var type = this.format.getType();
var responseType; var responseType;
@@ -97,13 +99,13 @@ ol.source.FormatVector.prototype.loadFeaturesFromURL =
goog.asserts.fail(); goog.asserts.fail();
} }
if (goog.isDefAndNotNull(source)) { if (goog.isDefAndNotNull(source)) {
callback.call(thisArg, this.readFeatures(source)); success.call(thisArg, this.readFeatures(source));
} else { } else {
this.setState(ol.source.State.ERROR); this.setState(ol.source.State.ERROR);
goog.asserts.fail(); goog.asserts.fail();
} }
} else { } else {
this.setState(ol.source.State.ERROR); error.call(thisArg);
} }
goog.dispose(xhrIo); goog.dispose(xhrIo);
}, false, this); }, false, this);

View File

@@ -48,13 +48,15 @@ ol.source.StaticVector = function(options) {
if (goog.isDef(options.url) || goog.isDef(options.urls)) { if (goog.isDef(options.url) || goog.isDef(options.urls)) {
this.setState(ol.source.State.LOADING); this.setState(ol.source.State.LOADING);
if (goog.isDef(options.url)) { if (goog.isDef(options.url)) {
this.loadFeaturesFromURL(options.url, this.onFeaturesLoaded_, this); this.loadFeaturesFromURL(options.url,
this.onFeaturesLoadedSuccess_, this.onFeaturesLoadedError_, this);
} }
if (goog.isDef(options.urls)) { if (goog.isDef(options.urls)) {
var urls = options.urls; var urls = options.urls;
var i, ii; var i, ii;
for (i = 0, ii = urls.length; i < ii; ++i) { for (i = 0, ii = urls.length; i < ii; ++i) {
this.loadFeaturesFromURL(urls[i], this.onFeaturesLoaded_, this); this.loadFeaturesFromURL(urls[i],
this.onFeaturesLoadedSuccess_, this.onFeaturesLoadedError_, this);
} }
} }
} }
@@ -63,11 +65,19 @@ ol.source.StaticVector = function(options) {
goog.inherits(ol.source.StaticVector, ol.source.FormatVector); goog.inherits(ol.source.StaticVector, ol.source.FormatVector);
/**
* @private
*/
ol.source.StaticVector.prototype.onFeaturesLoadedError_ = function() {
this.setState(ol.source.State.ERROR);
};
/** /**
* @param {Array.<ol.Feature>} features Features. * @param {Array.<ol.Feature>} features Features.
* @private * @private
*/ */
ol.source.StaticVector.prototype.onFeaturesLoaded_ = function(features) { ol.source.StaticVector.prototype.onFeaturesLoadedSuccess_ = function(features) {
this.addFeaturesInternal(features); this.addFeaturesInternal(features);
this.setState(ol.source.State.READY); this.setState(ol.source.State.READY);
}; };

View File

@@ -184,6 +184,15 @@ ol.source.TileVector.prototype.loadFeatures =
var tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z); var tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z);
var tileCoord = [z, 0, 0]; var tileCoord = [z, 0, 0];
var x, y; var x, y;
/**
* @param {string} tileKey Tile key.
* @param {Array.<ol.Feature>} features Features.
* @this {ol.source.TileVector}
*/
function success(tileKey, features) {
tiles[tileKey] = features;
this.setState(ol.source.State.READY);
}
for (x = tileRange.minX; x <= tileRange.maxX; ++x) { for (x = tileRange.minX; x <= tileRange.maxX; ++x) {
for (y = tileRange.minY; y <= tileRange.maxY; ++y) { for (y = tileRange.minY; y <= tileRange.maxY; ++y) {
var tileKey = this.getTileKeyZXY_(z, x, y); var tileKey = this.getTileKeyZXY_(z, x, y);
@@ -195,16 +204,8 @@ ol.source.TileVector.prototype.loadFeatures =
var url = tileUrlFunction(tileCoord, 1, projection); var url = tileUrlFunction(tileCoord, 1, projection);
if (goog.isDef(url)) { if (goog.isDef(url)) {
tiles[tileKey] = []; tiles[tileKey] = [];
this.loadFeaturesFromURL(url, goog.partial( this.loadFeaturesFromURL(url, goog.partial(success, tileKey),
/** goog.nullFunction, this);
* @param {string} tileKey Tile key.
* @param {Array.<ol.Feature>} features Features.
* @this {ol.source.TileVector}
*/
function(tileKey, features) {
tiles[tileKey] = features;
this.setState(ol.source.State.READY);
}, tileKey), this);
} }
} }
} }