简体   繁体   English

Rails从PORO演示者动作中调用JavaScript并将结果返回给View

[英]Rails call JavaScript from PORO presenter action and return result to View

I am unsure of what the best way of doing this is, or if my thinking is fundamentally flawed. 我不确定执行此操作的最佳方法是什么,或者不确定我的想法是否存在根本性缺陷。

I have a controller that creates a Plain Old Ruby Object (PORO) presenter on its #index action: 我有一个控制器,可在其#index动作上创建一个普通的旧Ruby对象(PO​​RO)演示者:

# 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

And the presenter which uses data from multiple models: 演示者使用来自多个模型的数据:

# 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

And, finally, the View that defers to the presenter: 最后,适合演示者的视图:

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

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

I want the #show action of the presenter to call some JavaScript (D3 code for visualisation), and return the result to the View: 我希望演示者的#show动作调用一些JavaScript(可视化的D3代码),然后将结果返回给View:

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

What is the best way to accomplish this? 做到这一点的最佳方法是什么?

I've had success using ActiveModel::Conversion . 我已经使用ActiveModel::Conversion成功了。 This mixes in a few things that allows you to tie a view to a presenter. 这混合了一些内容,使您可以将视图绑定到演示者。

In your presenter 在您的主持人中

class MyPresenter

  include ActiveModel::Conversion

  def initialize(...)
  ...

end

then in view, you can do something like 然后,您可以执行类似的操作

<%=h render @my_presenter %>

With this, Rails will look for the view that represents your widget/presenter in app/views/my_presenters/_my_presenter.html.erb which you can flesh out with your html. 这样,Rails将在app/views/my_presenters/_my_presenter.html.erb中寻找表示您的小部件/演示者的app/views/my_presenters/_my_presenter.html.erb ,您可以使用html app/views/my_presenters/_my_presenter.html.erb

To get the D3 hookup, you should probably attach it after the document is loaded. 要获取D3联播,您可能应该在加载文档后将其附加。 Assuming you've got jQuery in there, you could do something like 假设您有jQuery,则可以执行以下操作

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

This could probably sit in your application.js or maybe in the presenter partial wrapped in a <script> tag. 这可能位于您的application.js也可能位于包装在<script>标记中的presenter部分中。

There's a write up of this technique here: http://blog.carbonfive.com/2014/01/07/presenters-to-widgets-with-activemodelconversion/ 这里有此技术的文章: http : //blog.carbonfive.com/2014/01/07/presenters-to-widgets-with-activemodelconversion/

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM