自定义刺激反射类

问题描述

我正在Rails 6中进行一个实验项目。对我来说,这个项目的目的是学习新方法并尝试使用新的(“ ish”)技术。

简而言之,我在Trailblazerstimulus_reflex以及view_component的陪同下使用view_component_reflex基础设施。

据我所知,StimulusReflexViewComponentReflex希望反射类驻留在reflexes文件夹/命名空间中。

我要实现的目标:

将我的ViewComponentReflex组件嵌套在trailblazer概念文件夹(命名空间)中。

我尝试过的事情:

创建了以下文件夹结构:

  • concepts/post/component/counter.rb
class Component::Counter < ApplicationComponent

  def initialize
    @loading = false
  end

  def loading=(new_value)
    @loading = new_value
    refresh! '#loader'
  end

  def do_expensive_action
    prevent_refresh!

    self.loading = true
    wait 20
    self.loading = false
  end

end
  • concepts/post/component/counter/counter.html.erb
<%= component_controller do %>
  <div id="loader">
    <% if @loading %>
      <p>Loading...</p>
    <% end %>
  </div>

  <button type="button" data-reflex="click->Component::Counter#do_expensive_action" >Load Content</button>
<% end %>

预期结果:

我希望Rails允许使用命名空间等。设置上述内容并运行示例应用程序时,出现错误uninitialized constant Component::CounterReflex。此类的依赖已内置于其中一颗宝石中,但我不确定在哪里可以找到它。我尝试覆盖ViewComponentReflex中的某些方法无济于事。

当我将Component::Counter移到components文件夹(如this example中所示)时,代码起作用。

是否有任何方法可以使用此堆栈重新定义/配置反射类的路由(模块)?

更新:

我的应用程序的GitHub存储库可以为found here

我按下计数器按钮时看到的确切错误消息是:

StimulusReflex::Channel is streaming from StimulusReflex::Channel
06:48:27 log.1       | StimulusReflex::Channel#receive({"target"=>"Component::Counter#do_expensive_action","args"=>[],"url"=>"http://krated.test/","attrs"=>{"type"=>"button","data-reflex"=>"click->Component::Counter#do_expensive_action","data-key"=>"6b36d7d05b8737b0328d19bd2fff2679901b1736bb9e242b128e3b715aba6e87","data-controller"=>"stimulus-reflex","data-action"=>"click->stimulus-reflex#__perform","checked"=>false,"selected"=>false,"tag_name"=>"BUTTON","value"=>""},"dataset"=>{"data-reflex"=>"click->Component::Counter#do_expensive_action","data-action"=>"click->stimulus-reflex#__perform"},"selectors"=>[],"reflexId"=>"a091247b-d53b-4e63-ac59-78c72c4a3cb1","permanent_attribute_name"=>"data-reflex-permanent","params"=>{}})
06:48:27 log.1       | StimulusReflex::Channel Failed to invoke Component::Counter#do_expensive_action! http://krated.test/ uninitialized constant Component::CounterReflex /Users/hermann/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.3/lib/active_support/inflector/methods.rb:284:in `const_get

解决方法

view_component_reflex的2.3.5版本期望组件类名称以Component结尾。如果他们不这样做,它将失败。参见https://github.com/joshleblanc/view_component_reflex/blob/v2.3.5/lib/view_component_reflex/engine.rb#L18

因此,请再次调用您的组件:

Component::CounterCountComponent

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...