Executing geoprocessing task in before-apply-edits

4783
4
Jump to solution
04-07-2015 06:18 AM
JillianStanford
Occasional Contributor III

Hello,

I’m creating a custom edit widget that will populate some values on the new feature from a geoprocessing service.

The plan is to capture the before-apply-edits event of the feature layer, execute the service, update the attributes and then open the attribute editor. I know this will work except the attribute editor doesn’t wait for the service to respond. I thought I needed to implement dojo.deferred but I guess I'm confused as to it's purpose because that’s not working. In the most general terms, how do I stop the thread while waiting for the service to respond?

Some code:

on(featureLayer, 'before-apply-edits', lang.hitch(this, this.onBeforeApplyEdits))

onBeforeApplyEdits: function (evt) {
            if (evt.adds != null) {
                this._newFeature = evt.adds[0];

                var features = [];
                features.push(this._newFeature);
                var featureSet = new FeatureSet();
                featureSet.features = features;
                var params = {"Query_Point": featureSet};
                
                var gp = new Geoprocessor("http://avalanche:6080/arcgis/rest/services/GetMileposts/GPServer/Get%20Mileposts");
                var def =  gp.execute(params);
                def.then(lang.hitch(this, function (idResults) {
                        this._newFeature.attributes["MP"] = idResults[0].value;
                }),function(err){
                });
           }
        }

What am I doing wrong?

Thanks!

Jill

0 Kudos
1 Solution

Accepted Solutions
JillianStanford
Occasional Contributor III

In case it can help someone else, here is how I ended up handling this:

At the beginning of the before-apply-edits handler I disable the attribute inspector popup using the dojox/widget/Standby widget. This greys out and adds a spinner to the popup. When the service results are returned I update the attributes using the attributeInspector refresh method and enable the popup.

This actually works better than waiting for the results before showing the attribute inspector because the user won't be left wondering why their click fire off the popup.

onBeforeApplyEdits: function (evt) {
              if (evt.adds != null) {
                  var standby = new Standby({ target: this.editor.attributeInspector.id, color: 'white' });
                  document.body.appendChild(standby.domNode);
                  standby.startup();
                  standby.show();
  1.                   this._newFeature = evt.adds[0];
                  var features = [];                   features.push(this._newFeature);                   var featureSet = new FeatureSet();                   featureSet.features = features;                   var gp = new Geoprocessor("http://avalanche:6080/arcgis/rest/services/GetMileposts/GPServer/Get%20Mileposts");                   var params = { "Query_Point": featureSet };                   gp.execute(params, lang.hitch(this, function (idResults) {                       this._newFeature.attributes["MP"] = idResults[0].value;                       var ai = this.editor.attributeInspector;                       ai.refresh();                       standby.hide();                   }), function (err) {                       standby.hide();                   });               }           }

View solution in original post

0 Kudos
4 Replies
JillianStanford
Occasional Contributor III

In case it can help someone else, here is how I ended up handling this:

At the beginning of the before-apply-edits handler I disable the attribute inspector popup using the dojox/widget/Standby widget. This greys out and adds a spinner to the popup. When the service results are returned I update the attributes using the attributeInspector refresh method and enable the popup.

This actually works better than waiting for the results before showing the attribute inspector because the user won't be left wondering why their click fire off the popup.

onBeforeApplyEdits: function (evt) {
              if (evt.adds != null) {
                  var standby = new Standby({ target: this.editor.attributeInspector.id, color: 'white' });
                  document.body.appendChild(standby.domNode);
                  standby.startup();
                  standby.show();
  1.                   this._newFeature = evt.adds[0];
                  var features = [];                   features.push(this._newFeature);                   var featureSet = new FeatureSet();                   featureSet.features = features;                   var gp = new Geoprocessor("http://avalanche:6080/arcgis/rest/services/GetMileposts/GPServer/Get%20Mileposts");                   var params = { "Query_Point": featureSet };                   gp.execute(params, lang.hitch(this, function (idResults) {                       this._newFeature.attributes["MP"] = idResults[0].value;                       var ai = this.editor.attributeInspector;                       ai.refresh();                       standby.hide();                   }), function (err) {                       standby.hide();                   });               }           }
0 Kudos
JoseSanchez
Occasional Contributor III

Jillian,

I am looking for a way to validate attributes entered by the editor. Is it done on the "onBeforeApplyEdits" event?

Thanks

0 Kudos
JillianStanford
Occasional Contributor III

Hi Jose,

Yes, exactly.

The parameter of the before-apply-edits event includes an array of the features that are being modified (added, updated or deleted). You can manipulate these features here before the feature service is updated.

In this example, I add the username and timestamp to the new record before it's pushed to the server.

_onBeforeApplyEdits: function (evt) {              
if (evt.adds != null) {                      
var currentDate = new Date();                      
var dateTime = currentDate.getTime() - (currentDate.getTimezoneOffset() * 60000);
this._newFeature = evt.adds[0];                      
this._newFeature.attributes["createUserField"] = this._currentUsername;                      
this._newFeature.attributes["createDateField"] = dateTime;                      
this._newFeature.attributes["updateUserField"] = this._currentUsername;                      
this._newFeature.attributes["updateDateField"] = dateTime;                  
}          
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Hope that helps!

Jill

JoseSanchez
Occasional Contributor III

Thank you very much!!!

0 Kudos