JavaScript API 2.7 vs. 2.8 - FeatureLayer.applyEdits

4845
15
Jump to solution
04-02-2012 12:39 PM
by Anonymous User
Not applicable
Hello all,

Has anyone had any issues running applyEdits on feature layers since the update to version 2.8 of the API?  I find that with versions 2.7 or earlier, I could execute applyEdits() on a FeatureLayer object, even if the layer hasn't been added to a map, with essentially two lines of code:

var featureLayer = new esri.layers.FeatureLayer(url,{<opts>});
featureLayer.applyEdits([newGraphic],null,null,function(addResults){ // verify results });

However, when I try this in 2.8, and if the layer is not already added to a map object, I get an error message that looks like:

----- TypeError: arr is undefined http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.8 Line 14 -----


I've found that the only way to work around this is to actually add the layer to the map (even if I don't necessarily want it displayed to the user), then run applyEdits...but this makes things a bit more complicated.  Basically, after creating the layer, I need to connect to the onLayerAddResult function and run applyEdits within that callback event...when the callback is fired, I can remove the layer again (if I don't want to keep it in the map), and disconnect from the event.  Once that is setup, I finally add the layer to the map and wait for the callback to execute.

var featureLayer = new esri.layers.FeatureLayer(url,{<opts>,visible:false}); var listener = dojo.connect(map,"onLayerAddResult",this,function(layer){  if (layer==featureLayer)  {   dojo.disconnect(listener);   featureLayer.applyEdits([newGraphic],null,null,function(addResults){ // verify results });   map.removeLayer(featureLayer);  } }); map.addLayer(featureLayer);


My guess is that this is a result of one of the two following fixes/enhancements listed for version 2.8:


  • NIM079205: Enhance feature layer to support editor tracking

  • NIM079209: Do not serialize non-editable fields from a feature layer in applyEdits


If it's the latter, then I would suspect that the error is happening because the FeatureLayer has not yet retrieved the metadata from the FeatureLayer's REST service, and that this only happens after the layer gets added to a map.

Should it still be possible to applyEdits on a layer that is not added to a map object first?  Alternatively, is there a better way I can do this?

Thanks for any advice.
0 Kudos
1 Solution

Accepted Solutions
derekswingley1
Frequent Contributor
We fixed this in 3.0:  http://help.arcgis.com/en/webapi/javascript/arcgis/help/jshelp/new_v30.html

Specifically:

NIM081335: Feature Layer: applyEdits throws error when called before adding the layer to map

View solution in original post

0 Kudos
15 Replies
derekswingley1
Frequent Contributor

Should it still be possible to applyEdits on a layer that is not added to a map object first?  Alternatively, is there a better way I can do this?


Yes. This still works for me at 2.8:  http://jsfiddle.net/aZjHR/

Can you post a more complete code sample that shows this problem?
0 Kudos
by Anonymous User
Not applicable
I'm not sure that the example you're linking to is comparable - in my case, the FeatureLayer points to a REST url.  In the sample you linked to, the FeatureLayer is being created from an empty FeatureCollection object that has no association with an ArcGIS REST feature service.

I'll try to put together a sample, but I don't have an ArcGIS service that I can easily open up to the public for applying edits...any ideas of what I could use for a substitute?
0 Kudos
by Anonymous User
Not applicable
Actually, if I take that exact sample, and substitute these lines:

        featureLayer = new esri.layers.FeatureLayer(featureCollection, {
          id: 'flickrLayer',
          infoTemplate: popupTemplate
        });


with this:

        featureLayer = new esri.layers.FeatureLayer("http://localhost/ArcGIS/rest/services/MapName/FeatureServer");


Then I click run in the jsFiddle interface, I can reproduce the same error mentioned above (in Firefox/Firebug).  So if you have a FeatureServer that you can drop into that spot in your code, you might be able to see the same thing I'm seeing.
0 Kudos
derekswingley1
Frequent Contributor
I will take a look and get back to you.
0 Kudos
by Anonymous User
Not applicable
Has anyone been able to confirm this issue yet, or are there any recommended solutions besides my workaround outlined above?
0 Kudos
derekswingley1
Frequent Contributor
Hi Mike,

Apologies for not getting back to you.

This is indeed a bug. When a feature layer isn't added to the map, the edit is successful, but internally the applyEdits success callback is trying to draw the feature. Since the feature layer isn't added to the map, this isn't possible. We'll try to get a fix in the next release. Until then, please use the workaround you outlined above. I'll also make a note to post back here once I have a public bug tracking ID.
0 Kudos
gisscconsulting
New Contributor
I have exact error that prevents me updating from 2.6 to 2.8.

Look forward to seeing this fixed in next release.

Code break here

var attr = { "USAGE": status, "ACTION":"update"};

var newGraphic = new esri.Graphic(Point, null, attr, null);

featuureLayer.applyEdits([newGraphic], null, null);
0 Kudos
derekswingley1
Frequent Contributor
I have exact error that prevents me updating from 2.6 to 2.8.

Look forward to seeing this fixed in next release.

Code break here

var attr = { "USAGE": status, "ACTION":"update"};

var newGraphic = new esri.Graphic(Point, null, attr, null);

featuureLayer.applyEdits([newGraphic], null, null);


Can you confirm that applyEdits fails ONLY when you haven't added your feature layer to the map? If your featureLayer has been added to the map, and applyEdits is failing, that is a different issue.
0 Kudos
HaroldBostic
Occasional Contributor II
Derek,

I can confirm that applyEdits fails on standalone tables as well.  A table that is added to the published document, prior to 2.8 could be brought in as a feature layer and operated on.  Needless to say, adding this layer to the map is meaningless so the above solution does not work.  When playing around a bit, if I set MODE = Snapshot on the tables featurelayer, the error I get is object does not support setAttributes, however, removing that setting gives me the error that states something about the geomtery.

Hope this helps, until there is a fix, I'm stuck at 2.7.

Thanks
0 Kudos