decorators

AMD: require(["esri/core/accessorSupport/decorators"], (decorators) => { /* code goes here */ });
ESM: import * as decorators from "@arcgis/core/core/accessorSupport/decorators.js";
Object: esri/core/accessorSupport/decorators
Since: ArcGIS Maps SDK for JavaScript 4.2

This module contains Accessor TypeScript decorators. Decorators allow us to define and/or modify behavior of existing properties, methods, and constructors at design time.

Method Overview

Name Return Type Summary Object

A property decorator that creates a two-way binding between the property it decorates and an inner property of one of its members.

decorators

This method decorator is used to define the method that will cast a property from a class.

decorators

This property decorator is used to define the function or class for a property.

decorators

This convenience decorator is used to define an Accessor property.

decorators

This decorator is used to define an Accessor subclass.

decorators

Method Details

aliasOf

Method
aliasOf(propertyName){Function}

A property decorator that creates a two-way binding between the property it decorates and an inner property of one of its members.

Parameter
propertyName String

The aliased property name.

Returns
Type Description
Function The property decorator.
Examples
// property example
@aliasOf("viewModel.name")
name: string = "name";
// method example
@aliasOf("viewModel.someMethod")
someMethod: () => string;

cast

Method
cast(propertyName){Function}

This method decorator is used to define the method that will cast a property from a class.

Parameter
propertyName String

The property name the function will cast.

Returns
Type Description
Function The method descriptor.
See also
Example
import Accessor from "esri/core/Accessor";
import { subclass, property, cast } from "esri/core/accessorSupport/decorators";

@subclass()
class Color extends Accessor {

  @property()
  r: number = 0;

  @property()
  g: number = 0;

  @property()
  b: number = 0;

  @property()
  a: number = 1;

  @cast("r")
  @cast("g")
  @cast("b")
  protected castComponent(value) {
    // cast method that clamp the value that
    // will be set on r, g or b between 0 and 255
    return Math.max(0, Math.min(255, value));
  }

  @cast("a")
  protected castAlpha(value) {
    // cast method that clamp the value that
    // will be set on a between 0 and 255
    return Math.max(0, Math.min(1, value));
  }
}

cast

Method
cast(functionOrClass){Function}

This property decorator is used to define the function or class for a property.

Parameter
functionOrClass Function

The function or class to cast the property

Returns
Type Description
Function The property descriptor.
See also
Example
import Accessor from "esri/core/Accessor";
import { subclass, property, cast } from "esri/core/accessorSupport/decorators";

function clampRGB(component: number) {
  return Math.min(Math.max(component, 0), 255);
}

function clampAlpha(alpha: number) {
  return Math.min(Math.max(alpha, 0), 1);
}

@subclass()
class Color extends Accessor {

  @property()
  @cast(clampRGB)
  r: number = 0;

  @property()
  @cast(clampRGB)
  g: number = 0;

  @property()
  @cast(clampRGB)
  b: number = 0;

  @property()
  @cast(clampRGB)
  a: number = 1;
}

property

Method
property(propertyMetadata){Function}

This convenience decorator is used to define an Accessor property. Any property defined with this decorator can now be get and set. In addition, you can watch for any property changes.

Parameters
Specification
propertyMetadata Object
optional

An object describing the property.

Specification
dependsOn String[]
optional

Property names of dependencies.

type Function
optional

The constructor used to autocast the property.

cast Function
optional

The function to use to autocast the property. Alternative to define the type. The function is called with the value set by the user and should return the cast value.

readOnly Boolean
optional
Default Value: false

Indicates whether the property is read-only.

constructOnly Boolean
optional
Default Value: false

Indicates whether the property can be set during construction but is otherwise read-only.

aliasOf String
optional

The property decorator that creates a two-way binding between the property it decorates and an inner property of one of its members.

value Object
optional

The default value for the property.

Returns
Type Description
Function The property descriptor.
See also
Example
// typescript syntax to specify the property.
@property()
title: string = "Awesome Title!"

subclass

Method
subclass(declaredClass){Function}

This decorator is used to define an Accessor subclass. Any subclass of Accessor must use the @subclass decorator.

Parameter
declaredClass String
optional

The subclass name.

Returns
Type Description
Function The class decorator.
Example
// TypeScript syntax which creates a subclass that extends the Accessor class.
@subclass("my.custom.class")
class MyCustomClass extends Accessor {
  // ...
}

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