【问题标题】:Rails turbo frame response not firing turbo:loadRails turbo 帧响应未触发 turbo:load
【发布时间】:2021-03-29 16:20:15
【问题描述】:

我有一个使用Turbo 的标准内联编辑,它将show 视图的turbo 框架替换为_form 中的turbo 框架。但在这种情况下,$(document).on "turbo:load", -> 事件不会触发。

我没有看到应该使用的another event。任何想法如何在替换涡轮框架的响应上运行 js? (例如在表单中初始化日期选择器)

【问题讨论】:

    标签: ruby-on-rails hotwire-rails turbo-rails turbo-frames


    【解决方案1】:

    框架加载时触发的事件仍在构建中:https://github.com/hotwired/turbo/pull/59

    我们目前仅有的事件是turbo:before-fetch-requestturbo:before-fetch-response,它们在每次Turbo 更改框架或导航页面时在document 级别触发。框架的内容不会在事件触发时加载,但它允许您使用框架的 loaded 属性来连接在框架加载时运行的承诺。

    $(document).on("turbo:before-fetch-response", (e) ->
      frame = document.getElementById("myFrame")
      # Frame has finished loading
      if frame.complete
        #run my code
      else
        # Frame is still loading, run this code when it finishes
        frame.loaded.then () ->
          # run my code
    
    

    否则,您可以编写一个 Stimulus 控制器来包装您的日期选择器初始化,这样您就不必注意它进出 DOM。

    【讨论】:

    • 谢谢你,但我更担心2021年还有人写coffeescript...
    • 我写了coffeescript,因为这似乎是问题的内容......
    • 很公平! :)
    • 问题中的咖啡脚本不是在 2021 年编写的,如果这可以缓解您的担忧@Askdesigners!
    猜你喜欢
    • 2023-01-08
    • 2023-02-02
    • 1970-01-01
    • 2015-05-31
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多