Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...