简体   繁体   English

如何在ReactJS组件中使用Ruby方法?

[英]How to use Ruby methods in ReactJS components?

I am starting playing with ReactJS for my Rails apps. 我开始为Rails应用程序使用ReactJS。

One of the things I noticed very soon would be using Ruby methods in ReactJS components - typically, what I am using daily, is formatting date, shortening text/strings: 我很快注意到的一件事是在ReactJS组件中使用Ruby方法-通常,我每天使用的是格式化日期,缩短文本/字符串:

content = this.state.posts.map(post => {
            return(
                <li key={post.id}>
                        {post.created_at.strftime('%B')}
                        {post.message}
                    </li>
                )
            });

This will obviously raise an error Uncaught TypeError: post.created_at.strftime is not a function , because I am applying a ruby method on javascript data (I am used to do these operations in Rails views). 显然,这将引发错误Uncaught TypeError: post.created_at.strftime is not a function ,因为我在javascript数据上应用了ruby方法(我曾在Rails视图中执行这些操作)。

What's the correct approach here with /in ReactJS components? 在/ ReactJS组件中,这里的正确方法是什么? Should I pre-prepare the format of data from database in the controller? 我应该从控制器中的数据库中准备数据格式吗? Or should I research for JS functions to replicate the behavior of the respective Ruby methods? 还是应该研究JS函数来复制相应Ruby方法的行为?

Or is there a third way to do this a better way? 还是有第三种方法可以做得更好?

There are two issues to understand. 有两个问题需要了解。

  1. The code you have above is JSX which is translated into javascript. 您上面的代码是JSX,它已翻译为javascript。 Unless you do something you don't have Ruby on the browser (but read on) 除非您执行某些操作,否则浏览器上就没有Ruby(但请继续阅读)

  2. The second is that the actual methods like post.id and post.created_at are referring to data that exists on the server, and again unless you do something to fix this you don't have access to that data. 第二个事实是,诸如post.idpost.created_at类的实际方法都引用了服务器上存在的数据,并且除非您进行某些操作以解决该问题,否则您将无法访问该数据。

If you want to write code like you have above, but in Ruby and have it work, you can use http://ruby-hyperloop.org . 如果您想像上面那样编写代码,但是要在Ruby中运行,可以使用http://ruby-hyperloop.org You can then write the above code entirely in Ruby and it will work. 然后,您可以完全用Ruby编写上面的代码,它将起作用。

render do
  state.posts.each do |post|
    LI(key: post) do
     "#{post.created_at.strftime('%B')} #{post.message}"
    end
  end
end

To more deeply answer your question here is how hyperloop works: First it uses the Opal Ruby->JS transpiler and Rails sprockets to translate your ruby code to JS before sending it the client. 为了更深入地回答您的问题,这里是hyperloop的工作原理:首先,它使用Opal Ruby-> JS Transpiler和Rails链轮将红宝石代码转换为JS,然后再发送给客户端。 This is similar to how ERB or Haml works. 这类似于ERB或Haml的工作方式。

Secondly Hyperloop gives you a complete DSL that lets write your React Component code in Ruby instead of JSX. 其次,Hyperloop为您提供了完整的DSL,可使用Ruby(而不是JSX)编写React Component代码。

Thirdly Hyperloop keeps ActiveRecord models synchronized between the client and server. 第三,Hyperloop使ActiveRecord模型在客户端和服务器之间保持同步。 So when in the above code you do a state.posts.each this means you are will need to fetch the posts from the server, and when they arrive trigger a rerender of the react component. 因此,当在上面的代码中执行state.posts.each这意味着您将需要从服务器获取帖子,并且当它们到达时触发react组件的重新呈现。

Hyperloop components can call normal react components, and likewise react components can call hyperloop components, since under the hood everything becomes a standard React.js component. Hyperloop组件可以调用正常的React组件,同样,React组件也可以调用Hyperloop组件,因为在幕后所有东西都变成了标准的React.js组件。

BTW the comment above about keeping business logic, etc separate is correct, and is supported in Hyperloop using the same mechanisms as rails, but that is another topic I think. 顺便说一句,上面关于保持业务逻辑等分离的评论是正确的,并且在Hyperloop中使用与rails相同的机制得到了支持,但这是我认为的另一主题。

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

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