Rails Cocoon CoffeeScript 插入前

Rails Cocoon CoffeeScript Before-Insert

努力理解 CoffeeScript 并在 Rails 中使用 Cocoon 调用插入前操作...我需要过滤掉与所选公司相关的项目。供应商筛选器工作正常,因为它与 Cocoon 无关,但项目筛选器不起作用。有什么帮助吗?

我的 CoffeeScript

ready = ->

  $('#quote_supplier_id').parent().hide()
  suppliers = $('#quote_supplier_id').html()
  $('#quote_company_id').change ->
    company = $('#quote_company_id :selected').text()
    escaped_company = company.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g,'\')
    options = $(suppliers).filter("optgroup[label='#{escaped_company}']").html()
    if options
      $('#quote_supplier_id').html(options)
      $('#quote_supplier_id').parent().show()
    else
      $('#quote_supplier_id').empty()
  $('#quote_supplier_id').parent().hide()

  $('#quote_transactions')
    .on('cocoon:before-insert'   
      projects = $('.projectselect').html()
      company = $('#quote_company_id :selected').text()
      escaped_company = company.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\')
      optionsprojects = $(projects).filter("optgroup[label='#{escaped_company}']").html()
      if optionsprojects
        $('.projectselect').html(optionsprojects)
      else
        $('.projectselect').empty()
    )


$(document).ready(ready)
$(document).on('page:load', ready)

我的表格

  <tbody id="quote_transactions">
    <%= f.fields_for :quote_transactions do |quote_transaction| %>
      <%= render 'transaction_fields', :f => quote_transaction %>
    <% end %>
  </tbody>
</table>

     <%= link_to_add_association '<i class="glyphicon glyphicon-plus"> Add Transaction</i>'.html_safe, f, :quote_transactions, :"data-association-insertion-node" => "tbody#quote_transactions", :"data-association-insertion-method" => "append", :partial => "transaction_fields", :type=>"button", :class=>"btn btn-default" %>

我要插入(和过滤)的字段

    <tr class='nested-fields'>
    <td>
    <div class="field">
      <%= f.grouped_collection_select :project_id, Company.order(:name), :projects, :name, :id, :description, {:include_blank => 'Please Select Project'}, {:class => 'form-control projectselect', :onfocus=>"extractId()", :onclick=>"extractId()"} %>
    </div>
    </td>
    <td>
    <div class="field">
      <%= f.text_field :description, :class=>"form-control" %>
    </div>
    </td>
    <td>
    <div class="field">
      <%= f.text_field :quote_amount, :class=>"form-control" %>
    </div>
    </td>
    <td>
    <%= link_to_remove_association '<i class="glyphicon glyphicon-remove"></i>'.html_safe, f, :class=>"btn btn-danger" %>
    </td>
    </tr>

问题是定位正确的元素,我的 CoffeeScript 已更新为:

  $('#quote_transactions').bind 'cocoon:before-insert', (e, inserted_item) ->
    projectsElem = inserted_item.find('.projectselect')
    projects = projectsElem.html()
    company = $('#quote_company_id :selected').text()
    escaped_company = company.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\')
    optionsprojects = $(projects).filter("optgroup[label='#{escaped_company}']").html()
    if optionsprojects
      projectsElem.html(optionsprojects)
    else
      projectsElem.empty()