symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
820 stars 297 forks source link

[LiveComponent] Execute model:set hook even for props changed on the server #1212

Open weaverryan opened 11 months ago

weaverryan commented 11 months ago

Currently, the model:set JS hook is only triggered if a model is changed by something on the client-side - e.g. a user types into an input or the user has a <button data-model="foo" data-value="bar" data-action="live#update">.

But, if the user triggers a LiveAction, and in that code, we change the value of a prop in PHP, the model:set hook is NOT called. This was done originally... only slightly on purpose: it was more code to add this feature, so I waited until it came up. Now it has, via a user on Slack.

I think it makes sense to trigger this whenever a prop changes for any reason. We DO have a spot where the props from the server are used to update the client-side props - https://github.com/symfony/ux/blob/2.x/src/LiveComponent/assets/src/Component/ValueStore.ts#L107 - it might require some sort of diff to detect any changes, then trigger the hook.

smnandre commented 11 months ago

How would this coordinate with model() ?

(cf targeting-loading-when-a-specific-model-changes )

weaverryan commented 11 months ago

If a model is updated "normally" on the client-side, then it would trigger the loading state for any fields with something like data-loading="model(email)|show". But if you changed email server-side, then that loading mechanism would never trigger. And I think that's expected - you're never waiting for the email change to finish loading. From the perspective of the JS component, it happens instantly (i.e. as soon as the Ajax call finishes, email changes and we immediately have the new HTML representing that state change).