如何触发 best_in_place 事件

Posted

技术标签:

【中文标题】如何触发 best_in_place 事件【英文标题】:How to trigger best_in_place events 【发布时间】:2014-04-14 10:15:15 【问题描述】:

我查看了所有的 Google 和 ***,并查看了 best_in_place javascript 代码,但无济于事。

我正在使用 best_in_place 通过使用 textarea 来编辑 Post 模型的属性,如下所示:

<%= best_in_place @post, :summary, 
                  :type => :textarea, :display_with => 'simple_format'  %>

我希望能够在按下 Enter/Return 时触发 best_in_place 的提交功能,除非它被按下 shift 键(这将允许回车),就像在可编辑区域之外单击时一样。使用常规文本/输入,这是默认功能(减去回车部分)。但是,当使用 textareas 时,它不是。

我从here 中提取了一个不错的 jQuery,它处理识别何时按下带有/不带 shift 的 Enter,但我不知道如何触发 best_in_place 来提交数据。

//Have to bind it to the activate function, or else 
//the element won't be in the DOM when it tries to identify it
$('.best_in_place').bind('best_in_place:activate', function()          
   $(this).find('textarea').keyup(function(e) 
      var $textarea = $(this);
      if(e.keyCode == 13 && !e.shiftKey) 
        e.preventDefault(); // Don't make a new line         
        // This line does weird things
       $(this).parents('.best_in_place').trigger('best_in_place:update').trigger('best_in_place:deactivate');
      
    );
);

我还有一些绑定到best_in_place:update 函数的函数,它们基本上添加和删除样式类。使用上面的代码,当我在 best_in_place 文本区域中按 Enter 键时,我绑定到 best_in_place:update 函数的函数会触发,但没有数据提交到服务器,正如我在控制台中看到的那样。

TL;DR;如何触发 best_in_place 提交/更新并在按下 Enter 键时停用,就像在可编辑区域外单击时一样?

【问题讨论】:

【参考方案1】:

$('body').click();可能是?没有使用过这个框架,但是如果没有提交按钮并且失去对输入提交表单的关注,可能会起作用。

【讨论】:

嗯,可能会触发 textarea 上的模糊事件。我相信这就是停用事情发生的地方。

以上是关于如何触发 best_in_place 事件的主要内容,如果未能解决你的问题,请参考以下文章

我如何将 country_select gem 与 best_in_place 编辑集成

如何让 best_in_place 的 respond_with_bip 工作?

ruby on rails + best_in_place + 数据表 + haml

样式占位符与 best_in_place 的值不同

如何找到在 BigQuery 中触发设置事件之前触发的事件?

页面按钮如何触发声音事件?