Modify ol.featureloader (and consequently ol.source.Vector) to accept a function as a url.

This commit is contained in:
Alvin Lindstam
2015-08-09 22:09:31 +02:00
parent 4045e06aa9
commit e41ba12445
3 changed files with 73 additions and 10 deletions

View File

@@ -1,4 +1,5 @@
goog.provide('ol.FeatureLoader');
goog.provide('ol.FeatureUrlFunction');
goog.provide('ol.featureloader');
goog.require('goog.asserts');
@@ -11,6 +12,16 @@ goog.require('ol.xml');
/**
* {@link ol.source.Vector} sources use a function of this type to load
* features.
*
* This function takes an {@link ol.Extent} representing the area to be loaded,
* a `{number}` representing the resolution (map units per pixel) and an
* {@link ol.proj.Projection} for the projection as arguments. `this` within
* the function is bound to the {@link ol.source.Vector} it's called from.
*
* The function is responsible for loading the features and adding them to the
* source.
* @api
* @typedef {function(this:ol.source.Vector, ol.Extent, number,
* ol.proj.Projection)}
@@ -19,7 +30,21 @@ ol.FeatureLoader;
/**
* @param {string} url Feature URL service.
* {@link ol.source.Vector} sources use a function of this type to get the url
* to load features from.
*
* This function takes an {@link ol.Extent} representing the area to be loaded,
* a `{number}` representing the resolution (map units per pixel) and an
* {@link ol.proj.Projection} for the projection as arguments and returns a
* `{string}` representing the URL.
* @api
* @typedef {function(ol.Extent, number, ol.proj.Projection) : string}
*/
ol.FeatureUrlFunction;
/**
* @param {string|ol.FeatureUrlFunction} url Feature URL service.
* @param {ol.format.Feature} format Feature format.
* @param {function(this:ol.source.Vector, Array.<ol.Feature>)} success
* Function called with the loaded features. Called with the vector
@@ -77,7 +102,12 @@ ol.featureloader.loadFeaturesXhr = function(url, format, success) {
}
goog.dispose(xhrIo);
}, false, this);
xhrIo.send(url);
if (goog.isFunction(url)) {
xhrIo.send(url(extent, resolution, projection));
} else {
xhrIo.send(url);
}
});
};
@@ -86,7 +116,7 @@ ol.featureloader.loadFeaturesXhr = function(url, format, success) {
* Create an XHR feature loader for a `url` and `format`. The feature loader
* loads features (with XHR), parses the features, and adds them to the
* vector source.
* @param {string} url Feature URL service.
* @param {string|ol.FeatureUrlFunction} url Feature URL service.
* @param {ol.format.Feature} format Feature format.
* @return {ol.FeatureLoader} The feature loader.
* @api