For my next trick, I would like to create an inventory list of items from the character
I have access to the Actor documentStore but I'm really not too sure what to do with it to make a list.
12 Replies
You should install
essential-svelte-esm
and check out the reactive document demos. There is a capability w/ TJSDocument to create a "reactive embedded collection". This allows you to do dynamic filtering / reducing of an embedded collection. Behind the scene this is implemented with "dynamic reducers" which is a separate library I built brought into TRL.
The goal of the next TRL release is to enable solid drag and drop support + sorting for dynamic reducers; hence this will be enabled for reactive embedded collections as well and will make creating inventory lists and any other table like views of embedded collections easy to implement.
This future capability is what will enable a TRL based game system reference repo done the "right way" and is why one doesn't exist currently.Dev snapshot video here: https://www.youtube.com/watch?v=Qb-8d8cm-9M
TyphonJS-FoundryVTT
YouTube
TRL - TyphonJS Runtime Library (0.0.19) dev snapshot - Reactive Emb...
Hi folks, just a quick dev snapshot update on a powerful upgrade to the reactive document / TJSDocument API. "Reactive Embedded Collections" are now possible to create in an adhoc or structured manner that allows non-destructive filtering and sorting of Foundry embedded collections in a manner that plays quite nice w/ Svelte.
Upgrade to TRL 0.0...
Thanks for the reply.
I'm trying to implement this in an ActorSheet, which already natively has drag and drop support. I notice that the EmbeddedDocAppShell implements its own.
Do I need that? If so how to implement it in an actor sheet without tripping over the native drag and drop mechanism? I'm guessing that if I rely on the native drag&drop, then reactivity won't work?
Or are you suggesting that I should wait till the next TRL release?
As things go I'm not really going to be able to walk you through any sort of interim solution whether that is using the Foundry core mechanisms or a custom solution in respect to hooking things up with reactive embedded collections. Certainly feel free to explore and try things out in the meantime.
It's probably best to wait until the next release. At least with this upcoming release all I'm working on is drag & drop + sorting aspects. Usually TRL has a cadence of around ~3 weeks per release. The most recent release was unusual as the scope grew quite a bit. I am working remotely for a bit of the next couple of months, so it might be longer than 3 weeks, but with a single goal to add hopefully it will be out soon.
Cool. Just for reference. I've done this as a first attempt:
Which does render a very basic list, non-reactively (you have to close and open the Actor sheet again before any new drops will show).
Even though you are not using reactive embedded collections this should update the list when data changes.
This is kind of where referring you back to the Svelte tutorials is probably relevant. 😄
Granted that is not a particularly good solution above as it will render again whenever anything in the document changes.
Hmm... you say I'm not using reactive embedded collections... but
doc
is a TJSDocument
and $doc.items
is an EmbeddedCollection
. Isn't that a reactive embedded collection?Watch the video above and install
essential-svelte-esm
to play around with the demo.I have 🙂 still confused though 🙂 I'll keep trying
Cool I got it using Svelte reactivity:
$: items = [...$doc.items];
You can certainly do that... What I listed above is just a shortcut of sorts. Svelte templates are reactive. Whenever you use a store IE
$doc
in a reactive statement IE $:
or in a template it will update reactively.
"reactive embedded collections" refers to the API of TJSDocument under the embedded
accessor. This is definitely an area where there could be better documentation in the actual source code. As things go pointing you to the TRL source code won't help too much sans thorough documentation. A fair amount of details are also in a separate library that can also use a bit more documentation. All of this does delve into more advanced territory in general. The API is rock solid and the separate library has 100% test coverage, so it all works, but I think it might be a bit troublesome in general to point you to to it understand things by just reading the code.
If I recall v10 was about to drop when I was working on that and had to switch over to maintaining FQL for that window of time. I will be buffing up all the documentation and provide more examples in the next TRL release.
In the meantime learning more about the Foundry API and how embedded collections work with the core API won't hurt.Sounds good. I had a look at the EmbeddedDocAppShell. The one thing I'm not grokking about it is where the array of items gets passed into the
embedded
store?
I see this "EmbeddedDocAppShell.svelte":
Which seems to be doing everything except actually passing in the items?
Or does it search through the documentStore's collections looking for one that matches the name "Item" as class name or something like that?
I assume so. Anyway I've managed to get a list working using the EmbeddedDoc thingy.
However, one problem persists. I can' figure out how to make a reactive SUM from the weight
property of each item in the inventory.
I tried this:
But of course that's not reactive because of the spread operator.
I'm gathering that I have to somehow register that as a reducer using doc.embedded.create
but it's not clear how I might do that.
Based on reading your svelte/store
code, my expectation is that I would do something like this:
But that doesn't work – so I'm not quite sure how to refactor my reducer as something that can be passed to the derived
property:
I understand I'm getting ahead of the docs here and you're busy progressing the library 🙂 so, no worries if you feel you can't get into it, but any help or pointers would be appreciated.
I managed to do this using svelte '$:
reactivity. no need for all the above.