【问题标题】:Rails call JavaScript from PORO presenter action and return result to ViewRails 从 PORO Presenter 操作调用 JavaScript 并将结果返回给 View
【发布时间】:2023-03-04 00:46:01
【问题描述】:

我不确定最好的方法是什么,或者我的想法是否存在根本缺陷。

我有一个控制器,它在其#index 操作上创建一个普通旧 Ruby 对象 (PORO) 演示者:

# app/controllers/my_controller.rb
class MyController < ApplicationController
    def index
        @my_presenter = MyPresenter.new(
            filter_1: params[:filter_1],
            filter_2: params[:filter_2])
    end
end

以及使用来自多个模型的数据的演示者:

# app/presenters/my_presenter.rb
class MyPresenter
    def initialize(filter_1: nil, filter_2: nil)
        @my_data = MyModel.my_scope(filter_1) # scoping in the my_model.rb file
        @my_other_data = MyOtherModel.my_scope(filter_1, filter_2)
    end

    def show(view)
        # Somehow call the my_function.js from here and return the result to the view
    end
end

最后,是服从于演示者的视图:

<!-- app/views/my_controller/index.html.erb -->
<h1>The view</h1>

<%= @my_presenter.show(this) %>

我希望演示者的 #show 操作调用一些 JavaScript(用于可视化的 D3 代码),并将结果返回给视图:

// app/assests/javascripts.js
function my_function(data) {
    // D3 visualisation to go here...
    return null
}

最好的方法是什么?

【问题讨论】:

    标签: javascript ruby-on-rails presenter


    【解决方案1】:

    我已经成功使用ActiveModel::Conversion。这混合了一些允许您将视图绑定到演示者的东西。

    在您的演示者中

    class MyPresenter
    
      include ActiveModel::Conversion
    
      def initialize(...)
      ...
    
    end
    

    然后在视图中,您可以执行类似的操作

    <%=h render @my_presenter %>
    

    有了这个,Rails 将在app/views/my_presenters/_my_presenter.html.erb 中寻找代表您的小部件/演示者的视图,您可以用您的 html 来充实它。

    要获得 D3 连接,您可能应该在加载文档后附加它。假设你有 jQuery,你可以做类似的事情

    $(function() {
      var my_function = function(data) { 
        // d3 stuff here
      };
      my_function(data)
    });
    

    这可能位于您的 application.js 中,或者可能位于包含在 &lt;script&gt; 标记中的演示者部分中。

    这里有一篇关于这种技术的文章:http://blog.carbonfive.com/2014/01/07/presenters-to-widgets-with-activemodelconversion/

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 2012-02-29
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      相关资源
      最近更新 更多