使用 ReactiveUtils 更改属性

尝试一下在线预览

此示例演示如何利用 reactiveUtils 来监视应用程序中不同数据类型和结构的状态。使用 reactiveUtils 允许组合来自多个源的值,从而减少或消除了创建多个观察程序的需要,并且能够直接使用集合

以下代码段演示如何在 MapView 的 stationaryscale 属性上使用 watch 方法来确定属性何时发生更改,并将新的缩放值记录到控制台。

         
1
2
3
4
5
6
7
8
9
// Use reactiveUtils to check when the view scale changes.
// view.stationary is included so that a message is only shown
// when the view is not moving.
reactiveUtils.watch(
    () => [view.stationary, view.scale], ([stationary, scale]) => {
        if (stationary) {
            console.log(`View Scale changed to: ${scale}`);
        }
    });

下面的代码段显示了在视图更改并将这些值记录到控制台后,如何利用 watch 方法访问视图范围的当前值和上一个值。

               
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Use reactiveUtils to check when the extent changes.
// view.stationary is included so that a message is only shown
// when the view is not moving.
let oldExtent = view.extent;
reactiveUtils.watch(
    () => [view.stationary, view.extent],
    ([stationary, extent], [wasStationary]) => {
        if (stationary) {
            console.log(`Current Extent: ${extent}`);
            console.log(`Previous Extent: ${oldExtent}`);
        } else if (wasStationary) {
            oldExtent = extent;
        }
    }
);

      
1
2
3
4
5
6
// Use reactiveUtils to check when a Collection has changed
reactiveUtils.watch(
    () => view.map.allLayers.every((layer) => layer.visible),
    (allVisible) => {
        console.log(`All layers are visible = ${allVisible}`)
    });

地图显示了林业和消防部提供的加利福尼亚州的火灾周长。

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.