Skip to content

Decorators

Bertrand Laporte edited this page Jan 11, 2019 · 6 revisions

List of decorators that can be used on hibe datasets:

@Dataset()

Class decorator to declare a class as a dataset

import { Dataset, value } from "../hibe";
@Dataset()
class Greeting {
    @value() message = "hello";
}

@value()

Property decorators to describe simple value properties. Value can be simple JS primitive types (string, boolean, number) or JS Objects - in which case only the object reference will be tracked: to track sub-references an object needs to be defined as a Dataset and the @dataset() decorator needs to be used instead of @value()

@Dataset
class ServerResponse {
    @value() isValid = true;
    @value() errorMessage = "";
    @value() data; // undefined by default
}

@dataset()

Property decorator to describe a property that is also a Dataset. It has 2 arguments

  • first: the Dataset constructor or a factory function [mandatory]
  • second: a boolean indicating if an object should be automatically created on first get() [optional - default: true]
@Dataset()
class LinkedListNode {
    @value() value;
    @dataset(TestNode, false) next: LinkedListNode;
}

@computed()

Property getter decorator to describe a computed property. The getter will only be called if its dependencies have changed (they will be automatically calculated when the property is computed) - otherwise the previous memoized value will be returned

@Dataset()
export class ArrTestNode {
    @value() name = "no name";
    @datalist(TestNode, false) list: TestNode[];

    @computed() get listLength() {
        if (!this.list) return 0;
        return this.list.length;
    }
}

@datalist()

Property decorator to describe an Array property. It takes 2 arguments

  • first: the Dataset constructor or a factory function of the array item [mandatory]
  • second: a boolean indicating if the array should be automatically created on first get() [optional - default: true]

Note: @datalist(Foo) is equivalent to @dataset(list(Foo))

@datamap()

Property decorator to describe a Map property. It takes 2 arguments

  • first: the Dataset constructor or a factory function of the map item [mandatory]
  • second: a boolean indicating if the array should be automatically created on first get() [optional - default: true]

Note 1: Data Map keys must be strings (otherwise they are not serializable - cf. convert())
Note 2: @datamap(Foo) is equivalent to @dataset(map(Foo))

Clone this wiki locally