Last active
August 29, 2015 14:28
-
-
Save odoe/437198dbc5e091918a93 to your computer and use it in GitHub Desktop.
Sample edit tools for EsriJS 4.0beta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as esriRequest from 'esri/request'; | |
import * as GraphicsLayer from 'esri/layers/GraphicsLayer'; | |
class Edit { | |
constructor(params) { | |
this.map = params.map; | |
this.layer = params.layer; | |
this.dummyLayer = new GraphicsLayer(); | |
this.cache = []; | |
this.map.add(this.dummyLayer); | |
} | |
_edit(graphic, type) { | |
let usePost = true; | |
let action = `/${type.toLowerCase()}Features`; | |
let data = graphic.toJSON(); | |
let map = this.map; | |
let layer = this.layer; | |
let url = layer.url + action; | |
map.remove(layer); | |
this.dummyLayer.clear(); | |
esriRequest({ | |
url, | |
content: { | |
features: JSON.stringify([data]), | |
f: 'json' | |
}, | |
handleAs: 'json' | |
}, { usePost }) | |
.then(_ => map.add(layer)) | |
.otherwise(_ => map.add(layer)); | |
} | |
add(graphic, immediate = true) { | |
if (immediate) { | |
this._edit(graphic, 'add'); | |
} else { | |
this.cache.push({ type: 'add', graphic }); | |
this.dummyLayer.add(graphic); | |
} | |
} | |
update(graphic, immediate = true) { | |
if(immediate) { | |
this._edit(graphic, 'update'); | |
} else { | |
this.cache.push({ type: 'add', graphic }); | |
this.dummyLayer.add(graphic); | |
} | |
} | |
remove(graphic, immediate = true) { | |
if(immediate) { | |
this._edit(graphic, 'delete'); | |
} else { | |
this.cache.push({ type: 'delete', graphic }); | |
this.dummyLayer.add(graphic); | |
} | |
} | |
save() { | |
if (this.cache.length) { | |
this.cache.map(x => this._edit(x.graphic, x.type)); | |
this.dummyLayer.clear(); | |
this.cache.length = 0; | |
} | |
} | |
} | |
export default Edit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment