Slots module
📖 Description
Slots are an elementary part of Tripetto's data collection mechanism. They are defined by the form definition and denote the data that a form can collect. By default, there are slot types available to collect primitive data types like strings, numbers, booleans, and more. But it is also possible to define your own slot types for advanced data collection using the Slot
base class and the @slot
decorator (see example below).
There are four different kinds of slots:
static
: Static slots are fixed slots that are required for the functional operation of a block (for example, a string slot that holds the text value of a text input block);dynamic
: Dynamic slots are used for dynamic elements in a block (for example, slots that are created for each checkbox of a block that supports a customizable list of checkboxes);feature
: Feature slots are used for optional features of a block (for example, a slot that holds the score value of a block that supports an optional score feature);meta
: Meta slots are used to store metadata (for example, the time taken to answer a block).
See the Slots guide for more information.
📦 Built-in slot types
The following slot types are always available:
👩💻 Example
You can define your own slot types for advanced data collection using this Slot
base class and the @slot
decorator:
import { Slots, castToNumber } from "@tripetto/builder";
// Let's define a new slot type to store a square metre unit that automatically appends the unit symbol m².
@Slots.slot("square-metre")
class SquareMetre extends Slots.Slot<number> {
// Converts the pristine data to the correct type (in this case number).
public toValue(data): number {
return castToNumber(data);
}
// Retrieves the string representation of the value (in this case with the unit symbol m² added).
public toString(data): string {
return `${this.toValue(data)} m²`;
}
}
💎 Classes
🎀 Decorators
🎀 @deserialize
Marks a class property as a deserializable property. This means the property can be deserialized automatically from the form definition.
Decorator type
Property ℹ️
Applies to
Decorator signature
@serialize
If you don't want automatic deserialization, you need to override the deserialize
method in your slot class.
🎀 @property
Marks a class property as a slot property. Slot properties are stored in the form definition and are monitored by the builder. Whenever the value of a slot property changes, it kicks off a form definition change event in the builder. Use the @serialize
and @deserialize
decorators to enable automatic serialization to and deserialization from the form definition.
Decorator type
Property ℹ️
Applies to
Decorator signature
@property
Example
import { Slots, castToNumber } from "@tripetto/builder";
// Let's define a new slot type to store a square metre unit that automatically appends the unit symbol m².
@Slots.slot("square-metre")
class SquareMetre extends Slots.Slot<number> {
// Let's add a property to set a maximum number of square meters.
@property
@serialize // Enable automatic serialization to the form definition.
@deserialize // Enable automatic deserialization from the form definition.
public maximum?: number;
// Converts the pristine data to the correct type (in this case number).
public toValue(data): number {
const metres = castToNumber(data);
if (this.maximum && metres > this.maximum) {
return this.maximum;
}
return metres;
}
// Retrieves the string representation of the value (in this case with the unit symbol m² added).
public toString(data): string {
return `${this.toValue(data)} m²`;
}
}
🎀 @serialize
Marks a class property as a serializable property. This means the property can be serialized automatically to the form definition.
Decorator type
Property ℹ️
Applies to
Decorator signature
@serialize
If you don't want automatic serialization, you need to override the serialize
method in your slot class.
🎀 @slot
Registers a new slot type.
Decorator type
Class ℹ️
Applies to
Decorator signature
@slot(typeIdentifier: string)
Parameters
Name | Type | Optional | Description |
---|---|---|---|
typeIdentifier | string | No | Specifies the unique slot type identifier. |
The supplied type identifier must be unique; otherwise, an error is thrown. The following identifiers are reserved by the built-in slot types and therefore not allowed: string
, number
, boolean
, date
, text
, and numeric
.
Example
import { Slots, castToNumber } from "@tripetto/builder";
// Let's define a new slot type to store a square metre unit that automatically appends the unit symbol m².
@Slots.slot("square-metre")
class SquareMetre extends Slots.Slot<number> {
// Converts the pristine data to the correct type (in this case number).
public toValue(data): number {
return castToNumber(data);
}
// Retrieves the string representation of the value (in this case with the unit symbol m² added).
public toString(data): string {
return `${this.toValue(data)} m²`;
}
}