TJSDocument delete action
When emitting this event, TJSDocument passes "undefined" as the first argument instead of the document?
1 Reply
This can be a little tricky for TJSDocument. The store value
$doc
is the underlying document and when it is deleted in Foundry that is set to undefined
.
Depending on your use case using optional chaining like $doc?.name
can come into play or a {#if $doc}
directive in a template.
There are two ways to be notified when a document is deleted. The one that works best right now is using the TJSDocument options delete
callback. You can set a callback function that will be invoked with the Foundry document that is deleted.
You can do this by new TJSDocument({ delete: (delDoc) => console.log('!! Document deleted: ${delDoc?.name}
});
You can also set the callback via doc.setOptions({ delete: () => ... });
Be mindful of the callbacks set and how TJSDocument is used as you are in charge of destroying any TJSDocument instance as necessary via doc.destroy()
. This is important if you create a TJSDocument instance in a component that is destroyed / recreated, etc.
----
The other option for receiving notifications is the getter doc.updateOptions
, note no $
as this is a getter of the TJSDocument instance itself, that will have the state of the last "render context" from Foundry. Foundry being Foundry this data is potentially inconsistent across core versions and also likely will continue to be modified across Foundry core versions, but updateOptions
will have the last state and it will be { action: 'delete' }
when subscribers are notified. Note Foundry also uses renderContext
at times instead of action
and likely in v12 things are going to switch to renderContext
at the whims of the core team.
So try this in a component to see these changes:
This won't pick up the delete
update option when set with the current TRL, but it will work in the TRL version soon to be released. I'm not sure if something internal changed in Svelte, but I have it working as intended w/ the next TRL release.
Right now you'll get logged messages when the document updates where the value is action: 'update'
and more data from the Foundry DB operation. IE change the name of the document, etc.
----
Right now it's best to use the delete
options callback.