当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaScript ArcGIS Accessor.watch用法及代码示例


基本信息

以下是所在类或对象的基本信息。

AMD: require(["esri/core/Accessor"], (Accessor) => { /* code goes here */ });

ESM: import Accessor from "@arcgis/core/core/Accessor";

类: esri/core/Accessor

子类: ActionBase , ActiveLayerInfo , AddressCandidate , AddressCandidate , AggregatedGeometry , ApplicationProperties , ApplicationProperties , AreaMeasurement3DViewModel , AreasAndLengthsParameters , AreasAndLengthsParameters , Association , AssociationGeometriesResult , AttachmentInfo , AttachmentQuery , AttachmentQuery , AttachmentsViewModel , AttributeColorInfo , AttributionViewModel , AuthoringInfo , AuthoringInfoVisualVariable , Background , BarChartMediaInfo , BarcodeScannerInput , Basemap , BasemapGalleryItem , BasemapGalleryViewModel , BasemapLayerListViewModel , BasemapToggleViewModel , BasemapView , Bookmark , BookmarksViewModel , BufferParameters , BufferParameters , BuildingComponentSublayerView , BuildingExplorerViewModel , BuildingFilter , BuildingLevel , BuildingPhase , BuildingSublayer , BuildingSummaryStatistics , ButtonMenuItem , ButtonMenuViewModel , Callout3D , Camera , ChartMediaInfoValue , ChartMediaInfoValueSeries , ClassBreakInfo , ClosestFacilityParameters , ClosestFacilityParameters , ClosestFacilitySolveResult , ClosestFacilitySolveResult , CloudyWeather , Collection , ColorBackground , ColorRamp , ColorRamp , ColorSizeStop , ColorStop , ColormapInfo , ComboBoxInput , CompassViewModel , Content , Conversion , CoordinateConversionViewModel , CreateFeaturesWorkflowData , CreateWorkflowData , Credential , CustomTemplate , DataFile , DataFile , DataLayer , DataLayer , DatePickerViewModel , DateTimePickerInput , DaylightViewModel , DensifyParameters , DensifyParameters , DimensionalDefinition , DirectLineMeasurement3DViewModel , DirectionLine , DirectionPoint , DirectionsFeatureSet , DirectionsFeatureSet , DirectionsViewModel , DistanceMeasurement2DViewModel , DistanceParameters , DistanceParameters , Domain , Draw , DrawAction , Edges3D , EditorViewModel , Edits , Element , ElementExpressionInfo , ElevationProfileLine , ElevationProfileViewModel , Environment , ExpandViewModel , ExpressionInfo , ExpressionInfo , FacilityLayerInfo , FeatureEffect , FeatureEffect , FeatureFilter , FeatureFilter , FeatureFormViewModel , FeatureReductionCluster , FeatureReductionSelection , FeatureSet , FeatureSet , FeatureSnappingLayerSource , FeatureTableViewModel , FeatureTemplate , FeatureTemplatesViewModel , FeatureType , FeatureViewModel , Field , FieldColumn , FieldConfig , FieldGroupConfig , FieldInfo , FieldInfoFormat , FindParameters , FindParameters , FindResult , FindResult , FloorFilterViewModel , FlowRenderer , FoggyWeather , Font , FormTemplate , Format , FullscreenViewModel , FunctionResult , GPMessage , GPMessage , GamepadInputDevice , GamepadSettings , GamepadSettings , GeneralizeParameters , GeneralizeParameters , Geometry , GeometryFieldsInfo , Graphic , Ground , GroundView , HandleOwner , Handles , HeatmapColorStop , HeightModelInfo , HistogramViewModel , HomeViewModel , IdentifyParameters , IdentifyParameters , IdentifyResult , IdentifyResult , ImageAngleParameters , ImageAngleResult , ImageHistogramParameters , ImageHistogramParameters , ImageIdentifyParameters , ImageIdentifyParameters , ImageIdentifyResult , ImageIdentifyResult , ImageMediaInfo , ImageMediaInfoValue , ImageParameters , ImagePixelLocationParameters , ImagePixelLocationResult , ImageSample , ImageSampleParameters , ImageSampleResult , InitialViewProperties , InitialViewProperties , Input , InputField , InputFieldGroup , JobInfo , JobInfo , KMLSublayer , LOD , LabelClass , Layer , LayerFloorInfo , LayerListViewModel , LayerOptions , LayerView , LegendLayer , LegendLayer , LegendViewModel , LengthsParameters , LengthsParameters , LevelLayerInfo , Lighting , LineChartMediaInfo , LineOfSightTarget , LineOfSightViewModel , LineStyleMarker3D , LineStylePattern3D , LineSymbolMarker , LinearUnit , LinearUnit , ListItem , LocalBasemapsSource , LocateViewModel , Magnifier , Map , MapFloorInfo , MapImage , MeasurementViewModel , MeshComponent , MeshMaterial , MeshTexture , MosaicRule , NAMessage , NAMessage , Navigation , NavigationToggleViewModel , Network , NetworkElement , NetworkFeatureSet , NetworkUrl , OAuthInfo , OffsetParameters , OffsetParameters , OpacityStop , ParameterValue , ParameterValue , PieChartMediaInfo , PixelBlock , PointBarrier , PointCloudFilter , PointCloudRenderer , PolygonBarrier , PolylineBarrier , PopupTemplate , PopupViewModel , Portal , PortalFolder , PortalGroup , PortalItem , PortalItemResource , PortalQueryParams , PortalQueryResult , PortalRating , PortalUser , Presentation , PrintParameters , PrintParameters , PrintTemplate , PrintTemplate , PrintViewModel , ProjectParameters , ProjectParameters , Query , Query , RadioButtonsInput , RainyWeather , RasterColormapRenderer , RasterData , RasterData , RasterFunction , RasterInfo , RasterShadedReliefRenderer , RasterStretchRenderer , RelatedRecordsInfo , RelatedRecordsInfoFieldOrder , RelationParameters , RelationParameters , Relationship , RelationshipQuery , RelationshipQuery , Renderer , RouteInfo , RouteParameters , RouteParameters , RouteResult , RouteResult , RouteSolveResult , RouteSolveResult , ScaleBarViewModel , ScaleRangeSliderViewModel , ScaleRanges , SceneModification , Search , SearchLayer , SearchLayerField , SearchSource , SearchTable , SearchTableField , SearchViewModel , ServerInfo , ServiceAreaParameters , ServiceAreaParameters , ServiceAreaSolveResult , ServiceAreaSolveResult , ShadowCastViewModel , SiteLayerInfo , SizeStop , SketchViewModel , SlicePlane , SlicePlane , SliceViewModel , Slide , SliderViewModel , SnappingControlsViewModel , SnappingOptions , SpatialReference , StatisticDefinition , StatisticDefinition , Stop , StylePattern3D , Sublayer , SunnyWeather , SwipeViewModel , SwitchInput , Symbol , Symbol3DLayer , SynthesizeAssociationGeometriesParameters , Task , TemplateItem , TemplateItemGroup , TemplateOptions , Terminal , TerminalConfiguration , TextBoxInput , TileInfo , TileMatrixSet , TimeExtent , TimeInfo , TimeInterval , TimePickerViewModel , TimeSliderViewModel , TopFeaturesQuery , TopFilter , TraceLocation , TraceParameters , TraceResult , TrackViewModel , TravelMode , TrimExtendParameters , TrimExtendParameters , UI , UniqueValueInfo , UpdateWorkflowData , UtilityNetworkTraceViewModel , VectorFieldRenderer , View , ViewAnimation , ViewState , Viewing , Viewpoint , VisualVariable , WMSSublayer , WMTSStyle , WMTSSublayer , WeatherViewModel , Widget , Workflow , ZoomViewModel

