FeatureLayer: "graphic-add" event is not fired consistently.

3336
8
08-31-2015 01:49 PM
mikechen
New Contributor

Hi,

I have added a FeatureLayer to my web map for online editing. I have also set an event listener to the "graphic-add" event.

I use the Editor widget and the TemplatePicker widget.

I first choose a template from the TemplatePicker, and then digitize a graph on the map.

The plan is to call a function to update the attribute information after the graph is added to the featurelayer.

myEditingFLayer.on("graphic-add", updateGrpAttribute);

updateGrpAttribute : function(evt){

var gg = evt.graphic;

gg.attributes["UserName"] = userName;

gg.attributes["LASTUPDATE"] = updateTime;

gg.attributes["CntyFips"] = CNTY_ID;

gg.attributes["CAPTUREMETH"] = "Placed on Map";

myEditingFLayer.applyEdits(null, [gg], null, EditNew_Success, error);

}

But, this handler is not consistently fired. If I see the progress bar displays, the designated event handler will not be executed.

Please advise.

Thanks,

Mike

0 Kudos
8 Replies
KellyHutchins
Esri Frequent Contributor

If you want to update a feature just before its added to a feature layer your best bet is to listen to the feature layer's before-apply-edits event. This event fires just before features are added to the layer.

0 Kudos
mikechen
New Contributor

Kelly, thanks for the reply.

I am not sure how to listen to "before-apply-edits" event.

When I use the "graphic-add" event, my scenario is as follows:

(1) choose a template from the TemplatePicker widget.

(2) digitize a graph on the map. Since the graph is based on selected template, I suppose the graph is added to the template's corresponding featurelayer once the sketch is finished. Is this understanding right?

(3) listen to the "graphic-add" event and update the attribute information.

If I listen to the "before-apply-edits" event, how should I listen to this event based on my workflow?

Thanks,

Mike

0 Kudos
KellyHutchins
Esri Frequent Contributor

Are you using the editor widget? Or are you using the template picker and draw toolbar on their own? If you are using the editor widget it will take care of adding the graphic to the feature layer for you and all you need to do is add logic to listen for on-before-apply-edits.

If you are using the template picker and draw toolbar without the Editor widget then take a look at this sample. Note that the sample code listens for draw-end and then calls applyEdits to add the graphic to the Feature Layer. At this point in the code you can associate attribute information with the feature. 

Edit without editor widget | ArcGIS API for JavaScript

0 Kudos
mikechen
New Contributor

Kelly, thanks for your reply.

I am using TemplatePicker widget and Editor widget. I have figured out how to apply "before-apply-edits" event. But, the designated event listener is not fired at some cases, too. This problem is the same as "graphic-add" event.

The problem happens after double-click to finish the sketch, if the progress bar on the Editor widget shows up, both event listeners will not be fired and the attached attributeInspector popup without any attribute updating.

If the progress bar does not show up, both event listeners will be fired and the attached attributeInspector popup with the updated attribute information.

If the progress bar show up, it will take a while (in seconds) for the digitized sketch to show up.

Since the event listeners are designed for their corresponding event, they should be fired no matter how long it takes for the edits to be applied.

I don't know why the event listeners are not fired if the progress bar shows up.

Thanks for your help.

Mike

0 Kudos
KellyHutchins
Esri Frequent Contributor

Can you post your code?

0 Kudos
mikechen
New Contributor

As the code is very long. I just copy the segments that are for this purpose.

The code segment for templatePicker and Editor:

templatePicker = new TemplatePicker(templateParam, "templateDiv");

templatePicker.startup();

templatePicker.on("selection-change", templateValidate);

templatePicker.clearSelection();

aEditor = new Editor(editor_params, 'editorDiv');

The code segment to define the event listener.

templateValidate : function(evt) {

selectedTemplate = this.getSelected();

LAYER_NAME = selectedTemplate.featureLayer.id;

if (LAYER_NAME == "myRdLayer" ) {

hdlrGrpAdded_rd = on("before-apply-edits",updateAttribute_rd);

}

},

The code segment of the event listener.

updateAttribute_rd : function(evt){

var dt = convert_js_time_to_sqlserver( String(new Date()) );

// check the edits is about adding, deleting, or updating

var bDelete = !($.isEmptyObject(evt.deletes));

var bAdd = !($.isEmptyObject(evt.adds));

var bUpdate = !($.isEmptyObject(evt.updates));

// end of check editing status

var gg;

if (bAdd){

console.log("Before a road is created, update road info. Time: " + new Date());

gg = evt.adds[0];

gg.attributes["LASTUPDATE"] = dt;

gg.attributes["LASTEDITOR"] = UserName;

gg.attributes["STCOFIPS"]   = CNTY_ID;

gg.attributes["STATENAME"]  = StateName;

gg.attributes["STATERIGHT"] = StateName;;

gg.attributes["FROMLEFT"]   = 1;

gg.attributes["FROMRIGHT"]  = 2;

gg.attributes["INWATER"]    = "No";

gg.attributes["ONEWAYDIR"]  = "From-To";

SAMS.srvlyrSams_roadCenterLn.applyEdits([gg], null, null, EditNew_Success, error2);

}

if (bDelete){

console.log("Delete a road. Time: " + new Date());

}

if (bUpdate){

console.log("Update a road. Time: " + new Date());

}

},

0 Kudos
mikechen
New Contributor

Kelly,

I think that I found the situation that the event listener is not fired.

First, the targeted FeatureLayer is one layer of a FeatureServer. We have noticed that this FeatureServer is not running fast. Every pan of the map will take a while for the data on the FeatureServer to redraw.

To increase the performance of the FeatureServer, we have set the scale dependence. This data will not be visible until the map is zoomed into about 4K. Basically, the FeatureServer data will be available at scale of 4k, 2k, 1K (the ESRI's universal scale for caching).

Secondly, the situation of not-firing the "before-apply-edits" or "graphic-add" event listener is like this: if the map is panned (intentionally or not) during the digitization, the data from the FeatureServer have to reload and the reloading is not fast for FeatureServer. This is the reason why the event listener is not fired.

If the map is not panned during the digitization, the designated event listener works well.

So, why do the map panning and/or FeatureServer data reloading cause the "graphic-add" and/or the "before-apply-edits" event listeners not working?

Thanks for your advice.

Mike

0 Kudos
KellyHutchins
Esri Frequent Contributor

I setup a test app here and can't reproduce. The before-apply-edits event fires even when I pan while creating a new feature.

JS Bin - Collaborative JavaScript Debugging

What mode are your feature layer's in? Have you tried adding your editable layer as a dynamic map service and then add the feature layer in selection only mode? Details about that can be found in the "Selection only mode" section here:

Feature Layers | Guide | ArcGIS API for JavaScript

0 Kudos