@vttforge/core / BaseTypeDataModel
Function: BaseTypeDataModel() ​
Call Signature ​
BaseTypeDataModel(): (...
args) =>TypeDataModelHooks
Build a base class with no knowledge of the schema.
The hooks are typed; the schema's own fields are not, since nothing said what they are. Pass your schema function instead to get those too.
Returns ​
(...args) => TypeDataModelHooks
Call Signature ​
BaseTypeDataModel<
S>(defineSchema):TypedTypeDataModelCtor<S>
Build a base class that knows its schema.
Hand it the function that returns your fields and it implements static defineSchema() for you, so the schema is written once:
const defineCharacterSchema = () => {
const f = fields();
return { level: new f.NumberField({ required: true, nullable: false, initial: 1 }) };
};
class CharacterData extends BaseTypeDataModel(defineCharacterSchema) {
declare armorClass: number;
prepareDerivedData() {
this.armorClass = 10 + this.level; // this.level is number
}
}this.parent is the document this data belongs to. It comes back as the shared document surface, since nothing said which kind of document holds this schema. Name the kind to reach items and the rest:
class CharacterData extends BaseTypeDataModel(defineCharacterSchema) {
declare parent: ActorLike;
prepareDerivedData() {
this.carried = this.parent.items.reduce((n) => n + 1, 0);
}
}It has to be a function, not an object: fields() reads a Foundry global that does not exist when the module is first evaluated.
A subclass may still declare its own static defineSchema(); that one wins, the same as any other static.
Type Parameters ​
S ​
S extends Record<string, FieldInstance>
Parameters ​
defineSchema ​
() => S