-
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 把手
基本信息
以下是所在類或對象的基本信息。
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
函數(或屬性)的定義如下:
相關用法
- JavaScript ArcGIS Accessor.set用法及代碼示例
- JavaScript ArcGIS Accessor.WatchHandle用法及代碼示例
- JavaScript ArcGIS Accessor.get用法及代碼示例
- JavaScript ArcGIS AreaMeasurement3D.analysis用法及代碼示例
- JavaScript ArcGIS AttachmentQuery.keywords用法及代碼示例
- JavaScript Array values()用法及代碼示例
- JavaScript ArcGIS Attachments.container用法及代碼示例
- JavaScript Array fill()用法及代碼示例
- JavaScript Array reverse()用法及代碼示例
- JavaScript Atomics.xor()用法及代碼示例
- JavaScript ArcGIS Attribution.classes用法及代碼示例
- JavaScript Atomics.isLockFree()用法及代碼示例
- JavaScript Array isArray()用法及代碼示例
- JavaScript ArcGIS AreaMeasurement2D.viewModel用法及代碼示例
- JavaScript ArcGIS AreaMeasurement2D.on用法及代碼示例
- JavaScript Array entries()用法及代碼示例
- JavaScript ArcGIS AreaMeasurement3D.classes用法及代碼示例
- JavaScript ArcGIS AreaMeasurementAnalysis用法及代碼示例
- JavaScript ArcGIS AreaMeasurement3D.on用法及代碼示例
- JavaScript Array.from()用法及代碼示例
- JavaScript Array includes()用法及代碼示例
- JavaScript Atomics.add()用法及代碼示例
- JavaScript Array find()用法及代碼示例
- JavaScript ArcGIS Attachments.when用法及代碼示例
- JavaScript ArcGIS Attachments.on用法及代碼示例
注:本文由純淨天空篩選整理自arcgis.com大神的英文原創作品 Accessor.watch。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。