Skip to content

Send data back and forth as JSON #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2022

Conversation

weaverryan
Copy link
Member

Q A
Bug fix? yes
New feature? no
Tickets Fixes #251 (a bit unrelated, but it fixes it)
License MIT

tl;dr the "data" of your component is now sent via JSON for the Ajax calls. Ajax calls are still made via GET (unless you're triggering an action or your data is too long) with a new ?data={... json}.

Longer Explanation

Until now, I've been trying to send data back to the server as ?query params or as POST data (which is just query params stored in the body of the request).

However, using query parameters simply isn't robust enough. Most notably, it makes handling null values impossible. If you have a LiveProp - e.g. firstName - on the server set to null, how should that be sent back to the server? As ?firstName=? In that case, how do you distinguish from an empty string? One option is to NOT send firstName at all on the Ajax request. But then, what if you have a LiveProp that is an array with ['firstName' => null]? If we don't send firstName, the array will dehydrate to an empty array.

In the end, the goal is to effectively "freeze" your component's data, send it to the frontend, and have the frontend send it back. Using query parameters simply isn't robust enough to do this without, but JSON is.

There is no real downside to this: it just felt more pure to send data back and forth as query parameters and return HTML. We still return HTML, but sending JSON will make the behavior solid.

@norkunas
Copy link
Contributor

norkunas commented Feb 3, 2022

Also just want to point out that in this way it will be impossible to support #102 Bug I :) Unless with some workarounds which would allow to convert files to base64 so symfony serializer would transform it

@weaverryan
Copy link
Member Author

#102 Bug I :)
Bug I) Check file uploads with actions

You're right that uploads will definitely NOT work after this. But I think it's still fixable - data uri is one approach, but we can also look into submitting a "multipart" request body (like a normal file upload) where one part is the file upload and another part is the JSON data.

@weaverryan weaverryan merged commit bb5b7c7 into symfony:2.x Feb 3, 2022
weaverryan added a commit that referenced this pull request Feb 4, 2022
…autofocus) (kbond)

This PR was merged into the 2.x branch.

Discussion
----------

[Twig][Live] null attribute values render without value (ie autofocus)

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Tickets       | n/a
| License       | MIT

Ref: #220 (comment)

A null attribute value renders without the _value_:

```twig
{# templates/components/my_component.html.twig #}
<input{{ attributes}}/>

{# render component #}
{{ component('my_component', { type: 'text', value: '', autofocus: null }) }}

{# renders as: #}
<input type="text" value="" autofocus/>
```

**TODO:**
- [x] `null` vs `''` (#220 (comment))
- [x] attributes without a value like `autofocus` (#220 (comment))
- [x] LiveComponent dehydration/hydration issues (`null` values are lost during re-render) - solved in #264

Commits
-------

c1e484b [Twig][Live] null attribute values render without value (ie autofocus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LiveComponent] _willDataFitInUrl does not calculate correctly
3 participants