Merge pull request #9596 from jahow/fix-source-missing-event
Vector source / prevent adding features with duplicate id in the collection
This commit is contained in:
@@ -282,6 +282,9 @@ class VectorSource extends Source {
|
|||||||
* instead. A feature will not be added to the source if feature with
|
* instead. A feature will not be added to the source if feature with
|
||||||
* the same id is already there. The reason for this behavior is to avoid
|
* the same id is already there. The reason for this behavior is to avoid
|
||||||
* feature duplication when using bbox or tile loading strategies.
|
* feature duplication when using bbox or tile loading strategies.
|
||||||
|
* Note: this also applies if an {@link module:ol/Collection} is used for features,
|
||||||
|
* meaning that if a feature with a duplicate id is added in the collection, it will
|
||||||
|
* be removed from it right away.
|
||||||
* @param {import("../Feature.js").default} feature Feature to add.
|
* @param {import("../Feature.js").default} feature Feature to add.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -300,6 +303,9 @@ class VectorSource extends Source {
|
|||||||
const featureKey = getUid(feature);
|
const featureKey = getUid(feature);
|
||||||
|
|
||||||
if (!this.addToIndex_(featureKey, feature)) {
|
if (!this.addToIndex_(featureKey, feature)) {
|
||||||
|
if (this.featuresCollection_) {
|
||||||
|
this.featuresCollection_.remove(feature);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -719,6 +719,19 @@ describe('ol.source.Vector', function() {
|
|||||||
expect(source.getFeatures().length).to.be(0);
|
expect(source.getFeatures().length).to.be(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('prevents adding two features with a duplicate id in the collection', function() {
|
||||||
|
source = new VectorSource({
|
||||||
|
features: new Collection()
|
||||||
|
});
|
||||||
|
const feature1 = new Feature();
|
||||||
|
feature1.setId('1');
|
||||||
|
const feature2 = new Feature();
|
||||||
|
feature2.setId('1');
|
||||||
|
const collection = source.getFeaturesCollection();
|
||||||
|
collection.push(feature1);
|
||||||
|
collection.push(feature2);
|
||||||
|
expect(collection.getLength()).to.be(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with a collection of features plus spatial index', function() {
|
describe('with a collection of features plus spatial index', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user