【问题标题】:Ruby on Rails 4 and cocoon gem : How to interrupt deletion on cocoon:before-removeRuby on Rails 4 和 cocoon gem:如何中断 cocoon 上的删除:before-remove
【发布时间】:2014-04-15 05:02:06
【问题描述】:

我正在寻找一种方法来更复杂地确认我现在使用的那个看起来像这样:

     <%= link_to_remove_association "<i class='icon-remove-sign'></i>".html_safe, 
      p, :class => 'btn-link remove has-tooltip',
     :data => {:original_title => "Delete phone", 
     :confirm => 'Are you sure you want to delete this phone?'} %>

如果电话条目为空白,我只想避免确认对话框。

我认为在 'cocoon:before-remove' 事件中一定有办法,但我找不到它?

例如,在这样的 Coffesecript 函数中:

$(document).delegate '.phones', 'cocoon:before-remove', (e, item) ->

    tel = $(item).find('.tel .form-control')
    conf = true
    if tel.val().trim() != ''
      conf = confirm('Are you sure you want to delete this phone?')

    if conf
      $(@).data('remove-timeout', 1000)
      item.fadeOut('slow')
    else
      #stop deletion!!

    conf

有什么线索吗?

【问题讨论】:

    标签: ruby-on-rails ruby coffeescript cocoon-gem


    【解决方案1】:

    实际上,您可以在处理程序中为 'cocoon:before-remove' 调用 event.preventDefault()

    阻止事件默认行为将取消删除操作。

    【讨论】:

      【解决方案2】:

      cocoon 似乎缺少此功能,但我只是分叉了 repo 并添加了它。目前,您可以执行以下操作:

      更改您的 gemfile:

      gem 'cocoon', git: 'https://github.com/BroiSatse/cocoon.git'
      

      然后修改你的处理程序:

      $(document).delegate '.phones', 'cocoon:before-remove', (e, item, result) ->
      
        tel = $(item).find('.tel .form-control')
        conf = true
        if tel.val().trim() != ''
          conf = confirm('Are you sure you want to delete this phone?')
      
        if conf
          $(@).data('remove-timeout', 1000)
          item.fadeOut('slow')
        else
          result.val = false
      

      注意处理程序的额外参数。

      【讨论】:

      • 哇,非常感谢!我现在只是在测试它,它就像一个魅力......我喜欢开源社区和 GitHub :-)
      • 请注意,这可能会误导客户,因为在提交表单之前不会删除记录。如果客户确认他想删除它然后导航回来,他可能会感到惊讶。
      猜你喜欢
      • 2016-01-09
      • 1970-01-01
      • 2015-10-27
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 2016-02-14
      相关资源
      最近更新 更多