You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #245 [Live] Always send HTML over the wire instead of JSON (weaverryan)
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
[Live] Always send HTML over the wire instead of JSON
| Q | A
| ------------- | ---
| Bug fix? | yes (one part of it is)
| New feature? | yes
| Tickets | None
| License | MIT
Hi!
Currently, the LiveComponent controller expects to get JSON back from the server, containing `{ data: ..., html: '...' }`. But, Stimulus is so cool, that this isn't necessary! The LiveComponent Ajax requests can simply return HTML in all cases. The rendered element will have an updated `data-live-data-value` attribute with the latest data. And so, the Stimulus component instantly reads that updated data and incorporates it: there is no need to send back the "component data" on a separate key in the JSON.
This makes the live component system more boring in a good way: it's just a way to return HTML (and then we have fancy JavaScript that adds all the interactivity). It's also a bit faster now, as previously we were dehydrating the component data twice on re-render.
I can't think of any downsides to this approach - everything seems to work just fine.
Cheers!
Commits
-------
f18d797 [Live] Always send HTML over the wire instead of JSON
@@ -195,20 +189,8 @@ export default class extends Controller {
195
189
this._makeRequest(null);
196
190
}
197
191
198
-
_getValueFromElement(element: HTMLElement){
199
-
constvalue=element.dataset.value||element.value;
200
-
201
-
if(!value){
202
-
constclonedElement=(element.cloneNode());
203
-
// helps typescript know this is an HTMLElement
204
-
if(!(clonedElementinstanceofHTMLElement)){
205
-
thrownewError('cloneNode() produced incorrect type');
206
-
}
207
-
208
-
thrownewError(`The update() method could not be called for "${clonedElement.outerHTML}": the element must either have a "data-value" or "value" attribute set.`);
0 commit comments