Closed JaimeTorrealba closed 9 months ago
Hey @JaimeTorrealba !
What do you think?
I think what you're proposing could be done behind the scenes. E.g., use a watcher to watch this prop and if it changes and if the value is a function, then call the function.
But for me, there's a conceptual mismatch with that pattern.
Vue templates, along with HTML/XML/etc. are declarative by design. They just describe. They aren't imperative and they don't have side effects in and of themselves.
If simpleLog
were immediately called here ...
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :getObjectId="simpleLog">
... that would at least look imperative and like it's producing side effects.
For me, that's not the expected behavior. A DOM equivalent to your example would be:
<div onclick="simpleLog">
Like your example currently, just putting that in an HTML document doesn't execute simpleLog
. It just describes: "the onclick handler should be equal to simpleLog". It's up to the executing environment to execute the function when the user clicks or when the programmer triggers the click with JS.
unmm, yes I know we can do that using template ref, But I'm not entire agree with your answer :). Or maybe I don't understand you
maybe "simpleLog" was not a good example lets call it "callback"
The way I see it we don't have side effects, ThreeJs comes with a lot of methods like clone, getObjectById, getWorldPosition, etc Same way, the DOM has some methods in two. (I don't see how the onClick is equivalent to getObjectById)
I find, for example, a little annoying, to have to create a template ref for something like adding my mesh to a different layer.
in fact that doesn't work, maybe for https://github.com/Tresjs/tres/issues/230
Why don't open the possibilty to have something like
<script setup>
const myCallback = e => { // e here will be the actual function getObjectId, then I can do as I like for example
const myInnerMesh = e(''myInnerObjectId")
}
</script>
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :getObjectId="myCallback">
Same with others methods like clone or add?
Or maybe I don't understand you
No problem. Maybe my answer wasn't clear. ;)
Do I understand this right?
<script setup>
const myCallback = e => {
// e here will be the actual function getObjectId, then I can do as I like for example
const myInnerMesh = e("myInnerObjectId")
}
</script>
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :getObjectId="myCallback">
:field="value"
in a template normally means "bind value
to field
" (declarative)value
and send it the actual value of field
" (imperative)So any other alternative to this issue? more directives?
So any other alternative to this issue?
Afaik, the "blessed" way to do this in Vue is what you're already doing:
<script>
for interactivity and <template>
for declaring bindings. ref="..."
for moving from <template>
to <script>
.Fwiw, I also get tired of typing out shallowRef
s, etc. But I find that reading random people's Vue SFC code is a lot easier than reading random people's straight JS code.
So I think there's good stuff in the "blessed" way of doing things. Even if it takes longer to write.
Hey, @JaimeTorrealba thank you for opening this discussion, I find it really valuable and interesting.
Vue templates, along with HTML/XML/etc. are declarative by design. They just describe. They aren't imperative and they don't have side effects in and of themselves.
That would at least look imperative and like it's producing side effects.
I agree with @andretchen0 on this, passing a function through a prop is an antipattern in Vue, and since the goal of the library is to be a declarative solution using vue renderer this approach would kinda break that statement.
In Vue you pass data down you emit events up. React does allow pass functions down as prop by design.
One alternative would be to dynamically enable emits for the methods but this is done on the runtime
and not in the custom renderer, or as we initially thought, to detect if the instance[prop]
is a function and then pass the value as args of that function, but I guess @JaimeTorrealba for some cases you need the returned value right?
If the last question answer is yes, as @andretchen0 suggested, using refs
is the correct way forward. We could try emits or directives, but then you will have to create an individual directive for each function in the ecosystem of three.
Hey, thanks for taking the time, to read and response @alvarosabu, @andretchen0.
I'll definitely create some directives to address some of these methods (for example, v-layer, of course I'll open an issue first) but now I understand better the situation :)
Description
What is the right way to use some of the in-built funcions for the template instance?
For example, to add layers or other methods like getObjectId, add, etc?
Now, if I try this:
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :getObjectId="simpleLog">
Doesn't work, or even execute my functionSame with layers
<TresMesh :position="[-2, 2, 0]" :rotation="[0, Math.PI, 0]" :layers-enable="5">
Suggested solution
Accept the inline methods
Alternative
No response
Additional context
I know I can do this by template ref, but for many cases will be useful and less code, if we could access that methods inline.
What do you think?
Validations