0

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

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

# 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
}

实现这一目标的最佳方法是什么?

4

1 回答 1

0

我已经成功使用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中的演示者部分中。<script>

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

希望这可以帮助

于 2015-11-16T00:15:17.850 回答