Disallow adding the same feature twice
This commit is contained in:
@@ -128,15 +128,18 @@ ol.source.Vector.prototype.addFeatureInternal = function(feature) {
|
|||||||
var extent = geometry.getExtent();
|
var extent = geometry.getExtent();
|
||||||
this.rBush_.insert(extent, feature);
|
this.rBush_.insert(extent, feature);
|
||||||
} else {
|
} else {
|
||||||
this.nullGeometryFeatures_[goog.getUid(feature).toString()] = feature;
|
this.nullGeometryFeatures_[featureKey] = feature;
|
||||||
}
|
}
|
||||||
var id = feature.getId();
|
var id = feature.getId();
|
||||||
if (goog.isDef(id)) {
|
if (goog.isDef(id)) {
|
||||||
var sid = id.toString();
|
var sid = id.toString();
|
||||||
goog.asserts.assert(!(goog.isDef(this.idIndex_[sid])));
|
goog.asserts.assert(!(sid in this.idIndex_),
|
||||||
|
'Feature with same id already added to the source: ' + id);
|
||||||
this.idIndex_[sid] = feature;
|
this.idIndex_[sid] = feature;
|
||||||
} else {
|
} else {
|
||||||
this.undefIdIndex_[goog.getUid(feature).toString()] = feature;
|
goog.asserts.assert(!(featureKey in this.undefIdIndex_),
|
||||||
|
'Feature already added to the source');
|
||||||
|
this.undefIdIndex_[featureKey] = feature;
|
||||||
}
|
}
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
|
new ol.source.VectorEvent(ol.source.VectorEventType.ADDFEATURE, feature));
|
||||||
|
|||||||
@@ -363,6 +363,21 @@ describe('ol.source.Vector', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('the undefined feature id index', function() {
|
||||||
|
var source;
|
||||||
|
beforeEach(function() {
|
||||||
|
source = new ol.source.Vector();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('disallows adding the same feature twice', function() {
|
||||||
|
var feature = new ol.Feature();
|
||||||
|
source.addFeature(feature);
|
||||||
|
expect(function() {
|
||||||
|
source.addFeature(feature);
|
||||||
|
}).to.throwException();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user