Hi all.
I started using resources in a really small project, and I already often find myself in a situation where I find them a bit annoying to use due to the lack of derivation.
Let me take an example: A service returns a resource. I want to display that resource value in my component, but the value needs to be transformed first. Problem: There is no way to transform the resource into another resource. The only possibility is to transform the value of the resource.
So I'm left with three choices.
- Create another child component which takes the value of the resource as input, and use a computed to transform it in that child component. Quite clean, but a bit annoying when the parent component is already really simple and doesn't really benefit from being split in two.
- Add a computed signal, which reads
resource.value()!and transforms it (note the!operator). But it's not obvious that this signal is derived from the resource value and can only be used inside a@if (resource.hasValue()). We lose the benefit of thehasValueguard. - Add a computed signal, which reads
resource.hasValue()andresource.value()and transforms it, or returns undefined if there is no value. But then either I use@if (resource.hasValue())in the template, and I still need to check if the computed signal value is not undefined, or I only test if the computed signal has a value, but it's weird to have two different sources that don't seem related.