自从:用于 JavaScript 4.0 的 ArcGIS API

用法说明

Accessor.watch函数(或属性)的定义如下:

watch (path, callback) {WatchHandle}


监视实例上的属性更改。

监视属性更改对于跟踪对象的更改至关重要。要开始监视属性的更改,请使用属性名称和每次属性更改时执行的回调函数调用watch()

let handle = mapview.watch("scale", function(newValue, oldValue, propertyName, target) {
  console.log(propertyName + " changed from " + oldValue + " to " + newValue);
});

要停止监视更改,请在 watch() 返回的对象上调用 remove() 方法。

handle.remove();

存储来自watch() 的结果对象以正确清理引用非常重要。

let viewHandles = [];
function setView(view) {
  // remove the handles for the current view.
  viewHandles.forEach(function(handle) {
    handle.remove();
  });
  viewHandles.length = 0;

  this.view = view;

  // watch for properties on the newly set view.
  if (view) {
    viewHandles.push(
      view.watch("scale", scaleWatcher);
    );
  }
}

setView(mapView);
setView(null);

get()set() 一样,可以通过传递路径来观察对象层次结构深处的属性。如果路径中的属性不存在,则使用 undefined 调用监视回调。

let view = new SceneView({
  map: new Map({
    basemap: "streets-vector"
  })
});

view.watch("map.basemap.title", function(newValue, oldValue) {
  console.log("basemap's title changed from " + oldValue + " to " + newValue);
});

view.map.basemap = "topo-vector";
// output: "basemap's title changed from Streets to Topographic"

view.map = null;
// output: "basemap's title changed from Topographic to undefined"

传递以逗号分隔的属性路径列表或属性路径数组,以使用相同的回调观察多个属性。使用回调调用的第三个参数来确定更改了哪些属性。

view.watch("center, scale, rotation", function(newValue, oldValue, propertyName) {
  console.log(propertyName + " changed");
});

// equivalent of
view.watch(["center", "scale", "rotation"], function(newValue, oldValue, propertyName) {
  console.log(propertyName + " changed");
});

// equivalent of
let callback = function(newValue, oldValue, propertyName) {
  console.log(propertyName + " changed");
}
view.watch("center", callback);
view.watch("scale", callback);
view.watch("rotation", callback);

Accessor 不会在其值更改后立即为属性调用监视回调。相反,当一个属性的值发生变化并且如果该属性被监视时,Accessor 会安排一个通知,然后在以后处理该通知。可以观察像 view.scale 这样频繁更改的属性,而无需限制回调。

// Divides the view.scale three times
view.watch("scale", function(newValue, oldValue) {
  console.log("view's scale changed from " + oldValue + " to " + newValue);
});
console.log("current view scale: " + view.scale);
view.scale = view.scale / 2;
view.scale = view.scale / 2;
view.scale = view.scale / 2;
console.log("current view scale: " + view.scale);

// output the following:
// current view scale: 36978595.474472
// current view scale: 4622324.434309
// view's scale changed from 36978595.474472 to 4622324.434309

watch() 带有一个实用模块reactiveUtils,它为查看属性提供了便利的函数。

参数:

类型说明

要监视的属性或属性。可以将多个属性指定为逗号分隔的列表。

callback watchCallback

属性值更改时执行的回调。

返回:

类型 说明
Accessor.WatchHandle 一个 watch 把手

相关用法


注:本文由纯净天空筛选整理自arcgis.com大神的英文原创作品 Accessor.watch。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。