From a468e8025a700ec2327a8ed9373ffebd146868a4 Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Thu, 16 Apr 2015 15:46:29 +0200 Subject: [PATCH] Modify draw interaction dispatch order When finishing drawing, this PR dispatches the `drawend` event before adding the feature to the source or collection. This change allows user code to finish configuring the feature (setting style, properties, ...). It is useful since inserting a feature, or later on modifying it, may trigger processing which only makes sense if the feature is ready. For example, ol3-cesium will recreate a 3D counterpart. --- src/ol/interaction/drawinteraction.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ol/interaction/drawinteraction.js b/src/ol/interaction/drawinteraction.js index 9942d9280a..fd564a4d90 100644 --- a/src/ol/interaction/drawinteraction.js +++ b/src/ol/interaction/drawinteraction.js @@ -517,6 +517,8 @@ ol.interaction.Draw.prototype.addToDrawing_ = function(event) { /** * Stop drawing and add the sketch feature to the target layer. + * The {@link ol.DrawEventType.DRAWEND} event is dispatched before inserting + * the feature. * @api */ ol.interaction.Draw.prototype.finishDrawing = function() { @@ -557,13 +559,16 @@ ol.interaction.Draw.prototype.finishDrawing = function() { sketchFeature.setGeometry(new ol.geom.MultiPolygon([coordinates])); } + // First dispatch event to allow full set up of feature + this.dispatchEvent(new ol.DrawEvent(ol.DrawEventType.DRAWEND, sketchFeature)); + + // Then insert feature if (!goog.isNull(this.features_)) { this.features_.push(sketchFeature); } if (!goog.isNull(this.source_)) { this.source_.addFeature(sketchFeature); } - this.dispatchEvent(new ol.DrawEvent(ol.DrawEventType.DRAWEND, sketchFeature)); };