Skip to content

Conversation

@indigotechtutorials
Copy link
Contributor

@indigotechtutorials indigotechtutorials commented Sep 23, 2021

Allows option to pass in html to broadcast from model
something like this will allow you to add html to a broadcast instead of a partial or otherwise automatically finding a partial with to_partial_path
broadcast_replace_to(:feed, html: "<p> Random HTML </p>")

Allows option to pass in html to broadcast from model this works with the **rendering methods included from the Turbo::Broadcastable module.

@indigotechtutorials indigotechtutorials changed the title Broadcast html from model Broadcast HTML from model Sep 23, 2021
@indigotechtutorials indigotechtutorials changed the title Broadcast HTML from model Broadcast HTML from Model Sep 23, 2021
@dhh
Copy link
Member

dhh commented Sep 23, 2021

Nice! Can you amend docs too?

@seanpdoyle
Copy link
Contributor

Since you're a first time contributor, a maintainer with GitHub permissions will need to intervene so that your tests to run in CI.

@indigotechtutorials
Copy link
Contributor Author

Tests failed last time but pass in development

@indigotechtutorials
Copy link
Contributor Author

I would really like to get this merged in I need a rerun of the cli @dhh

@dhh dhh merged commit 3992b7e into hotwired:main Sep 30, 2021
@indigotechtutorials indigotechtutorials deleted the Broadcast-HTML-From-Model branch September 30, 2021 17:58
@bgvo
Copy link

bgvo commented Oct 12, 2021

In our project we have had the need to broadcast a template instead of a partial. I think the code would be very similar to the one @yunggindigo introduced. Something like:

unless o.include?(:html) || o.include?(:template)
  o[:partial] ||= to_partial_path
end

@seanpdoyle
Copy link
Contributor

@bgvo the option to pass inline: to (evaluate an inline template) was discussed, but ultimately it was decided it would remain unsupported. Templates with interpolation should be declared elsewhere in app/views files. I believe html: was added to support objects that return escaped and HTML template strings.

What is the use case you have in mind?

@romanos
Copy link

romanos commented Oct 13, 2021

I've run into (I think) a similar problem. There are times the turbo frame needing an update lives in a template, rather than a partial. There seems to be no way for me to target that frame from the broadcast command. Since this is based loosely on ActionView::ObjectRenderer I would have expected to be able to pass template: 'path/to/template' rather than just specifying the partial.

Especially for more complex relationships when it's not just the object itself but related objects or more complex views that might need the update. I've updated my code in a similar way to @bgvo above.

@seanpdoyle
Copy link
Contributor

@romanos if you were to broadcast_render_later_to, and declare a partial: option or rely on ActionView::ObjectRenderer to infer the partial path, would you have access to render template: "..." from within that partial?

@indigotechtutorials
Copy link
Contributor Author

indigotechtutorials commented Oct 13, 2021

@romanos For this problem it would make sense to put the content that lives currently in the turbo-frame into a partial and render it in the turbo-frame initially and then whenever you need to update the turbo-frame you can broadcast_update_to your frame which will replace the inner html with the partial with the new data.

@romanos
Copy link

romanos commented Oct 14, 2021

Yes these workarounds... work. But it smelled funny so I went with the aforementioned method. I've been keeping my eye out cause I can't be the only one.

(From mobile)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants