【问题标题】:Rails 3.2 Trying to Reduce Repetition in jQuery Ajax Remote FormsRails 3.2 尝试减少 jQuery Ajax 远程表单中的重复
【发布时间】:2012-05-22 09:41:41
【问题描述】:

我在 Rails 3.2 中开发了一个后端,允许管理员使用 ajax 和 jQuery 执行 CRUD 操作。我已经使用 js.coffee 文件处理了几个控制器/视图来处理 ajax。我的视图文件结构示例如下。

views
  users
    index.html.erb
    _form.html.erb
    _edit_form.html.erb
    _user.html.erb
    create.js.coffee
    edit.js.coffee        
    update.js.coffee
    destroy.js.coffee
  customers
    index.html.erb
    _form.html.erb
    _edit_form.html.erb
    _customer.html.erb
    create.js.coffee
    edit.js.coffee        
    update.js.coffee
    destroy.js.coffee
   email_blasts
     index.html.erb
     _form.html.erb
     _edit_form.html.erb
     _blast.html.erb
     create.js.coffee
     edit.js.coffee        
     update.js.coffee
     destroy.js.coffee
  .
  .
  .
  .

用户视图和客户视图的示例 create.js.coffee 如下所示。请注意遵循精确模式的实例变量和 div 名称。下面的所有咖啡脚本都遵循我上面列出的所有视图中的确切结构。上面列出的所有 create.js.coffee 文件实际上是相同的。我的问题是减少重复的最佳方法是什么?这会是我应该为其创建插件的东西吗?我应该把它放在一个助手中......等等。我不确定如何在 Rails 中处理这个问题,因为我是一名前 php 程序员 :( 任何人的帮助将不胜感激!

为用户视图创建.js.coffee:

$('#errors').empty()
$('#errors').show()
<% if @user.errors.any? %>
  $('<%= escape_javascript(render :partial => "errors", :locals => {:target => @user })%>')
    .appendTo('#errors')
<% else %>
  $('<%= escape_javascript(render(:partial => @user))%>')
    .appendTo('#user_table')
    .hide()
    .fadeIn(200)
$('#errors').hide()
$('#new_user')[0].reset()
$('#users_count').html '<%= users_count %>'
<% end %>
$('#error_close').click ->
  $('#errors').fadeOut() 

为客户视图创建.js.coffee

$('#errors').empty()
$('#errors').show()
<% if @customer.errors.any? %>
  $('<%= escape_javascript(render :partial => "errors", :locals => {:target => @customer })%>')
    .appendTo('#errors')
<% else %>
  $('<%= escape_javascript(render(:partial => @customer))%>')
    .appendTo('#customer_table')
    .hide()
    .fadeIn(200)
  $('#errors').hide()
  $('#new_customer')[0].reset()
  $('#customers_count').html '<%= customers_count %>'
<% end %>
$('#error_close').click ->
  $('#errors').fadeOut()

【问题讨论】:

    标签: ruby-on-rails-3 jquery ruby-on-rails-3.1 coffeescript


    【解决方案1】:

    您应该构建您的代码,如果您想重用它,请停止混合 javascript 和 erb。制作完成这项工作的 JavaScript 类,并将数据放在页面中的脚本标记旁边...

    尝试使用一些框架,通过提供像 Backbone.js 这样的结构来帮助你。

    这可能对你有用:

    或者乱七八糟的方式:

    # Function 
    foo = (condition, firstBlock, secondBlock) ->
      $('#errors').empty()
      $('#errors').show()
      if condition
        firstBlock
          .appendTo('#errors')
      else
        secondBlock
          .appendTo('#customer_table')
          .hide()
          .fadeIn(200)
        $('#errors').hide()
        $('#new_customer')[0].reset()
        $('#customers_count').html '<%= customers_count %>'
      $('#error_close').click ->
        $('#errors').fadeOut()
    
    # Execution
    foo(<% @user.errors.any? %>, $('<%= stuff %>'), $('<%= moreStuff %>'))
    

    【讨论】:

    • 为什么混合使用 erb 和 javascript 会使代码重用变得更加困难?有没有办法在不使用服务器端 javascript 的情况下完成我需要的事情?
    • 您希望如何针对特定情况重用充满预设变量的代码?您可以重用通用和干净的东西,这段代码只是特定的。因此,您需要提取通用部分并在需要的地方对其进行参数化。所以大部分代码都放在一个可重用的类中,每个使用它的页面都可以使用 erb 和所有参数调用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多