I have a simple point editing javascript site. I am trying to get the topmost layer feature and unique ID from an identify to link the new feature to the feature it intersects when place. So if I place a new workorder on a hydrant I want the hydrant layer and the hydrants ID to populate in the workorder attributes. The problem is identifyTask is differed and the add function on "before-apply-edits" function finishes before the deffered returns the values. Is there a way to wait for the deferred or to move the differed into an update function to update the attributes with the values? Below is my code. I appologize in advance its pretty raw right now and I had to cut some to get under the character limit.
<script src="http://js.arcgis.com/3.9/"></script>
<script>
var map;
require([
"esri/map",
"esri/toolbars/edit",
"esri/tasks/PrintTask",
"esri/dijit/Geocoder",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/layers/FeatureLayer",
"esri/tasks/IdentifyTask",
"esri/tasks/IdentifyParameters",
"esri/dijit/editing/Editor",
"esri/dijit/editing/TemplatePicker",
"esri/config",
"dojo/_base/array", "dojo/parser", "dojo/keys", "dojo/_base/Color", "dojo/dom-construct", "dojo/dom-class",
"dijit/form/Button",
"dijit/layout/BorderContainer", "dijit/layout/ContentPane",
"dojo/domReady!"
], function (
Map, Edit, PrintTask, Geocoder,
ArcGISDynamicMapServiceLayer, FeatureLayer,
IdentifyTask, IdentifyParameters,
Editor, TemplatePicker,
esriConfig,
arrayUtils, parser, keys, Color, domConstruct, domClass, Button
) {
parser.parse();
var identifyTask, identifyParams;
// refer to "Using the Proxy Page" for more information: https://developers.arcgis.com/en/javascript/jshelp/ags_proxy.html
esriConfig.defaults.io.proxyUrl = "/proxy";
//This service is for development and testing purposes only. We recommend that you create your own geometry service for use within your applications.
esriConfig.defaults.geometryService = new esri.tasks.GeometryService("http://servername/arcgis/rest/services/Utilities/Geometry/GeometryServer");
map = new Map("map", {
basemap: "topo",
center: [-116.140, 43.589],
zoom: 16
});
geocoder = new Geocoder({
map: map
}, "search");
geocoder.startup();
map.on("layers-add-result", initEditor);
//add WorkOrders and Feature
var facilityURL = "http://servername/arcgis/rest/services/UWID_MobileMap/MapServer"
var facilityLayer = new ArcGISDynamicMapServiceLayer(facilityURL);
map.addLayer(facilityLayer);
var operationsPointLayer = new FeatureLayer("http://servername/arcgis/rest/services/UWID_WorkOrderEdit/FeatureServer/0", {
mode: FeatureLayer.MODE_ONDEMAND,
id: "WORKORDER",
outFields: ["*"]
});
map.addLayers([
operationsPointLayer
]);
var printUrl = "http://servername/arcgis/rest/services/UWID_WorkOrderPrint/GPServer/WorkOrderPrint";
printTask = new esri.tasks.PrintTask(printUrl, { async: true });
pparams = new esri.tasks.PrintParameters();
pparams.map = map;
function initEditor(evt) {
var templateLayers = arrayUtils.map(evt.layers, function (result) {
return result.layer;
});
var templatePicker = new TemplatePicker({
featureLayers: templateLayers,
grouping: true,
rows: "auto",
columns: 3
}, "templateDiv");
templatePicker.startup();
var layers = arrayUtils.map(evt.layers, function (result) {
return { featureLayer: result.layer };
});
var settings = {
map: map,
templatePicker: templatePicker,
layerInfos: layers,
};
operationsPointLayer.on("before-apply-edits", function (evt) {
var targetID = evt.target
var toAdd = evt.adds;
dojo.forEach(toAdd, function (add) {
//setupID to get assett data
var layerIDName = null;
var layerLegacyID = null;
//layerLegacyID, layerIDName = doIdentify(add);
//add.attributes.FCLASS = layerIDName;
//add.attributes.FACILITYKEY = layerLegacyID;
add.attributes.WORKID = new Date().valueOf();
//add.attributes.CREATEDBY
var test = new Date().toLocaleDateString();
var datenow = new Date();
var strDate = (datenow.getMonth() + 1) + "/";
strDate += datenow.getDate() + "/";
strDate += datenow.getFullYear();
add.attributes.CREATEDDATE = strDate;
add.attributes.LASTUPDATE = strDate;
//add.attributes.LASTEDITOR
identifyTask = new IdentifyTask(facilityURL);
identifyParams = new IdentifyParameters();
identifyParams.tolerance = 5;
identifyParams.returnGeometry = true;
//identifyParams.layerIds = [0, 2];
identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_TOP;
identifyParams.width = map.width;
identifyParams.height = map.height;
identifyParams.geometry = add.geometry;
identifyParams.mapExtent = map.extent;
identifyTask.execute(identifyParams, function (idResults) {
idresult = idResults[0];
layerIDName = idresult.layerName;
layerLegacyID = idresult.feature.attributes.LEGACYID;
add.attributes.FCLASS = layerIDName;
add.attributes.FACILITYKEY = layerLegacyID;
});
});
});
var params = { settings: settings };
var myEditor = new Editor(params, 'editorDiv');
var printButton = new Button({
label: "Print", onClick: function () {
var workIDtoPrint = myEditor.attributeInspector.layerInfos[0].fieldInfos[0].dijit.value
print(workIDtoPrint)
}
}, domConstruct.create("div", null, myEditor.attributeInspector.editButtons, "first"));
domClass.add(printButton.domNode, "atiMyButton");
myEditor.startup();
}
});
function print(workIDT) {
var ptemplate = new esri.tasks.PrintTemplate();
// use the extent of the webmap in the output PDF
console.log("work ID " + workIDT)
ptemplate.preserveScale = true;
pparams.template = ptemplate;
pparams.extraParameters = { WorkIDToPrint: workIDT };
printTask.execute(pparams, printComplete);
}
function printComplete(result) {
console.log("url " + result.url)
window.open(result.url);
}
</script>