ruby-on-rails Rails + Turbo_stream自定义操作:我可以在没有Stimulus的情况下根据DOM状态做出有条件的响应吗?

nzk0hqpo  于 2024-01-09  发布在  Ruby
关注(0)|答案(1)|浏览(302)

我有一个页面,里面有几个用于填写学术数据的模态表单,对于每一个模态,相关的Rails控制器动作都会在一个单独的模态中为表单呈现turbo响应。
注意事项:这并不遵循“填充”页面中预先打印的模态的模式,因为在该应用程序中,用户可能需要同时进行多个模态。
所以我这样做是因为我希望用户能够在提交任何表单之前同时处理多个表单。例如,他们可以关闭一个模态以查看下面的页面,或者查看正在进行的不同模态表单。然后他们可以在页面上使用相同的切换再次打开第一个模态,并从他们离开的地方开始。
每个模态只在第一次出现时从头开始呈现,然后可以关闭或打开它,而无需将其从DOM中删除,直到提交。
我目前正在用一个Stimulus控制器来处理这个问题,它检查模态是否已经在DOM中,要么show是现有的模态,要么fetch是来自Rails控制器的响应,使用requestjs-rails来呈现空白表单模态并将其放入DOM中,然后show
刚刚发现Turbo.StreamActions,我想知道这是否可以完全在Turbo响应中完成。
但是我无法想象如何使用Turbo自定义操作来检查DOM状态(具体来说,返回一个布尔值,是否有一个具有相同ID的模态已经打印在页面中):Turbo操作似乎是Ruby --> JS的单向响应。它们用于发送“HTML over the wire”。Turbo的“payload”。StreamAction是html,而不是数据。
或者.我可以写一个自定义的Turbo.StreamActionreturn的值吗?我无法想象它是如何工作的.
这就是我被卡住的地方:

  1. # app/views/somethings/new.erb
  2. # can a turbo_stream.action take two arguments?
  3. <%
  4. args = { partial: "shared/modal_form",
  5. locals: { title: title, body: body } }
  6. <%= turbo_stream.show_or_render_modal_form(id, args) %>
  1. // app/javascript/application.js
  2. Turbo.StreamActions.open_modal = function () {
  3. $("#" + this.templateContent.textContent).modal('show')
  4. };
  5. Turbo.StreamActions.modal_exists = function () {
  6. return document.getElementById(this.templateContent.textContent)
  7. };
  8. Turbo.StreamActions.show_or_render_modal_form = function () {
  9. // ??? need to pass an id for the form, the partial path,
  10. // and local_assigns for the partial. Can that all be
  11. // packed into this.templateContent ?
  12. };
  1. # config/initializers/turbo_stream_actions.rb
  2. def show_or_render_modal_form(id, args)
  3. if action(:modal_exists, "#", id)
  4. action(:open_modal, "#", id)
  5. else
  6. action(:render, "#", **args)
  7. end
  8. end
cgfeq70w

cgfeq70w1#

这里不需要Stimulus,但是“呈现或显示模态”部分应该由JavaScript处理。

  1. <!-- view -->
  2. <!-- `content` is not necessary, you could render whatever you want from controller actions -->
  3. <!-- vvvvvvvvvvvvvv -->
  4. <%= button_to "edit something 1", "/", params: {modal: {id: :modal_1, content: "one"}} %>
  5. <%= button_to "edit something 2", "/", params: {modal: {id: :modal_2, content: "two"}} %>
  6. <!-- adjust urls as needed ^^^ -->
  7. <div id="modals"></div>
  8. <style type="text/css">
  9. .hidden { display: none; }
  10. </style>

字符串
当你点击这些按钮时,它应该首先呈现一个文本字段,第二次点击只会显示呈现的模态并隐藏所有其他模态,文本字段中的任何更改都应该保持不变。

  1. # controller
  2. respond_to do |format|
  3. format.turbo_stream do
  4. render turbo_stream: turbo_stream.action(
  5. :modal, # action
  6. "modals", # target (not strictly necessary, you could append modal to <body>)
  7. # content (modal form or anything you want to have here)
  8. helpers.tag.div(id: params[:modal][:id]) do
  9. helpers.text_field_tag :content, params[:modal][:content]
  10. end
  11. )
  12. end
  13. end
  1. // app/javascript/application.js
  2. // append action for reference: https://github.com/hotwired/turbo/blob/v7.3.0/src/core/streams/stream_actions.ts#L11
  3. Turbo.StreamActions.modal = function () {
  4. // targetElements are automatically found by Turbo which is just #modals target
  5. // it is an array because you could have multiple targets with a class selector
  6. // https://github.com/hotwired/turbo/blob/v7.3.0/src/elements/stream_element.ts#L99
  7. this.targetElements.forEach((target) => {
  8. Array.from(target.children).forEach((child) => {
  9. child.classList.add("hidden")
  10. })
  11. if (this.duplicateChildren.length > 0) {
  12. // duplicateChildren is a TurboStream function
  13. // https://github.com/hotwired/turbo/blob/v7.3.0/src/elements/stream_element.ts#L75
  14. this.duplicateChildren.forEach((modal) => {
  15. // if there is a modal already on the page, just unhide it
  16. modal.classList.remove("hidden")
  17. })
  18. } else {
  19. target.append(this.templateContent)
  20. }
  21. });
  22. };

的数据
显然,我跳过了实际的模态css外观,这将取决于你的前端css框架。

如果你需要更复杂的逻辑,你可以渲染一个<turbo-stream>标签,并带有其他可以在JavaScript中使用的属性:

  1. # config/initializers/turbo_stream_actions.rb
  2. module CustomTurboStreamActions
  3. # https://github.com/hotwired/turbo-rails/blob/v1.5.0/app/models/turbo/streams/tag_builder.rb#L214
  4. # add attributes to <turbo-stream>
  5. # vvvvvvvvvvvvvv
  6. def modal(target, content = nil, attributes: {}, allow_inferred_rendering: true, **rendering, &block)
  7. template = render_template(target, content, allow_inferred_rendering: allow_inferred_rendering, **rendering, &block)
  8. # vvvvvvvvvvvv
  9. turbo_stream_action_tag :modal, target:, template:, **attributes
  10. end
  11. ::Turbo::Streams::TagBuilder.include(self)
  12. end

  • 网址:http://github.com/hotwired/turbo-rails/blob/v1.5.0/app/helpers/turbo/streams/action_helper.rb#L26*
  1. # controller
  2. respond_to do |format|
  3. format.turbo_stream do
  4. render turbo_stream: turbo_stream.modal(
  5. "modals",
  6. helpers.tag.div(id: params[:modal][:id]) do
  7. helpers.text_field_tag :content, params[:modal][:content]
  8. end,
  9. # add some attributes
  10. attributes: {if: "something"}
  11. )
  12. #=>
  13. # <turbo-stream if="something" action="modal" target="modals">
  14. # <template>
  15. # <div id="modal_1">
  16. # <input type="text" name="content" id="content" value="one">
  17. # </div>
  18. # </template>
  19. # </turbo-stream>
  20. end
  21. end
  1. // app/javascript/application.js
  2. Turbo.StreamActions.modal = function () {
  3. console.log(this.getAttribute("if"));
  4. // ... hopefully, you get the idea.
  5. };

的字符串
但是,也许在这一点上,它会更简单,有一个刺激控制器的前端逻辑。

展开查看全部

相关问题