[GIS] applyEdits to FeatureLayer does nothing

arcgis-javascript-apieditingfeature-layer

I am trying to create a series of layers using a county map of the US. In order to save load time I am only loading the county polygon geometries once, then using applyEdits (https://developers.arcgis.com/javascript/3/jsapi/featurelayer.html#applyedits) to add the county geometries to the layers, like a template. I have tried this in a lot of different ways here is what I currently have:

self.initializePolygonLayers = function(){
    var layers = self.layers;

    $.getJSON("api/layerData/templates/counties", {}, function (data) {
        self.initializeSinglePolygonLayer(self.layers()[self.layerIndex], data);
    });
};

self.initializeSinglePolygonLayer = function(layer, data) {

    if(layer.baseLayerName != "County")
    {
        self.layerIndex++;
        if(self.layerIndex < self.layers().length) {
            self.initializeSinglePolygonLayer(self.layers()[self.layerIndex], data);
        }
    } else {
        var counties = new Array();
        console.log("defining layer: " + layer.name);
        window[layer.name] = defineFeatureLayer("ObjectID", 2, false, ["${NAME}", "${*}"]);
        window[layer.name].setRenderer(createRampRenderer(layer.id));
        window[layer.name].setVisibility(false);
        map.addLayer(window[layer.name]);

        for(var index in data) {
            var county_obj = data[index];
            var geometry = wktToEsriJson(county_obj.geometry);
            var attributes = { "NAME" : county_obj.NAME, "ObjectID" : index, "PostalID" : county_obj.Postal_CountyRegion_ID
, "mappedField" : 0.0};

            if(geometry !== false) {
              var graphic = new esri.Graphic(geometry);
              graphic.setAttributes(attributes);
              counties[counties.length] = graphic;
            } else {
              console.log("Error Loading WKT: " + county_obj.geometry);
            }

        }

        console.log("applying edits:" + layer.name + " counties count: " + counties.length);

        window[layer.name].applyEdits(counties, null, null, function(data){
            console.log("success"); 
            console.log("success for layer " + layer.name + " graphics count:" + window[self.layers()[self.layerIndex].name].graphics.length);
        }, function(data){
            console.log("fail"); 
            console.log("fail for layer " + layer.name + " graphics count:" + window[self.layers()[self.layerIndex].name].graphics.length); 
        }).then(function() {
            console.log("then for layer " + layer.name + " graphics count:" + window[self.layers()[self.layerIndex].name].graphics.length);
            self.layerIndex++;
            if(self.layerIndex < self.layers().length) {
                self.initializeSinglePolygonLayer(self.layers()[self.layerIndex], data);
            }
        });
    }

};

Here is what happens, applyEdits succeeds for all the layer (I'm trying it for two layers) the first layer renders fine and the second one is blank. Looking into it the the first layer gets all the graphics and the second one, despite applyEdits succeeding, has no features. What's going on here?

Best Answer

I found the answer. The offending line is below:

window[layer.name] = defineFeatureLayer("ObjectID", 2, false, ["${NAME}", "${*}"]);

the definition of this function is below:

defineFeatureLayer = function(idString, dimension, ondemand, infoTemplateArray, feats)
    {

      var infoTemplate = new InfoTemplate(infoTemplateArray[0],infoTemplateArray[1]);
      var symbol;
      if(feats === undefined) {
        feats =[];
      }

      if(ondemand === undefined)
      {
        ondemand = false;
      }

      var esriFeatureType;

      if(dimension == 2)
      {
        esriFeatureType = "esriGeometryPolygon";
        symbol = new SimpleFillSymbol();
      }
      else if(dimension == 1)
      {
        esriFeatureType = "esriGeometryPolyline";
        symbol = new SimpleLineSymbol();
      }
      else
      {
        esriFeatureType = "esriGeometryPoint";
        symbol = new SimpleMarkerSymbol();
      }


      var featureCollection = {
        "layerDefinition": null,
        "featureSet": {
        "features": feats,
        "geometryType": esriFeatureType
        }
      };
      featureCollection.layerDefinition = {
        "geometryType": esriFeatureType,
        "objectIdField": "ObjectID",
        "drawingInfo": {
        "renderer": {
          "type": "simple",
          "symbol": symbol
        }
        },
        "fields": [{
          "name": "ObjectID",
          "alias": "ObjectID",
          "type": "esriFieldTypeOID"
        },{
          "name": "ID",
          "alias": "ID",
          "type": "esriFieldTypeInteger"
        },{
          "name": "mappedField",
          "alias": "mappedField",
          "type": "esriFieldTypeDouble"
        },{
          "name": "NAME",
          "alias": "NAME",
          "type": "esriFieldTypeString"
        }  
        ]
      };

      if( ondemand === true )
      {
      //  console.log("rendering layer " + idString + " as ondemand");
        featureLayer = new FeatureLayer(featureCollection, {
          id: idString,
          minScale : 80000,
          mode: FeatureLayer.MODE_ONDEMAND,
          infoTemplate: infoTemplate
        });
      }
      else {
        //console.log("rendering layer " + idString + " as snapshot");
        featureLayer = new FeatureLayer(featureCollection, {
          id: idString,
          mode: FeatureLayer.MODE_SNAPSHOT,
          infoTemplate: infoTemplate
        });
      }

      return featureLayer;
    };

As you can see the first param is idString and corresponds to the id option in the FeatureLayer constructor options object. Changing the above line to a unique string, in my case I did the following:

        window[layer.name] = defineFeatureLayer(layer.name, 2, false, ["${NAME}", "${*}"]);
Related Question