Forien
Forien
TTyphonJS
Created by Forien on 9/12/2023 in #typhonjs-runtime
TJSDocument not working properly with Module Sub-Types (Data Models)
In Foundry, modules can define a Sub-Types using Data Models. On Data Models we can also define functions. We can also update the parent document using this.updateSource(), for example:
export default class MyCustomDataModel extends foundry.abstract.TypeDataModel {
static defineSchema() {
const fields = foundry.data.fields;

return {
customField: new fields.StringField({}),
}
}

updateField(value) {
this.updateSource({customField: value});
}
}
export default class MyCustomDataModel extends foundry.abstract.TypeDataModel {
static defineSchema() {
const fields = foundry.data.fields;

return {
customField: new fields.StringField({}),
}
}

updateField(value) {
this.updateSource({customField: value});
}
}
this however, does not update TJDocument and does not update the Store. Changing the function to updating the parent, however, works:
updateField(value) {
this.parent.update({"system.customField": value});
}
updateField(value) {
this.parent.update({"system.customField": value});
}
From what I can tell, the DataModel#updateSource does not call any Hooks, which might be why. I think it's worth investigating if there is a way of making DataModel#updateSource triggering the Store in TJSDocument to update.
4 replies