Quick Tip - Tools for ArcGIS JSAPI 4.0beta1

Quick Tip - Tools for ArcGIS JSAPI 4.0beta1

Rene Rubalcava | August 23, 2015

I recently wrote about filling some gaps in the ArcGIS 4.0beta1. In that case, I did a quick edit sample. However that got me thinking that this edit sample could be enhanced to include updates, deletes and even caching of data so that edits aren't done right away.

What I came up with looks like this. Warning - ES6 incoming


So real quick, let's talk about what is happening here. This an ES2015 class called Edit. The constructor parameters require the map and a layer, specifically a FeatureLayer. The API for this class covers your basic editing needs. It has an add, update and del method. del is short for delete, but delete is a keyword in JavaScript, so need to shorten it a bit. I could have called it annihilate or maybe kill, but del seemed appropriate. Update - I changed this to remove

What these methods do is check for a second parameter called immediate, which has a default value of true. If this remains true, it simply pass the graphic to an _edit function with the type of action to take: add, update, or delete. The _edit method actually does all the hard work and converts the graphic to JSON and sends it to the appropriate endpoint of the ArcGIS REST API. Remember, it's all just REST.

Right now in the 4.0beta, you need to remove the FeatureLayer and add it back again in order to refresh the features shown after an edit. So keep that in mind when you are doing edits for now. I also force esri/request to use POST for each call, just to be safe.

Now if a method is passed an immediate of false, then the FeatureLayer is not edited and the graphic is added to a dummy layer and pushed into a cache. You then have the option to call the save method and push all the edits in the cache, clean the dummy layer and clear the cache. This can be useful if you have users that may not want their edits to be immediate, but maybe want to tidy things up before pushing them to the server. This portion could probably be extended as well to allow users to edit items in the cache.

Note - I should point out that my sample and the Edit tool do not handle symbology. So that's left up to you to define the symbology of your Graphics if you are not going to immediately edit the data. Otherwise when editing the symbology will be taken from the Feature Service.

This is just one tool currently in the repo. I'm also thinking of adding an Identify tool and any others to kind of fill the gaps in the beta for people that are just too eager to try and use the 4.0beta in a production environment.

Feel free to fork and contribute tools you think would be useful as well!