documentationfor yFiles for HTML 2.6

Attribute

Specialized type that can be used to describe meta information about other types, methods, fields, properties and constructors.

Inheritance Hierarchy

Remarks

All attributes extend Attribute or a subtype of an attribute. They can also implement Interfaces.

The actual creation of an attribute is delayed to the point in time when it is needed by the reflection API. Therefore, the attribute constructors are wrapped and replaced by a function that creates an instance of the attribute.

const MyAttribute = Attribute('MyAttribute', {
  constructor: function(message) {
    this.message = message
  }
})

class MyClass {
  get myField() { ... }

  // provide a static $meta property that returns an object with meta properties for each field
  static get $meta() {
    return {
      myField: [MyAttribute('hello').init({ recipient: 'field' })],
      // '$self' is for the class itself
      $self: [MyAttribute('hello').init({ recipient: 'class' })]
    }
  }
}
Class.fixType(MyClass) // required for classes that don't inherit from a yFiles library class

const fieldAttribute = MyClass.$class.getProperties()[0].getAttributes()[0]
console.log(fieldAttribute.message + ' ' + fieldAttribute.recipient) // 'hello field'

const classAttribute = MyClass.$class.getAttributes()[0]
console.log(classAttribute.message + ' ' + classAttribute.recipient) // 'hello class'

Even though MyAttribute is invoked at the definition of MyClass, no attribute is created until the reflection API is used (in the above example: using getCustomAttribute).

The result of the constructor function of an attribute is a function, that, when invoked, returns an instance of the attribute. It also provides an init method that adds the properties of the given object to the attribute.

Note: Attribute constructors are always called without the new keyword.

Type Details

yfiles module
lang
yfiles-umd modules
All modules
Legacy UMD name
yfiles.lang.Attribute