Skip to content

This sample demonstrates how to utilize reactiveUtils to watch the state of different data types and structures within the application. Using reactiveUtils allows for combining values from multiple sources which reduces or eliminates the need to create multiple watchers, as well as gives the ability to work directly with Collections.

The following code snippet shows how to use the watch method on the stationary, extent, and scale properties of the map to track changes and manipulate the UI.

let oldExtent = viewElement.extent;
reactiveUtils.watch(
// Track scale, extent, and movement in the view.
() => [viewElement.stationary, viewElement.extent, viewElement.scale],
([stationary, extent, scale]) => {
// Set the previous extent when the view is not moving.
if (stationary) {
oldExtent = extent;
return;
}
// Show the last stationary extent when movement resumes.
if (oldExtent === extent) showExtent(extentCells.previous, oldExtent);
// Show the latest extent and scale while the view moves.
showExtent(extentCells.current, extent);
currentScaleLabel.textContent = roundToHundredthsPlace(scale);
},
);

This code snippet below shows how to watch for a change in the allLayers property of the map, which is a Collection, and use its value.

reactiveUtils.watch(
// Track visibility of layers in the Collection.
() => [viewElement.allLayerViews.map((layerView) => layerView.visible)],
() => {
let layersVisibleText = "All layers are currently visible.";
visibleLayersList.textContent = "";
viewElement.map.allLayers.forEach((layer) => {
if (layer.visible) {
const listItem = document.createElement("calcite-list-item");
listItem.label = layer.title;
visibleLayersList.appendChild(listItem);
} else {
layersVisibleText = "Not all layers are currently visible.";
}
});
visibleLayersLabel.textContent = layersVisibleText;
},
);

The map displays Fire Perimeters for the state of California from the Department of Forestry and Fire Protection.