Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

javascript - Hotwire: How to render two partials for a broadcasted object on to two different pages?

Relatively new to Stimulus and Hotwire and wanted to know,

"Is there a way to render two different partials for the same broadcasted object on two different pages?

I’m trying to render and broadcast an object on a page from one controller to another page from different controller with different styling.

Example If I print <%= render @live_room.room.questions %> in page live_room.html.erb and print <%= render @room.questions %> in show.html.erb

They both render the partial _question.html.erb

I would like render a different design depending on the page.

Has anyone faced this issue before or know how to solve this? I’d love to get some insight!

Cheers!

question from:https://stackoverflow.com/questions/65907777/hotwire-how-to-render-two-partials-for-a-broadcasted-object-on-to-two-different

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can specify which partials to render. For example, after the cart model is updated a replacement is broadcasted to the badge_id and another one to cart. Each renders a unique partial.

class Cart
  ...

  after_update_commit do
    broadcast_replace_to 'badge',
                         partial: 'carts/badge',
                         locals: { cart: self },
                         target: "badge_#{id}"
  end

  after_update_commit do
    broadcast_replace_to 'cart',
                         partial: 'carts/active_cart',
                         locals: { cart: self }
  end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...