Draw Toolbar onDrawEnd firing twice

1951
2
Jump to solution
09-16-2013 09:37 AM
BrianBeck
New Contributor III
I have a simple editing application with the following code:

startEditing : function(template) {      var drawingTool = template.template.drawingTool;      switch(drawingTool) {         case FeatureTemplate.TOOL_POINT:             drawingTool = Draw.POINT;             break;         }      on(this.drawingToolbar, "draw-end", lang.hitch(this, this.createFeature, template));      this.drawingToolbar.activate(drawingTool); }


and

createFeature : function(template, evt) {      var featureLayer = template.featureLayer;     template = template.template;      var prototype = template.prototype;      var geometry = evt.geometry;      var graphic = new Graphic(prototype.toJson());     graphic.setGeometry(geometry);      this.initAttributes(graphic, featureLayer).then(function() {          var features = [graphic];          featureLayer.applyEdits(features).then(function(addResults) {             var objectIds = array.map(addResults, function(addResult) {                 return addResult.objectId;             });              var q = new Query();             q.objectIds = objectIds;              featureLayer.selectFeatures(q).then(function(features) {                 main.openForm(features);             });         });      });  }


Using a point tool with the drawing toolbar, I am finding that when I click a point on the map, the createFeature function is called twice resulting in 2 features being created with the same geometry.  How can I stop the draw toolbar from creating 2 features?
0 Kudos
1 Solution

Accepted Solutions
JasonZou
Occasional Contributor III
Move below statement out of startEditing function. I don't know what will trigger to call startEditing. But every time it's invoked, onDrawEnd event handler will be defined one more time, which makes createFeature invoked one more time.

I would recommend to define the onDrawEnd event handler in the same function where drawingToolbar instance is created so to ensure it's been defined once only.

on(this.drawingToolbar, "draw-end", lang.hitch(this, this.createFeature, template));

View solution in original post

0 Kudos
2 Replies
JasonZou
Occasional Contributor III
Move below statement out of startEditing function. I don't know what will trigger to call startEditing. But every time it's invoked, onDrawEnd event handler will be defined one more time, which makes createFeature invoked one more time.

I would recommend to define the onDrawEnd event handler in the same function where drawingToolbar instance is created so to ensure it's been defined once only.

on(this.drawingToolbar, "draw-end", lang.hitch(this, this.createFeature, template));
0 Kudos
BrianBeck
New Contributor III
Move below statement out of startEditing function. I don't know what will trigger to call startEditing. But every time it's invoked, onDrawEnd event handler will be defined one more time, which makes createFeature invoked one more time.

I would recommend to define the onDrawEnd event handler in the same function where drawingToolbar instance is created so to ensure it's been defined once only.

on(this.drawingToolbar, "draw-end", lang.hitch(this, this.createFeature, template));



Thank you Jason.  I also just realized that I was creating the onDrawEnd handler multiple times.  The startEditing function is called each time a selection is made in the templatePicker.  That means that if the user selects another template, I get a duplicate handler.  In my case, when I was testing I was selecting a template and adding a new feature.  Then I unselected the template which calls the stopEditing function.  My stopEditing function simply deactivates the drawingToolbar.  I have added code to save the onDrawEnd handler to a variable and then the stopEditing function can remove that handler when it is called.
0 Kudos