odoe

DIY Editing

Blog Post created by odoe on Aug 19, 2015

esri-edit.jpg

So I've talked a lot about the ArcGIS API for JavaScript 4.0beta1 a lot recently. I've proclaimed my love of Accessors, and I think Promises are groovy. There's a the whole new concept of separating the map and the view. It's chock full of good stuff. It is however, beta. So not everything is 100% and some stuff just isn't quite ready yet.

 

But you're impatient. You like living on the edge. You drink wine from the bottle. You want some stuff to work now! One of the things not in the 4.0beta1 API is editing. If you look at the docs for a FeatureLayer, it has no editing capabilities at the moment. It will, just not yet. That's ok though. You're a developer... you got this.

 

What is editing after all? Editing is nothing more than a capability of a feature service in the ArcGIS REST API. I've told you before, learn to speak rest and everything else just comes together. For this case, let's assume you just want to add features to the service. There's a capability specifically for that. It even has a sample of what the request and response will look like. It can't get much simpler than that.

 

So this sample editor is about as simple as you can get to add features to a Feature Service.

var Editor = declare(null, {
  constructor: function (params) {
    this.map = params.map;
    this.layer = params.layer;
  },
  add: function (graphic) {
    var data = graphic.toJSON();
    var map = this.map;
    var layer = this.layer;
    // http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Add_Features/02r30000010m000000/
    var url = this.layer.url + "/addFeatures";
    map.remove(layer);
    esriRequest({
      url: url,
      content: {
        features: JSON.stringify([data]),
      }
    }, {
      usePost: true
    }).then(function (response) {
      map.add(layer);
    }).otherwise(function () {
      map.add(layer);
    });
  }
});

 

That's it. Provide a Map and FeatureLayer and you're all set.

 

Notice that as of right now, you need to remove the layer from the map and then add it again to get the edits to show. Beta!

 

I'm just using this on a shared service, but esri/request should be able to handle authentication for you if you need it. Don't hold me to this right now, I haven't tried it yet.

 

Need to support deletes and updates? Look at the delete features and update endpoints. Or just use applyEdits and manage it how you want. Remember... It's all just REST man!

 

Here is a demo of this module in action.

 

So go on, give it a shot. Beta is beta, but sometimes you just gotta do whatcha gotta do.

 

So go forth and hack away my friends!

 

For more geodev tips and tricks, check out my blog.

Outcomes