When using a Feature Layer, applyedits fires multiple times

1087
4
Jump to solution
10-17-2013 11:07 AM
BethManghi
New Contributor III
I'm using a feature layer that is editable and when a new feature is added, I'd like to prompt the user with a dijit dialog box to confirm they want to add the new feature to the database.  This works fine when only one feature is added during a editing session, but if more than one is created, the applyedits function is fired multiple times and all the features in the editing session are duplicated with each new feature.  I used Firebug to see the multiple POST events are firing when I think there should only be one call to applyedits.  The duplicated features will also have an ObjectID this is out of sequence with the others.

I've tried omitting the dialog box and it work as expected.  I've tried clearing the default map graphics layer and the feature layer with no success.  Where are they hiding!  I've also modified an editing example from Esri and ran into the same problem.  Any advice is appreciated!
0 Kudos
1 Solution

Accepted Solutions
JasonZou
Occasional Contributor III
I can see one problem. You defined the event handlers for yesAddButton and noAddButton inside the callback function of draw-end event. So every time when you draw a graphic, yesAddButton and noAddButton event handlers will be defined one time. It will end up that if you draw two graphics, applyEdits will be triggered twice; three graphics, call applyEdits three times, and so on.

Move below code outside of dojo.connect(drawToolbar, "onDrawEnd", function(geometry) {...}); block. It should clear the issue.
  dojo.connect(yesAddButton, "onclick", function () {     dijit.byId("addFeatureDialog").hide();     //when features are added - add them to the undo manager               selectedTemplate.featureLayer.applyEdits([newGraphic], null, null, function() {       var operation = new esri.dijit.editing.Add({         featureLayer: selectedTemplate.featureLayer,         addedGraphics: [newGraphic]       });        undoManager.add(operation);        checkUI();     });         });   dojo.connect(noAddButton, "onclick", function () {     dijit.byId("addFeatureDialog").hide();   });

View solution in original post

0 Kudos
4 Replies
JasonZou
Occasional Contributor III
More than likely, applyEdits is triggered multiple times. Without looking at the code, it's hard to tell how multiple triggers are defined. Can you post your code? It would be great if you can post your code in jsFiddle.
0 Kudos
BethManghi
New Contributor III
Here's what I added to the Editor with undo redo example from Esri

The dijit dialog:


<div id="addFeatureDialog" data-dojo-type="dijit.Dialog" style="display: none;" title="Add Feature to Database?">
  <p>Do you want to add this location to the database?</p>
  <div id="yesNoDiv">
    <div id="yesAddButton" data-dojo-type="dijit.form.Button" >Yes</div>
    <div id="noAddButton" data-dojo-type="dijit.form.Button" >No</div>
  </div> <!-- end #yesNoDiv -->
</div> <!-- end #addFeatureDialog -->



Then displayed the dialog just before applyEdits is called (at line 22 in the example):

//once the geometry is drawn - call applyEdits to update the feature service with the new geometry
dojo.connect(drawToolbar, "onDrawEnd", function(geometry) {
  drawToolbar.deactivate();
  var dialog = dijit.byId("addFeatureDialog");
  var newAttributes = dojo.mixin({},selectedTemplate.template.prototype.attributes);
  var newGraphic = new esri.Graphic(geometry,null,newAttributes);
          
  dialog.style.display = "block";
  dialog.closeButtonNode.style.display = "none";
  dialog.show();
     
  dojo.connect(yesAddButton, "onclick", function () {
    dijit.byId("addFeatureDialog").hide();
    //when features are added - add them to the undo manager          
    selectedTemplate.featureLayer.applyEdits([newGraphic], null, null, function() {
      var operation = new esri.dijit.editing.Add({
        featureLayer: selectedTemplate.featureLayer,
        addedGraphics: [newGraphic]
      });

      undoManager.add(operation);

      checkUI();
    });
     
  });
  dojo.connect(noAddButton, "onclick", function () {
    dijit.byId("addFeatureDialog").hide();
  });
     
});
0 Kudos
JasonZou
Occasional Contributor III
I can see one problem. You defined the event handlers for yesAddButton and noAddButton inside the callback function of draw-end event. So every time when you draw a graphic, yesAddButton and noAddButton event handlers will be defined one time. It will end up that if you draw two graphics, applyEdits will be triggered twice; three graphics, call applyEdits three times, and so on.

Move below code outside of dojo.connect(drawToolbar, "onDrawEnd", function(geometry) {...}); block. It should clear the issue.
  dojo.connect(yesAddButton, "onclick", function () {     dijit.byId("addFeatureDialog").hide();     //when features are added - add them to the undo manager               selectedTemplate.featureLayer.applyEdits([newGraphic], null, null, function() {       var operation = new esri.dijit.editing.Add({         featureLayer: selectedTemplate.featureLayer,         addedGraphics: [newGraphic]       });        undoManager.add(operation);        checkUI();     });         });   dojo.connect(noAddButton, "onclick", function () {     dijit.byId("addFeatureDialog").hide();   });
0 Kudos
BethManghi
New Contributor III
Thank you, zj_zou!  Your advice did the trick!  I moved the yesAddButton and noAddButton event handlers to a different location in the code so they're only defined once.  Now, my original application is working as expected!
0 Kudos