diff --git a/src/ol/interaction/drawinteraction.js b/src/ol/interaction/drawinteraction.js index da5e210aad..87da838d99 100644 --- a/src/ol/interaction/drawinteraction.js +++ b/src/ol/interaction/drawinteraction.js @@ -678,7 +678,7 @@ ol.interaction.Draw.prototype.abortDrawing_ = function() { this.sketchFeature_ = null; this.sketchPoint_ = null; this.sketchLine_ = null; - this.overlay_.getSource().clear(); + this.overlay_.getSource().clear(true); } return sketchFeature; }; @@ -706,7 +706,7 @@ ol.interaction.Draw.prototype.updateSketchFeatures_ = function() { sketchFeatures.push(this.sketchPoint_); } var overlaySource = this.overlay_.getSource(); - overlaySource.clear(); + overlaySource.clear(true); overlaySource.addFeatures(sketchFeatures); }; diff --git a/src/ol/source/vectorsource.js b/src/ol/source/vectorsource.js index 60976af89b..96a3c5f4b1 100644 --- a/src/ol/source/vectorsource.js +++ b/src/ol/source/vectorsource.js @@ -372,26 +372,30 @@ ol.source.Vector.prototype.bindFeaturesCollection_ = function(collection) { */ ol.source.Vector.prototype.clear = function(opt_fast) { if (opt_fast) { - for (var featureId in this.featureChangeKeys_) { - var keys = this.featureChangeKeys_[featureId]; - goog.array.forEach(keys, goog.events.unlistenByKey); + if (goog.isNull(this.featuresCollection_)) { + for (var featureId in this.featureChangeKeys_) { + var keys = this.featureChangeKeys_[featureId]; + goog.array.forEach(keys, goog.events.unlistenByKey); + } + this.featureChangeKeys_ = {}; + this.idIndex_ = {}; + this.undefIdIndex_ = {}; + } else { + this.featuresCollection_.clear(); } - this.featureChangeKeys_ = {}; - this.idIndex_ = {}; - this.undefIdIndex_ = {}; } else { var rmFeatureInternal = this.removeFeatureInternal; if (!goog.isNull(this.featuresRtree_)) { this.featuresRtree_.forEach(rmFeatureInternal, this); goog.object.forEach(this.nullGeometryFeatures_, rmFeatureInternal, this); } - goog.asserts.assert(goog.object.isEmpty(this.featureChangeKeys_), - 'featureChangeKeys is an empty object now'); - goog.asserts.assert(goog.object.isEmpty(this.idIndex_), - 'idIndex is an empty object now'); - goog.asserts.assert(goog.object.isEmpty(this.undefIdIndex_), - 'undefIdIndex is an empty object now'); } + goog.asserts.assert(goog.object.isEmpty(this.featureChangeKeys_), + 'featureChangeKeys is an empty object now'); + goog.asserts.assert(goog.object.isEmpty(this.idIndex_), + 'idIndex is an empty object now'); + goog.asserts.assert(goog.object.isEmpty(this.undefIdIndex_), + 'undefIdIndex is an empty object now'); if (!goog.isNull(this.featuresRtree_)) { this.featuresRtree_.clear(); diff --git a/test/spec/ol/source/vectorsource.test.js b/test/spec/ol/source/vectorsource.test.js index 74bad15d13..8b7c6fb6e0 100644 --- a/test/spec/ol/source/vectorsource.test.js +++ b/test/spec/ol/source/vectorsource.test.js @@ -459,16 +459,24 @@ describe('ol.source.Vector', function() { expect(source.getFeaturesCollection()).to.equal(collection); }); - it('keeps the collection in sync with the source\'s features', function() { + it('adding/removing features keeps the collection in sync', function() { var feature = new ol.Feature(); source.addFeature(feature); expect(collection.getLength()).to.be(1); source.removeFeature(feature); expect(collection.getLength()).to.be(0); + }); + + it('#clear() features keeps the collection in sync', function() { + var feature = new ol.Feature(); source.addFeatures([feature]); expect(collection.getLength()).to.be(1); source.clear(); expect(collection.getLength()).to.be(0); + source.addFeatures([feature]); + expect(collection.getLength()).to.be(1); + source.clear(true); + expect(collection.getLength()).to.be(0); }); it('keeps the source\'s features in sync with the collection', function() {