Auto populate with edit widget

2468
1
Jump to solution
06-24-2012 03:34 PM
AHay
by
New Contributor III
Flex viewer 2.5
Hi we are using the  selection complete event of the editor widget to auto populate some fields when a feature is edited
we use this to populate the field that relates two feature classes in this case the filed NAME_REL, the function sends varaible to a second function Get_rel that runs a spatial query and populates the fields

problem is that the fields that are calculated in the get_rel function using spatial query do not make it to the editor form before it opens ans as such are not saved, the fields that are calculated directly in selection complete function are in the editor form when it opens and are this saved.

this is despite the apply edits parts of the functions, this does "apply the edits"  if i use the info tool to query the feature the relevant fields have the nec values but if i close application and reopen the values have not been saved

Can someone please advise me as to how to make the selection complete function wait for the Get_rel function to complete before it concludes,
is this an even listener scenario?, if so how would i implement it ?

private function featureLayer_selectionComplete(event:FeatureLayerEvent):void             {                 for each (var field:Field in event.target.layerDetails.fields)                 {                     if (field.type == Field.TYPE_DATE)                     {                         for each (var feature:Graphic in event.features)                         {                             var date:Date = new Date(feature.attributes[field.name]);                             if (date.milliseconds == 999)                             {                                 date.milliseconds++; //Add 1ms to date values ending in 999 to workaround REST date bug                                 feature.attributes[field.name] = date.time;                             }                         }                     }                                         if (field.name == "NAME_REL")                     {                         getRel(feature,"Paddock - Yard","NAME_P");                                                  field.editable = false;                     }                         if (field.name == "CREATEDATE")                             {                             if(feature.attributes[field.name] == null)                             {                                var newDate:Date = new Date();                                feature.attributes[field.name] =newDate.time;                             }                         field.editable = false;                     }                     if (field.name == "LASTUPDATE")                     {                                                 var lastdate:Date = new Date();                         feature.attributes[field.name] = lastdate.time;                         field.editable = false;                     }                                  }                 var updates:Array = [ feature ];                 event.featureLayer.applyEdits(null, updates, null);                 layername = event.featureLayer.name;             } private function getRel(feature:Graphic,lyr:String,field:String):void    {     var name:String;     var fid:String;          var query:Query = new Query();     query.geometry = feature.geometry;          var featureLayer:FeatureLayer = map.getLayer(lyr) as FeatureLayer;     featureLayer.queryFeatures(query, new AsyncResponder(onResult, onFault));          function onResult(featureSet:FeatureSet, token:Object = null):void     {      if (featureSet.features.length > 0)      {       for each (var myGraphic:Graphic in featureSet.features)       {          fid = myGraphic.attributes["ID"];        feature.attributes["RID"] = fid;                        if (myGraphic.attributes[field] != null)                 {         name = myGraphic.attributes["NAME_P"];         feature.attributes["NAME_REL"] = name;                  }        else        {         Alert.show("No " + lyr + " name found.");        }                var updates:Array = [ feature ];        featureLayer.applyEdits(null, updates, null);        Alert.show(feature.attributes["NAME_REL"],"Get Rel NAME_REL")        Alert.show(feature.attributes["RID"],"Get Rel RID")               }            }      else      {       Alert.show("No " + lyr + " found. Are you sure you want to add this without a " + lyr + "?");      }         }          function onFault(info:Object, token:Object = null):void     {      Alert.show(info.faultString + "\n\n" + info.faultDetail, "queryTask fault " + info.faultCode);     }         }
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AHay
by
New Contributor III
OK we figured it out was very simple in end just needed to pass another variable to get_rel function ,  it still doesn't populate in editor form but edits are applied to data base

here is code for those interested;

private function featureLayer_selectionComplete(event:FeatureLayerEvent):void             {                 for each (var field:Field in event.target.layerDetails.fields)                 {                     if (field.type == Field.TYPE_DATE)                     {                         for each (var feature:Graphic in event.features)                         {                             var date:Date = new Date(feature.attributes[field.name]);                             if (date.milliseconds == 999)                             {                                 date.milliseconds++; //Add 1ms to date values ending in 999 to workaround REST date bug                                 feature.attributes[field.name] = date.time;                             }                         }                     }                                         if (field.name == "NAME_REL")                     {                         getRel(event.featureLayer,feature,"Paddock - Yard","NAME_P");                                                  field.editable = false;                     }                         if (field.name == "CREATEDATE")                             {                             if(feature.attributes[field.name] == null)                             {                                var newDate:Date = new Date();                                feature.attributes[field.name] =newDate.time;                             }                         field.editable = false;                     }                     if (field.name == "LASTUPDATE")                     {                                                 var lastdate:Date = new Date();                         feature.attributes[field.name] = lastdate.time;                         field.editable = false;                     }                                  }                 var updates:Array = [ feature ];                 event.featureLayer.applyEdits(null, updates, null);                 layername = event.featureLayer.name;             }  private function getRel(featureTrg:FeatureLayer,featureRel:Graphic,lyr:String,field:String):void    {     var name:String;     var fid:String;          var query:Query = new Query();     query.geometry = featureRel.geometry;          var featureLayer:FeatureLayer = map.getLayer(lyr) as FeatureLayer;     featureLayer.queryFeatures(query, new AsyncResponder(onResult, onFault));          function onResult(featureSet:FeatureSet, token:Object = null):void     {      if (featureSet.features.length > 0)      {       for each (var myGraphic:Graphic in featureSet.features)       {          fid = myGraphic.attributes["ID"];        featureRel.attributes["RID"] = fid;                            if (myGraphic.attributes[field] != null)                 {         name = myGraphic.attributes["NAME_P"];         featureRel.attributes["NAME_REL"] = name;                  }        else        {         Alert.show("No " + lyr + " name found.");        }                var updates:Array = [ featureRel ];        featureTrg.applyEdits(null, updates, null);                              }            }      else      {       Alert.show("No " + lyr + " found. Are you sure you want to add this without a " + lyr + "?");      }         }

View solution in original post

0 Kudos
1 Reply
AHay
by
New Contributor III
OK we figured it out was very simple in end just needed to pass another variable to get_rel function ,  it still doesn't populate in editor form but edits are applied to data base

here is code for those interested;

private function featureLayer_selectionComplete(event:FeatureLayerEvent):void             {                 for each (var field:Field in event.target.layerDetails.fields)                 {                     if (field.type == Field.TYPE_DATE)                     {                         for each (var feature:Graphic in event.features)                         {                             var date:Date = new Date(feature.attributes[field.name]);                             if (date.milliseconds == 999)                             {                                 date.milliseconds++; //Add 1ms to date values ending in 999 to workaround REST date bug                                 feature.attributes[field.name] = date.time;                             }                         }                     }                                         if (field.name == "NAME_REL")                     {                         getRel(event.featureLayer,feature,"Paddock - Yard","NAME_P");                                                  field.editable = false;                     }                         if (field.name == "CREATEDATE")                             {                             if(feature.attributes[field.name] == null)                             {                                var newDate:Date = new Date();                                feature.attributes[field.name] =newDate.time;                             }                         field.editable = false;                     }                     if (field.name == "LASTUPDATE")                     {                                                 var lastdate:Date = new Date();                         feature.attributes[field.name] = lastdate.time;                         field.editable = false;                     }                                  }                 var updates:Array = [ feature ];                 event.featureLayer.applyEdits(null, updates, null);                 layername = event.featureLayer.name;             }  private function getRel(featureTrg:FeatureLayer,featureRel:Graphic,lyr:String,field:String):void    {     var name:String;     var fid:String;          var query:Query = new Query();     query.geometry = featureRel.geometry;          var featureLayer:FeatureLayer = map.getLayer(lyr) as FeatureLayer;     featureLayer.queryFeatures(query, new AsyncResponder(onResult, onFault));          function onResult(featureSet:FeatureSet, token:Object = null):void     {      if (featureSet.features.length > 0)      {       for each (var myGraphic:Graphic in featureSet.features)       {          fid = myGraphic.attributes["ID"];        featureRel.attributes["RID"] = fid;                            if (myGraphic.attributes[field] != null)                 {         name = myGraphic.attributes["NAME_P"];         featureRel.attributes["NAME_REL"] = name;                  }        else        {         Alert.show("No " + lyr + " name found.");        }                var updates:Array = [ featureRel ];        featureTrg.applyEdits(null, updates, null);                              }            }      else      {       Alert.show("No " + lyr + " found. Are you sure you want to add this without a " + lyr + "?");      }         }
0 Kudos