3

在显示页面中,我显示了一个周期性事件发生的日期列表,并计算了所有日期的统计信息,例如连续日期之间的最大、最小和平均间隔。

我使用 best_in_place gem 允许就地编辑日期。但是,每次更改日期时,都必须计算统计信息并从服务器重新呈现。

如何将回调函数挂钩到 best_in_place 编辑的完成,以便可以重新渲染统计信息?

这是我在 show.html.erb 中的 Rails 代码

<td id="event_date">
  <%= best_in_place @event, :occur_date %>
</td>

在 html 中是

<td id="event_date"> 
    <span class='best_in_place' id='best_in_place_event_132_occur_date' data-url='/events/132' data-object='event' data-attribute='occur_date' data-type='input'>2012-03-23</span>
</td>

我尝试了以下咖啡脚本代码:

jQuery ->
$("#best_in_place_event_*_occur_date").live 'ajax:complete', (evt, data, status, xhr) ->
  alert "a date has changed"

这似乎不起作用,在我编辑日期(occur_date)后没有任何反应。

任何人都知道我应该如何在成功进行 best_in_place 编辑后触发事件?

4

2 回答 2

2

好吧,看看源代码,似乎 best_in_place 正确地触发了它的 loadSuccessCallback 中的 ajax:success 事件。这意味着您应该能够使用一些 jQuery 将您自己的函数绑定到更改的事件。

$(".best_in_place").live 'change', (event) ->
  alert "Hello, World!"
于 2012-04-18T01:15:13.403 回答
1

至于你的例子:

<td id="event_date">
  <%= best_in_place @event, :occur_date, :classes => 'bip_date' %>
</td>

$('.bip_date').bind("ajax:success", function(){
alert("a date has changed")
});
// or: $('.event_data .best_in_place').bind(...)    

官方自述文件中:

'ajax:success' 事件在成功时触发。

使用绑定:

$('.best_in_place').bind("ajax:success", function ()
{$(this).closest('tr').effect('highlight'); });

要绑定特定于特定字段的回调,请使用辅助方法中的“类”选项,然后绑定到该类。

<%= best_in_place @user, :name, :classes => 'highlight_on_success' %>
<%= best_in_place @user, :mail, :classes => 'bounce_on_success' %>

$('.highlight_on_success').bind("ajax:success", function(){bip_date});
$('.bounce_on_success').bind("ajax:success", function(){$(this).closest('tr').effect('bounce'));});
于 2014-03-11T22:45:27.113 回答