简体   繁体   English

如何在服务器端Ruby和客户端Javascript之间干扰业务逻辑?

[英]How do I DRY up business logic between sever-side Ruby and client-side Javascript?

I have a Widget model with inheritance (I'm using Single-Table Inheritance, but it's equally valid for Class-per-Table). 我有一个带继承的Widget模型(我使用的是单表继承,但它同样适用于Class-per-Table)。 Some of the subclasses require a particular field; 一些子类需要特定的字段; others do not. 别人不这样做。

class Widget < ActiveRecord
  ALL_WIDGET_TYPES = [FooWidget, BarWidget, BazWidget]
end

class FooWidget < Widget
  validates_presence_of :color
end

class BarWidget < Widget
  # no color field
end

class BazWidget < Widget
  validates_presence_of :color
end

I'm building a "New Widget" form ( app/views/widgets/new.html.erb ) and would like to dynamically show/hide the color field based on a <select> for widget_type . 我正在构建一个“New Widget”表单( app/views/widgets/new.html.erb ),并希望根据widget_type<select>动态显示/隐藏color字段。

<% form_for @widget do |f| %>
  <%= f.select :type, Widget::ALL_WIDGET_TYPES %>
  <div class='hiddenUnlessWidgetTypeIsFooOrBaz'>
    <%= f.label :color %>
    <%= f.text_field :color %>
  </div>
<% end %>

I can easily write some jQuery to watch for onChange events on widget_type , but that would mean putting some sort of WidgetTypesThatRequireColor constant in my Javascript. 我可以轻松地编写一些jQuery来监视widget_type上的onChange事件,但这意味着在我的Javascript中放置某种WidgetTypesThatRequireColor常量。 Easy enough to do manually, but it is likely to get disconnected from the Widget model classes. 手动操作很容易,但很可能与Widget模型类断开连接。

I would prefer not to output Javascript directly in my view, though I have considered using content_for(:js) and have a yield :js in my template footer. 我宁愿不在我的视图中直接输出Javascript,虽然我已经考虑过使用content_for(:js)并且在我的模板页脚中有一个yield :js Any better ideas? 有更好的想法吗?

Here is the html I would generate. 这是我将生成的HTML。 You could generate it from the view or helper in various ways. 您可以通过各种方式从视图或帮助程序生成它。

<select ...>
    <option class="widget colored">FooWidget</option>
    <option class="widget uncolored">BarWidget</option>
    <option class="widget colored">BazWidget</option>
</select>

Then have jQ watch the select and show or hide the div based on the class of the selected option. 然后让jQ观察选择并根据所选选项的类显示或隐藏div。

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

相关问题 如何将服务器端ASP XmlHttpRequest代码转换为客户端JavaScript? - How do I convert my server-side ASP XmlHttpRequest code to client-side JavaScript? 如何将数据输入Javascript以进行客户端处理? - How can I input data to Javascript for client-side processing? 如何验证客户端 google+ 登录信息? - How do I validate a client-side google+ login? 如何将客户端属性添加到FirebaseListObservable中的项目? - How do I add client-side attributes to items in a FirebaseListObservable? 如何为 GatsbyJS 制作仅客户端的组件? - How do I make a client-side only component for GatsbyJS? 如何使用服务器上的 Node 从客户端 JavaScript 上传到 Google Cloud Storage? - How do I upload to Google Cloud Storage from client-side JavaScript, with Node on the server? 如何从客户端JavaScript的cloudinary获取我的图像列表? - How do I get a list of my images from cloudinary from client-side JavaScript? 在C#中,如何对客户端用户交互和javascript(jQuery)代码进行单元测试? - In C#, how do I unit-test client-side user interaction and javascript (jQuery) code? 如何在客户端JavaScript中对下载进行多线程 - How to multithread a download in client-side javascript 如何在客户端Javascript中调用Node / Express API密钥(.env)? - How do I call a Node/Express API key (.env) in Client-side Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM