简体   繁体   English

我可以使用类似于String#constantize的东西来初始化Hyperstack :: Component吗?

[英]Can I use something along the lines of String#constantize to initialize a Hyperstack::Component?

I have some objects in my DB that I like to render with a dedicated Hyperstack view Component. 我的数据库中有一些对象,我想使用专用的Hyperstack视图组件进行渲染。 Lets say my objects have a unique name property from A to J. Now I would like to loop through them with each and render a ComponentA , ComponentB , ComponentC , ... depending on the name of my object, and pass my object as a param to the component. 假设我的对象具有从A到J的唯一name属性。现在,我想遍历它们的each对象,并根据对象的name呈现ComponentAComponentBComponentC ,...,并将我的对象作为组件的参数。

What I do now is: 我现在要做的是:

DB_Objects.each do |object|
  if    object.name == 'A'
    ComponentA(data: object)
  elsif object.name == 'B'
    ComponentB(data: object)
  elsif object.name == 'C'
    ComponentC(data: object)
  elsif ...

What I want to do is something like: 我想做的是这样的:

DB_Objects.each do |object|
  ('Component' + object.name).constantize(data: object)
end

This is pseudo code as you can't give variables to constantize. 这是伪代码,因为您不能给变量提供常量。 But it shows what I would like to do. 但这显示了我想做的事。

How can I prevent a manual mapping of a object to its view. 如何防止将对象手动映射到其视图。

Never used HyperStack, but this should give you the right idea. 从未使用过HyperStack,但这应该给您正确的想法。

class Thing < ApplicationRecord
  OBJECT_TYPES = { 'A': 'CommponentA' }.freeze

  def to_component
    @to_component ||= OBJECT_TYPES[self.name].constantize.new(self)
  end 
end 

You could just "Component#{self.name}".constantize.new(self) but I've found that to be rather confusing to other developers when they jump in on a project. 您可以只使用"Component#{self.name}".constantize.new(self)但我发现,当其他开发人员进入某个项目时,这"Component#{self.name}".constantize.new(self)他们感到困惑。

Keep in mind String#constantize will return the actual constant, and not just a string that's constantized (camelcased and what have you) so you can call #new or #call or whatever you want on it. 请记住, String#constantize将返回实际的常量,而不仅仅是返回一个已常#new的字符串(驼峰式以及您所拥有的),因此您可以调用#new#call或任何您想要的#call

Every component class defines a method of the same name as the class. 每个组件类都定义一个与该类同名的方法。 When you say for example 当你说例如

ComponentA(data: object)

you are calling a method named ComponentA that is tied the the ComponentA class. 您正在调用一个与ComponentA类绑定的名为ComponentA的方法。

So to dynamically reference the component you would use the send method: 因此,要动态引用组件,可以使用send方法:

send('Component' + object.name, data: object)

Alternatively each component class has a class method insert_element which will render an instance of the component class, and insert it into the rendering buffer. 另外,每个组件类都有一个类方法insert_element ,它将渲染该组件类的一个实例,并将其插入到渲染缓冲区中。 For example: 例如:

('Component' + object.name).constantize.insert_element(data: object)

I bring this up because while it is longer, you could use it to generalize this idea of yours (which I think is pretty cool) 我提出这一点是因为它虽然更长,但是您可以使用它来概括您的想法(我认为这很酷)

class ApplicationRecord < ActiveRecord::Base
  def render(opts = {})
    ('Component' + object.name).constantize.insert_element(opts, data: object)
  end
end

Now you can say 现在你可以说

some_record.render

Of course to generalize it like this you might want to use something other than name . 当然,要像这样概括它,您可能要使用name以外的其他name Anyway you could really have fun with this ! 无论如何,您真的可以从中获得乐趣!

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

相关问题 我可以在没有activerecord的情况下使用hyperstack吗? - Can I use hyperstack without activerecord? 我将如何在 Hyperstack 组件中使用 React-Bootstrap? - How would I use React-Bootstrap in a Hyperstack component? ActiveSupport :: CoreExtensions :: String :: Inflections.constantize可以安全地与用户提供的数据一起使用吗? - Is ActiveSupport::CoreExtensions::String::Inflections.constantize safe to use with user-supplied data? 如何在 Hyperstack 中使用 FontAwsome Icons? - How do you use FontAwsome Icons in Hyperstack? 如何将字符串eval / constantize到方法,然后将参数传递给它 - how to eval / constantize a string to a method, then pass arguments to it 如何在自定义控制器模块中使用“ constantize” - how to use 'constantize' in custom controller module 我如何启用参数作为Hyperstack中的方法 - How do I enable params as methods in Hyperstack 如何使用content_for将某些内容放入:yield - How can I use content_for to put something in :yield 如何使用bingInt(作为字符串)作为参数? - How can I use a bingInt (as string) as a parameter? 在Rails上的ruby中播种db,由于缺少我不使用的方法而无法初始化对象 - seeding a db in ruby on rails, can't initialize a object because of a method missing I don't use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM